15
Content Providers

Content Providers - WordPress.com · 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

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Content Providers - WordPress.com · 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

Content Providers

Page 2: Content Providers - WordPress.com · 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

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.

Page 3: Content Providers - WordPress.com · 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

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.

Page 4: Content Providers - WordPress.com · 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

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

Page 5: Content Providers - WordPress.com · 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

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>

Page 6: Content Providers - WordPress.com · 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

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.

Page 7: Content Providers - WordPress.com · 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

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.

Page 8: Content Providers - WordPress.com · 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

Example Query Strings

Page 9: Content Providers - WordPress.com · 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

Using Contacts Content Provider

Page 10: Content Providers - WordPress.com · 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

Using Contacts Content Provider

Page 11: Content Providers - WordPress.com · 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

Using Contacts Content Provider

Page 12: Content Providers - WordPress.com · 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

Using Contacts Content Provider

Page 13: Content Providers - WordPress.com · 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

Using Contacts Content Provider

Page 14: Content Providers - WordPress.com · 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

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;

Page 15: Content Providers - WordPress.com · 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

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”);