Content Provider 사용하는 두가지 경우
- 내 애플리케이션에서 다른 애플리케이션의 Content Provider에 접근해서 데이터에 엑세스하기 위해
- 내 애플리케이션에 Content Provider를 만들어서 다른 애플리케이션에 데이터 제공하기 위해
ex) 사진첩 앱에는 콘텐츠 프로바이더가 구현되어있어서 다른 앱에서 가져오기 가능
queries
태그 사용, provider
태그로 authorities
속성 줘도 됨.<queries>
<package android:name="com.example.test_outer"/>
<!-- 요것도 가능 -->
<!-- <provider android:authorities="com.example.test_provider"/>-->
</queries>
CursorLoader
호출해 ContentResolver
객체 이용ContentResolver.query()
사용하여 콘텐츠 프로바이더 호출contentResolver.query(
Uri.parse("content://com.example.test_provider"),
null, null, null, null)
)
cursor = contentResolver.query(
UserDictionary.Words.CONTENT_URI, // 테이블의 주소, URI
projection, // 선택해줄 column, String배열
selectionClause, // 쿼리의 조건문
selectionArgs.toTypedArray(), // 쿼리의 조건문의 argument, 물음표를 대체할 값들
sortOrder // 쿼리 소팅 조건
)
contentResolver.insert(uri, values)
- 이 때 URI는 데이터를 가져올 경로이다.
content://호스트/경로
로 사용
ex) conent://com.example.test_provider/user/1
=> test_provider 앱의 user 테이블의 1 행
moveToNext
메소드로 결과를 하나씩 확인 가능 ContentProvider()
를 상속받아서 작성autorities
속성 필수로 선언! 외부에서 이 콘텐츠 프로바이더를 이용할 때 식별값으로 사용되는 고유한 값. <provider
android:name=".MyContentProvider"
android:authorities="com.example.test_provider"
android:enabled="true"
android:exported="true"></provider>
참고한 블로그
https://lucky516.tistory.com/171
https://velog.io/@ows3090/Android-ContentProvider-%EA%B5%AC%ED%98%84-%EB%B0%8F-%EC%82%AC%EC%9A%A9%EB%B2%95#contentprovider-%ED%81%B4%EB%9E%98%EC%8A%A4-%EA%B5%AC%ED%98%84
https://nam8399.github.io/android/msixteenth/