MobAppDev (Fall 2013): Content Providers
Post on 11-May-2015
515 Views
Preview:
Transcript
Mobile Application Development
Content Providers
Vladimir Kulyukin
www.vkedco.blogspot.comwww.vkedco.blogspot.com
Outline
● Representational State Transfers● Content Providers
Authority Registration Table Data Model ContentProvider Resolution Content URIs
Representational State Transfers
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
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
Content Providers
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
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
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
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
Authority Registration
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="org.vkedco.provider.science">
</provider>
Authority Registration
<provider
android:name=".ScienceContentProvider"
android:authorities="org.vkedco.provider.science">
</provider>
● In the above registration, the provider serves the URIs with the prefix
content://org.vkedco.provider.science
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
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
ContentProvider Resolution
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
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
Wrapping SQLite DBs with ContentProviders
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.
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
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 (BookContentProvider.java) implements and services these URIs after it is registered on Android
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
Implementing ContentProvider
● The content provider is implemented in BookContentProvider.java
● It publishes all URIs and MIMEs and matching codes
● It also implements the query method that returns Cursor objects
Implementing 1st Client
source code is here
Implementing 2nd Client
References ● http://developer.android.com/guide/topics/providers/content-providers.html● http://tools.ietf.org/html/rfc2046● http://www.iana.org/assignments/media-types● http://vkedco.blogspot.com/2012/11/mobappdev-contentproviders-as-web-sites.html
top related