Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Content Providers

Android Content Providers

Content Providers in general and built in examples

Avatar for P Campbell

P Campbell

October 28, 2014
Tweet

Other Decks in Programming

Transcript

  1. REST • REpresentational Sate Transfer (REST) • Typing a URL

    and getting a web page • Changing the state of a web server by filling a form and submitting it 2
  2. REST • Consists of clients and servers • Clients initiate

    requests • Servers process requests and return appropriate responses • Requests and responses are built around the transfer of representations of resources 3
  3. REST principles • Clients are separated from servers by a

    uniform interface –Clients need not to worry about how data is stored on servers  portable client code –Servers need not to worry about the user interface  improves server scalability and makes servers simpler 4
  4. REST principles • Stateless • Layered system –A client cannot

    ordinarily tell whether it is connected directly to the end server, or to an intermediary along the way –Hence allows for load balancing and shared caches 5
  5. REST principles • Uniform Interface –simplifies and decouples the architecture

    –enables the client and the server sides to evolve independently 6
  6. Android Content Providers • A way of sharing data across

    apps • Abstracting data into service • This idea of content providers makes data sources look like REST-enabled data providers such as web sites • A content provider is a wrapper around data • Content providers supply CRUD methods 7
  7. Android Content Providers • A SQLite database on an Android

    device is an example of a data source that you can encapsulate into a content provider • Android also has several built-in content providers that encapsulate databases 8
  8. Android Content Providers • Content providers play a significant role

    in sharing data between applications • The content providers’ responsibility is to provide an encapsulation mechanism not a data-access mechanism • You’ll need an actual data access mechanism such as SQLite to get to the underlying data sources 9
  9. Android’s Built-in Providers • The top-level items are SQLite databases

    encapsulated as providers –Contacts –Mediastore –Calendar • These SQLite databases typically have an extension of .db 10
  10. Android’s Built-in Providers • Calendars • Events • Attendees •

    Reminders 13 http://developer.android.com/guide/topics/providers/calendar-provider.html
  11. Android’s Built-in Providers • MediaStore –Audio • Albums • Artists

    • Genres • Playlists –Images • Thumbnails –Video New in API 19 Storage Access Framework 14 http://developer.android.com/guide/topics/providers/document-provider.html
  12. Content Providers • A system to access shared data •

    Similar to a REST web service • To each Content Provider, one or more URIs are assigned in the form: 15 content://<authority>/path
  13. To build a Content Provider • Define the DB •

    Create a class that extends android.content.ContentProvider • Implement query(), insert(), update(), delete() • Register the ContentProvider authority in the manifest • Expose the URIs and schema 16
  14. To use a Content Provider (built-in or other) • Discover

    the exposed data (schema, URIs) • Set the appropriate permission(s) in the manifest, for built in:  http://developer.android.com/reference/android/Manifest.permission.html • Get the URI(s) –Usually these are declared as a public constant inside the content provider class • Make a query(or other CRUD), maybe adding some where clauses –The query returns a Cursor • Navigate the Cursor 17
  15. Example: use Contacts, built-in Content Provider • You need to

    request permission via the manifest • Query the contacts content provider • Contacts information is shared among applications 18 <uses-permission android:name="android.permission.READ_CONTACTS"/>
  16. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); <// read through

    all contacts Cursor cursor = getContentResolver().query(ContactsContract.Contacts .CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String contactName = cursor.getString(cursor.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); Log.d(TAG, contactName); } // while cursor not at end cursor.close(); } // onCreate() Contacts: code 19 http://developer.android.com/guide/topics/providers/contacts-provider.html