Content providers in Android

3,642 views

Published on

Published in: Education
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,642
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
236
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Content providers in Android

  1. 1. Content Providers in Android
  2. 2. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  3. 3. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  4. 4. Overall structure Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 RemoteDatabase Files XML … connection
  5. 5. Content Provider is a source Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 RemoteDatabase Files XML … connection
  6. 6. For some consumers Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 RemoteDatabase Files XML … connection
  7. 7. Gives access to variety types of data Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 Remote Database Files XML … connection
  8. 8. Overall structure Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 RemoteDatabase Files XML … connection
  9. 9. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  10. 10. Interaction with Content Provider Application #2 Application #1 Activity #1 Activity #1 Activity #2 Content Provider Application #3 Activity #3 Activity #1 Activity #2 RemoteDatabase Files XML … connection
  11. 11. Activity to Content Provider accessActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  12. 12. ActivityActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  13. 13. Performing request Content Provider Query InsertContentResolver URI Update Delete
  14. 14. URIcontent://com.example.provider/articles Scheme Authority Path
  15. 15. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  16. 16. Constructing querySELECT _id, title, content, dateFROM articlesWHERE date >= 1352470000ORDER BY date ASC
  17. 17. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  18. 18. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  19. 19. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  20. 20. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  21. 21. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  22. 22. Constructing queryString[] mProjection = { "_id", "title", "content", "date", };String mSelection = "date >= ?";String[] mSelectionArgs = {"1352470000"};String mSortOrder = "date ASC";Cursor cursor = getContentResolver().query( MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  23. 23. Cursor _id title content date1 First article Lorem ipsum... 13524750132 Second article Dolor sit amet... 1352471413... ... ... ... if (mCursor != null) { while (mCursor.moveToNext()) { String title = mCursor.getString(Columns.TITLE); } }
  24. 24. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  25. 25. Activity & blocking queriesActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  26. 26. Activity & non blocking queriesActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  27. 27. Activity & LoaderActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  28. 28. Activity & Loaderpublic class ArticlesActivity extends FragmentActivity implementsLoaderCallbacks<Cursor> {...getSupportLoaderManager().initLoader(0, null, this);...public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( this, // context MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);}public void onLoadFinished( Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor);}
  29. 29. Activity & Loaderpublic class ArticlesActivity extends FragmentActivity implementsLoaderCallbacks<Cursor> {...getSupportLoaderManager().initLoader(0, null, this);...public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( this, // context MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);}public void onLoadFinished( Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor);}
  30. 30. Activity & Loaderpublic class ArticlesActivity extends FragmentActivity implementsLoaderCallbacks<Cursor> {...getSupportLoaderManager().initLoader(0, null, this);...public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( this, // context MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);}public void onLoadFinished( Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor);}
  31. 31. Activity & Loaderpublic class ArticlesActivity extends FragmentActivity implementsLoaderCallbacks<Cursor> {...getSupportLoaderManager().initLoader(0, null, this);...public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( this, // context MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);}public void onLoadFinished( Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor);}
  32. 32. Activity & Loaderpublic class ArticlesActivity extends FragmentActivity implementsLoaderCallbacks<Cursor> {...getSupportLoaderManager().initLoader(0, null, this);...public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader( this, // context MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);}public void onLoadFinished( Loader<Cursor> loader, Cursor cursor) { mAdapter.swapCursor(cursor);}
  33. 33. Activity & AsyncQueryHandlerActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  34. 34. Activity & AsyncQueryHandlerprivate AsyncQueryHandler mHandler;...mHandler = new MyAsyncQueryHandler(getContentResolver());mHandler.startQuery( 0, // token null, // cookie MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  35. 35. Activity & AsyncQueryHandlerclass MyAsyncQueryHandler extends AsyncQueryHandler { public MyAsyncQueryHandler(ContentResolver cr) { super(cr); } @Override protected void onQueryComplete( int token, Object cookie, Cursor cursor) { mAdapter.swapCursor(cursor); }}
  36. 36. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  37. 37. Permissions<permission android:name="com.example.provider.permission.READ_ARTICLES" android:protectionLevel="normal" /><provider android:name=".MyContentProvider" android:exported="false" android:authorities="com.example.provider" android:permission="com.example.provider.permission.READ_ARTICLES"/><uses-permission android:name="com.example.provider.permission.READ_ARTICLES" />
  38. 38. Permissions<permission android:name="com.example.provider.permission.READ_ARTICLES" android:protectionLevel="normal" /><provider android:name=".MyContentProvider" android:exported="false" android:authorities="com.example.provider" android:permission="com.example.provider.permission.READ_ARTICLES"/><uses-permission android:name="com.example.provider.permission.READ_ARTICLES" />
  39. 39. Permissions<permission android:name="com.example.provider.permission.READ_ARTICLES" android:protectionLevel="normal" /><provider android:name=".MyContentProvider" android:exported="false" android:authorities="com.example.provider" android:permission="com.example.provider.permission.READ_ARTICLES"/><uses-permission android:name="com.example.provider.permission.READ_ARTICLES" />
  40. 40. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  41. 41. Creating content providerpublic class MyContentProvider extends ContentProvider {...onCreate()query()insert()update()delete()getType()
  42. 42. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  43. 43. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  44. 44. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  45. 45. URI matchingsUriMatcher.addURI("com.example.provider", "articles/#", 0);sUriMatcher.addURI("com.example.provider", "articles/today", 1);sUriMatcher.addURI("com.example.provider", "articles/history/*", 2);...public String getType(Uri uri) { switch (sUriMatcher.match(uri)) {...
  46. 46. URI matchingsUriMatcher.addURI("com.example.provider", "articles/#", 0);sUriMatcher.addURI("com.example.provider", "articles/today", 1);sUriMatcher.addURI("com.example.provider", "articles/history/*", 2);...public String getType(Uri uri) { switch (sUriMatcher.match(uri)) {...
  47. 47. MIME typesgetType()vnd.android.cursor.dir/vnd.com.example.provider.articlevnd.android.cursor.item/vnd.com.example.provider.articlegetStreamTypes(){ "image/jpeg", "image/png", "image/gif" }
  48. 48. MIME typesgetType()vnd.android.cursor.dir/vnd.com.example.provider.articlevnd.android.cursor.item/vnd.com.example.provider.articlegetStreamTypes(){ "image/jpeg", "image/png", "image/gif" }
  49. 49. QuestionsThank you!
  50. 50. Useful linkshttp://developer.android.com/guide/topics/providers/content-provider-basics.htmlhttp://developer.android.com/guide/topics/providers/content-provider-creating.htmlhttp://developer.android.com/guide/topics/security/permissions.htmlhttp://gdg.org.uahttp://dnipro.gdg.org.ua
  51. 51. About speaker Alexey Ustenko — Android developer Coordniator of GDG Dnipropetrovsk @ustav

×