MobAppDev (Fall 2013): Content Providers

Uploaded on


More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Mobile Application Development Content Providers Vladimir Kulyukin
  • 2. Outline ● ● Representational State Transfers Content Providers     Authority Registration Table Data Model ContentProvider Resolution Content URIs
  • 3. Representational State Transfers
  • 4. Representational State Transfer: REST ● ● ● REST is a style of software architecture for distributed hypermedia systems such as WWW REST architecture consists of clients and servers Requests from clients and responses from servers are built around transfers of representations of resources
  • 5. Representational State Transfer: REST ● ● ● A resource is any coherent and meaningful concept that may addressed A representation is a sequence of bits that captures the state of a resource Architectures that comply with REST standards are called RESTful
  • 6. Content Providers
  • 7. What is a Content Provider? ● ● ● A Content Provider is a wrapper around data source A SQLite database is an example of a data source that can be exposed as a content provider A Content Provider is a REST-like abstraction
  • 8. When to Use Content Providers ● ● Only if you need to share some data among different applications For internal data access, you can use:  Preferences  Files  SQLite  Networks
  • 9. Android's Built-in Content Providers ● ● ● ● Several built-in providers are Browser, CallLog, Contacts, MediaStore, Settings These top-level providers are SQLite databases encapsulated as content providers Some of these databases have multiple tables: for example, Contacts has People, Phones, Photos, and Groups tables List of providers varies for different versions of Android
  • 10. Content Providers as Web Sites ● ● ● ● Each content provider registers itself on a device as a web site The registration is done with a string called authority Authority is similar to a domain name Authority string is the basis of a set of URIs the content provider offers
  • 11. Authority Registration
  • 12. Authority Registration ● ● Authority must be registered in AndroidManifest.xml This is how ContentProviders become known (registered) with the Android ecosystem <provider android:name=".ScienceContentProvider" android:authorities=""> </provider>
  • 13. Authority Registration <provider android:name=".ScienceContentProvider" android:authorities=""> </provider> ● In the above registration, the provider serves the URIs with the prefix content://
  • 14. Table Data Model ● ● ● ● Content providers structure data like relational database tables Rows can be viewed as records or instances of some data type (e.g. a Mathematician or a Book) Columns represent member variables and their values in corresponding instances It is irrelevant to outside clients whether the actual data reside in a relation database
  • 15. Content Resolvers ● ● ● When a component must access the data in a ContentProvider it must use the ContentResolver object in the Context of its application The ContentResolver object allows the component to communicate with the ContentProvider as a client The ContentResolver is the intermediary between the client component and the ContentProvider
  • 16. ContentProvider Resolution
  • 17. Content Resolvers ● ● ● ContentResolver objects offers clients the same methods as the corresponding ContentProvider object ContentResolver objects provide the create, retrieve, update and delete (aka CRUD) methods for persistent data storage Once paired, ContentProvider and ContentResolver objects handle inter-process communication
  • 18. ContentProvider Resolution ● ● ● In the ContentProvider model, data are manipulated only through URIs URIs are parsed by ContentResolvers Upon parsing the URI, a given ContentResolver object compares the authority extracted from the URI to the table of ContentProviders registered in the Android ecosystem
  • 19. Wrapping SQLite DBs with ContentProviders
  • 20. Problem Build a content provider that wraps around an SQLite database with images of poets (Rumi, Hafiz) and book covers (Essential Rumi, Illuminated Rumi, Year with Rumi, Year with Hafiz, & The Gift). Build two clients for the content provider: the first one retrieves images of poets and book covers; the second one retrieves images of book covers given ISBNs.
  • 21. Implementation Steps ● ● ● ● ● Design and populate SQLite databases (we have accomplished these steps in the previous projects/presentations) Implement a content provider Register the content provider on the Android ecosystem Implement the clients Test everything
  • 22. Content URIs ● Root URI: content://org.vkedco.mobappdev.content_providers.books ● Retrieval of book title records will use the following URIs ● Examples: – – ● content://org.vkedco.mobappdev.content_providers.books/id content://org.vkedco.mobappdev.content_provides.books/id/# Query URIs for clients: – – ● content://org.vkedco.mobappdev.content_providers.books/book_title/query?title=essential_rumi content://org.vkedco.mobappdev.content_providers.books/book_title/query? title=essential_rumi&author=rumi Content Provider ( implements and services these URIs after it is registered on Android
  • 23. Content Provider Registration <application> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name=".BookContentProvider_00001" android:authorities="org.vkedco.mobappdev.content_providers.books" android:process=":org.vkedco.mobappdev.content_providers.books"> </provider> </application> source code is here
  • 24. Implementing ContentProvider ● ● ● The content provider is implemented in It publishes all URIs and MIMEs and matching codes It also implements the query method that returns Cursor objects
  • 25. st Implementing 1 Client source code is here
  • 26. Implementing 2 nd Client
  • 27. References ● ● ● ●