Ahmad Arif Faizin
Google Associate Android Developer, Curriculum Developer Dicoding
Penyedia data untuk aplikasi lain
// Sets the columns to retrieve for the user profile
val projection = arrayOf(
ContactsContract.Profile._ID,
ContactsContract.Profile.DISPLAY_NAME_PRIMARY,
ContactsContract.Profile.PHOTO_THUMBNAIL_URI,
)
// Retrieves the profile from the Contacts Provider
val profileCursor = contentResolver.query(
ContactsContract.Profile.CONTENT_URI,
projection,
null,null,null //selection, selectionArgs, sortOrder
)
● ContentResolver : used to access data in a content provider. The
ContentResolver methods provide the basic "CRUD" (create, read, update, and
delete) functions of persistent storage.
● Content URIs : used to identify data in a provider. Content URIs include the
symbolic name of the entire provider (its authority) and a name that points to a
table (a path).
● Provider : a class that inherits to ContentProvider to implement CRUD operation
to database.
1. Buat Provider Class yang extends ke ContentProvider
2. Daftarkan Provider Class di Manifest
3. Tentukan Content URI
4. Tambah URI ke Contract Class
5. Buat URI Matcher
6. Lengkapi fungsi CRUD yang dibutuhkan
7. Coba untuk akses menggunakan ContentResolver
class NoteProvider : ContentProvider() {
private lateinit var noteHelper: NoteHelper
override fun onCreate(): Boolean {
// database initialization
noteHelper = NoteHelper.getInstance(context as Context)
noteHelper.open()
return true
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dicoding.picodiploma.mynotesapp">
<permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"
android:protectionLevel="normal" />
<permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"
android:protectionLevel="normal" />
<application...>
...
<provider
android:authorities="com.dicoding.picodiploma.mynotesapp"
android:name=".provider.NoteProvider"
android:exported="true"
android:readPermission="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"
android:writePermission="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/>
</application>
</manifest>
● Format:
<scheme>://<authorities>/<path>
<scheme>://<authorities>/<path>/# (untuk number)
<scheme>://<authorities>/<path>/* (untuk string)
● Example:
○ Get All Note:
content://com.dicoding.picodiploma.mynotesapp/note
○ Note with id:
content://com.dicoding.picodiploma.mynotesapp/note/#
object DatabaseContract {
const val AUTHORITY = "com.dicoding.picodiploma.mynotesapp"
const val SCHEME = "content"
class NoteColumns : BaseColumns {
companion object {
…
//result = content://com.dicoding.picodiploma.mynotesapp/note
val CONTENT_URI: Uri = Uri.Builder()
.scheme(SCHEME)
.authority(AUTHORITY)
.appendPath(TABLE_NAME)
.build()
}
}
}
class NoteProvider : ContentProvider() {
companion object {
private const val NOTE = 1
private const val NOTE_ID = 2
private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH)
init {
// content://com.dicoding.picodiploma.mynotesapp/note
sUriMatcher.addURI(AUTHORITY, TABLE_NAME, NOTE)
// content://com.dicoding.picodiploma.mynotesapp/note/id
sUriMatcher.addURI(AUTHORITY, "$TABLE_NAME/#", NOTE_ID)
}
}
...
}
class NoteProvider : ContentProvider() {
override fun query(uri: Uri, strings: Array<String>?, s: String?,
strings1: Array<String>?, s1: String?): Cursor? {
return when (sUriMatcher.match(uri)) {
// content://com.dicoding.picodiploma.mynotesapp/note
NOTE -> noteHelper.queryAll()
// content://com.dicoding.picodiploma.mynotesapp/note/1
NOTE_ID -> noteHelper.queryById(uri.lastPathSegment.toString())
else -> null
}
}
override fun insert(...): Uri? {...}
override fun update(...): Int {...}
override fun delete(...): Int {...}
}
val uriWithId = Uri.parse(CONTENT_URI.toString() + "/" + note?.id)
val cursor = contentResolver.query(uriWithId, null, null, null, null)
val cursor = contentResolver.query(CONTENT_URI,null, null, null, null)
● Get All Data
● Get Data by id
contentResolver.update(uriWithId, values, null, null)
contentResolver.insert(CONTENT_URI, values)
● Insert Data
● Update Data
contentResolver.delete(uriWithId, null, null)
● Delete Data
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dicoding.picodiploma.consumerapp">
<uses-permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"/>
<uses-permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/>
...
<queries>
<package android:name="com.dicoding.picodiploma.mynotesapp" />
</queries>
</manifest>
Contact:
@arif_faizin
Follow us: @dicoding

Dicoding Developer Coaching #20: Android | Apa itu Content Provider?

  • 1.
    Ahmad Arif Faizin GoogleAssociate Android Developer, Curriculum Developer Dicoding
  • 2.
    Penyedia data untukaplikasi lain
  • 4.
    // Sets thecolumns to retrieve for the user profile val projection = arrayOf( ContactsContract.Profile._ID, ContactsContract.Profile.DISPLAY_NAME_PRIMARY, ContactsContract.Profile.PHOTO_THUMBNAIL_URI, ) // Retrieves the profile from the Contacts Provider val profileCursor = contentResolver.query( ContactsContract.Profile.CONTENT_URI, projection, null,null,null //selection, selectionArgs, sortOrder )
  • 5.
    ● ContentResolver :used to access data in a content provider. The ContentResolver methods provide the basic "CRUD" (create, read, update, and delete) functions of persistent storage. ● Content URIs : used to identify data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). ● Provider : a class that inherits to ContentProvider to implement CRUD operation to database.
  • 6.
    1. Buat ProviderClass yang extends ke ContentProvider 2. Daftarkan Provider Class di Manifest 3. Tentukan Content URI 4. Tambah URI ke Contract Class 5. Buat URI Matcher 6. Lengkapi fungsi CRUD yang dibutuhkan 7. Coba untuk akses menggunakan ContentResolver
  • 7.
    class NoteProvider :ContentProvider() { private lateinit var noteHelper: NoteHelper override fun onCreate(): Boolean { // database initialization noteHelper = NoteHelper.getInstance(context as Context) noteHelper.open() return true } }
  • 8.
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dicoding.picodiploma.mynotesapp"> <permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE" android:protectionLevel="normal"/> <permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE" android:protectionLevel="normal" /> <application...> ... <provider android:authorities="com.dicoding.picodiploma.mynotesapp" android:name=".provider.NoteProvider" android:exported="true" android:readPermission="com.dicoding.picodiploma.mynotesapp.READ_DATABASE" android:writePermission="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/> </application> </manifest>
  • 9.
    ● Format: <scheme>://<authorities>/<path> <scheme>://<authorities>/<path>/# (untuknumber) <scheme>://<authorities>/<path>/* (untuk string) ● Example: ○ Get All Note: content://com.dicoding.picodiploma.mynotesapp/note ○ Note with id: content://com.dicoding.picodiploma.mynotesapp/note/#
  • 10.
    object DatabaseContract { constval AUTHORITY = "com.dicoding.picodiploma.mynotesapp" const val SCHEME = "content" class NoteColumns : BaseColumns { companion object { … //result = content://com.dicoding.picodiploma.mynotesapp/note val CONTENT_URI: Uri = Uri.Builder() .scheme(SCHEME) .authority(AUTHORITY) .appendPath(TABLE_NAME) .build() } } }
  • 11.
    class NoteProvider :ContentProvider() { companion object { private const val NOTE = 1 private const val NOTE_ID = 2 private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH) init { // content://com.dicoding.picodiploma.mynotesapp/note sUriMatcher.addURI(AUTHORITY, TABLE_NAME, NOTE) // content://com.dicoding.picodiploma.mynotesapp/note/id sUriMatcher.addURI(AUTHORITY, "$TABLE_NAME/#", NOTE_ID) } } ... }
  • 12.
    class NoteProvider :ContentProvider() { override fun query(uri: Uri, strings: Array<String>?, s: String?, strings1: Array<String>?, s1: String?): Cursor? { return when (sUriMatcher.match(uri)) { // content://com.dicoding.picodiploma.mynotesapp/note NOTE -> noteHelper.queryAll() // content://com.dicoding.picodiploma.mynotesapp/note/1 NOTE_ID -> noteHelper.queryById(uri.lastPathSegment.toString()) else -> null } } override fun insert(...): Uri? {...} override fun update(...): Int {...} override fun delete(...): Int {...} }
  • 13.
    val uriWithId =Uri.parse(CONTENT_URI.toString() + "/" + note?.id) val cursor = contentResolver.query(uriWithId, null, null, null, null) val cursor = contentResolver.query(CONTENT_URI,null, null, null, null) ● Get All Data ● Get Data by id
  • 14.
    contentResolver.update(uriWithId, values, null,null) contentResolver.insert(CONTENT_URI, values) ● Insert Data ● Update Data contentResolver.delete(uriWithId, null, null) ● Delete Data
  • 18.
    <?xml version="1.0" encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.dicoding.picodiploma.consumerapp"> <uses-permission android:name="com.dicoding.picodiploma.mynotesapp.READ_DATABASE"/> <uses-permission android:name="com.dicoding.picodiploma.mynotesapp.WRITE_DATABASE"/> ... <queries> <package android:name="com.dicoding.picodiploma.mynotesapp" /> </queries> </manifest>
  • 19.