You are on page 1of 11

ITE 2152

Introduction to Mobile
Application Development
Week 10

M P C Sandaru – Software Engineer


Content Providers
• Content Providers used to share data across
packages (data store)
• A content provider behaves very much like a
database—you can query it, edit its content,
and add or delete content.
• A content provider can use differ-ent ways to
store its data. The data can be stored in a
database, in files, or even over a network.
Content Providers (cont…)
• Android ships with many useful content providers
– Browser—Stores data such as browser bookmarks,
browser history, and so on
– CallLog—Stores data such as missed calls, call details,
and so on
– Contacts – stores contact details
– MediaStore—Stores media files such as audio, video,
and images
– Settings—Stores the device’s settings and preferences
Query a Content Provider
• To query a content provider, you specify the
query string in the form of a Uniform Resource
Identifier (URI), with an optional specifier for a
particular row.
– <standard_prefix>://<authority>/<data_path>/<id>
Various parts of the URI
• The standard prefix for content providers is
always
– content://.
• The authority specifies the name of the content
provider. An example would be contacts for the
built-in Contacts content provider.
– For third-party content providers, this could be the
fully qualified name, such as com.wrox.provider or
com.jfdimarzio.provider.
Various parts of the URI (cont…)
• The data path specifies the kind of data
requested. For example, if you are getting all the
contacts from the Contacts content provider then
the data path would be people, and the URI
would look like this:
– content://contacts/people.
• The id specifies the specific record requested. For
example, if you are looking for contact number 2
in the Contacts content provider, the URI would
look like this:
– content://contacts/people/2.
Example
Android CursorLoader
• There are three key benefits of using a CursorLoader:
– The query is handled on a background thread for you
(courtesy of being built on AsyncTaskLoader) so that large
data queries do not block the UI.
– CursorLoader is auto-updating. In addition to performing
the initial query, CursorLoader also registers a
ContentObserver with the dataset you requested and calls
forceLoad() on itself when the data set changes.
– This results in getting async callbacks anytime the data
changes in order to update the view.

• Example : http://www.theappguruz.com/blog/use-
android-cursorloader-example
Create your own content providers
• To fulfill this task we have to extend the abstract
ContentProvider class and override the various methods
define within it.
• We can create a class which extends the ContentProvider
base class.
• The various methods to override in this class are as follows:
– getType()—Returns the MIME type of the data at the given URI.
– onCreate()—Called when the provider is started
– query()—Receives a request from a client. The result is returned
as a Cursor object.
– insert()—Inserts a new record into the content provider.
– delete()—Deletes an existing record from the content provider.
– update()—Updates an existing record from the content provider.
Summary
TOPIC KEY CONCEPTS
Retrieving a managed cursor Use the CursorLoader class.
Two ways to specify a query Use either a query URI or a predefined query string
for a content provider constant.
Retrieving the value of a Use the getColumnIndex() method
column in a content provider
Querying URI for accessing a ContactsContract.Contacts.CONTENT_URI
contact’s name
Querying URI for accessing a ContactsContract.CommonDataKinds.Phone.CONTENT_URI
contact’s phone number
Creating your own content Create a class and extend the ContentProvider class.
provider
Thank You!

You might also like