Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
Content Providers
Content Providers
Different Ways of persisting Data
Shared Preferences
Files
SQLite Database
Database approach is the recommended way to save structured and complex data.
Sharing data using Database approach is a challenge because the database is accessible to only the package that created it.
Sharing Data In Android
Content provider is the recommended way to share data across packages.
Think of a content provider as a data store.
Packages can access the data stored in content provider using a consistent programming interface.
A content provider behaves very much like a database — you can query it, edit its content, as well as add or delete content.
Unlike a database, a content provider can use different ways to store its data.
The data can be stored in a database, in files, or even over a network.
Sharing Data In Android
Android ships with many useful content providers, including
the following:
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
Sharing Data In Android
Creating user defined content providers.
To query a content provider, you specify the query string in
the form of a URI, with an optional specifier for a particular
row. The format of the query URI is as follows:
<standard_prefix>://<authority>/<data_path>/<id>
Sharing Data In Android
The various parts of the URI are as follows:
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
net.learn2develop.provider.
Sharing Data In Android
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 Query Strings
Using Contacts Content Provider
Using Contacts Content Provider
Using Contacts Content Provider
Using Contacts Content Provider
Using Contacts Content Provider
Predefined Query String Constants
Apart from query URI, you can use a list of predefined query
string constants in Android to specify the URI for the different
data types.
For example, besides using the query
content://contacts/people,
you can rewrite the statement
Uri allContacts = Uri.parse(“content://contacts/people”);
Using one of the predefi ned constants in Android, as follows:
Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
Predefined Query String Constants
Some examples of predefi ned query string constants are as follows:
Browser.BOOKMARKS_URI
Browser.SEARCHES_URI
CallLog.CONTENT_URI
MediaStore.Images.Media.INTERNAL_CONTENT_URI
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
Settings.CONTENT_URI
If you want to retrieve the first contact, specify the ID of that contact, like this:
Uri allContacts = Uri.parse(“content://contacts/people/1”);