Upload
kurt-mbanje
View
438
Download
0
Embed Size (px)
Citation preview
Android Content Providers
Kurt Mbanje : DStv Digital Media+KurtMbanje@ckurtm
What's a Content Provider?
• They provide content to applications.• They encapsulate data and provide it to applications through the single
ContentResolver interface
Why?
• Share data between applications.• Predictive search within your app• Leverage power of loaders with cursors.
Ok so how does this work?
App 1 has content (database) and a registered ContentProvider
App 2 uses ContentResolver and content Uri to request data from a ContentProvider
Android uses the Uri provided by App 2 to find the matching registered ContentProvider to send and receive data from
The ContentProvider for App 1 uses this Uri to find the data and returns it to the ContentResolver supplied by App 2
Components
• android.content.ContentResolver getContext().getContentResolver()• android.net.Uri
Content Uri android.net.Uri
• The scheme – content://• Authority • Path usually matches a table name in your database• ID (optional) used for further filtering e.g. to get a specific row
android.content.ContentResolver
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Query
insert(Uri url, ContentValues values)INSERT
update(Uri uri, ContentValues values, String where, String[] selectionArgs)
Update
delete(Uri url, String where, String[] selectionArgs)
Delete
android.content.ContentResolver
Query
Example: Query Calendar
android.content.ContentResolver
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Query
insert(Uri url, ContentValues values)INSERT
update(Uri uri, ContentValues values, String where, String[] selectionArgs)
Update
delete(Uri url, String where, String[] selectionArgs)
Delete
android.content.ContentResolver
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Query
insert(Uri url, ContentValues values)INSERT
update(Uri uri, ContentValues values, String where, String[] selectionArgs)
Update
delete(Uri url, String where, String[] selectionArgs)
Delete
Example: Update event in Calendar
android.content.ContentResolver
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Query
insert(Uri url, ContentValues values)INSERT
update(Uri uri, ContentValues values, String where, String[] selectionArgs)
Update
delete(Uri url, String where, String[] selectionArgs)
Delete
Create a Content Provider1. Create an Authority
2. Create a your class that extends from ContentProvider
3. Implement the matching ContentResolver methods
4. Register your ContentProvider in manifest & mark export to true (only if you wish to share your data)
1: Create an Authority
2: Create your ContentProvider1. Extend From ContentProvider
2. Implement OnCreate
3. Create your Uri matcher
4. Overide methods e.g. query
3: Create your ContentProvider : Query Method
Type - public String getType(Uri uri)
Delete - public abstract int delete (Uri uri, String selection, String[] selectionArgs)
Update - public abstract int update (Uri uri, ContentValues values, String selection, String[] selectionArgs)
Insert - public abstract Uri insert (Uri uri, ContentValues values)
3: Create your ContentProvider : Other Methods
4: Register your ContentProvider
DroidProviderhttps://github.com/ckurtm/DroidProvider
Thanks
+KurtMbanje