Your SlideShare is downloading. ×
Content providers in Android
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Content providers in Android

1,928
views

Published on

Published in: Education

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,928
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
105
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Content Providers in Android
  • 2. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 3. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 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. 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. 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. 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. 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. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 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. Activity to Content Provider accessActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  • 12. ActivityActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  • 13. Performing request Content Provider Query InsertContentResolver URI Update Delete
  • 14. URIcontent://com.example.provider/articles Scheme Authority Path
  • 15. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 16. Constructing querySELECT _id, title, content, dateFROM articlesWHERE date >= 1352470000ORDER BY date ASC
  • 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. 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. 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. 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. 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. 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. 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. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 25. Activity & blocking queriesActivity Cursor ContentResolver Content Provider CursorAdapter ListView
  • 26. Activity & non blocking queriesActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  • 27. Activity & LoaderActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  • 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. 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. 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. 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. 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. Activity & AsyncQueryHandlerActivityCursorLoader Cursor ContentResolver Content Provider AsyncQueryHandler CursorAdapter ListView
  • 34. Activity & AsyncQueryHandlerprivate AsyncQueryHandler mHandler;...mHandler = new MyAsyncQueryHandler(getContentResolver());mHandler.startQuery( 0, // token null, // cookie MyContentProvider.ARTICLES_CONTENT_URI, mProjection, mSelection, mSelectionArgs, mSortOrder);
  • 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. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 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. 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. 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. ● Overall structure● Interaction with Content Provider● Constructing query● Retreiving cursor asyncronuously● Provider permissions● Creating Content Provider● Questions
  • 41. Creating content providerpublic class MyContentProvider extends ContentProvider {...onCreate()query()insert()update()delete()getType()
  • 42. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  • 43. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  • 44. URI matchingcontent://com.example.provider/articlescontent://com.example.provider/articles/*content://com.example.provider/articles/#
  • 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. 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. 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. 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. QuestionsThank you!
  • 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. About speaker Alexey Ustenko — Android developer Coordniator of GDG Dnipropetrovsk @ustav