In the previous section, we covered several methods for data persistence: preferences, files, and databases. It is recommended that you use Sqlitedatabase when you are saving complex data structures. However, sharing data is a challenge because the database is only available for packages that create it.
In this section, you'll introduce Android-specific ways to share data: using ContentProvider. It also describes how to use the built-in ContentProvider and create your own contentprovider to share data among multiple packages.
In Android, the recommended way to share data is to use ContentProvider. You can think of ContentProvider as a data store. How it stores data is irrelevant to the program that uses it. The important thing is how to get the data through the coding interface.
ContentProvider behaves very much like a database, you can perform additions and deletions to check the operation.
However, unlike a database, it can store data in different ways. Data can be stored in a database, in a file, or even on a network.
Android offers a number of useful contentprovider, as follows:
Browser stores browser bookmarks, browsing history, and more.
Calllog storage missed calls, call records, and so on.
Contacts Store contact details.
Mediastore stores media files, such as audio, video, pictures, etc.
Some configuration information for the Settings storage device.
In addition to these built-in contentprovider, you can create your own contentprovider.
To query for a contentprovider, you need to specify a query string in the form of a URI, specifying a row. The URI query form is as follows:
<standard_prefix>://<authority>/<data_path>/<id>
The components of the URI are as follows:
The prefix of the ContentProvider standard is content://
Authority specified the name of the ContentProvider. Contacts is the name of the built-in contacts ContentProvider. For third party ContentProvider, this authority may be a completed named name, such as Net.manoel.provider or Net.horsttnann.provider.
DATA_PATH Specifies the category of the request data. For example, if you are getting all the contacts in Contacts ContentProvider, then data_path should be people, that is, the URI would be: content://contacts/people
The ID specifies the requested record. For example, if you are looking for a contact number 2nd in Contacts ContentProvider, then the URI will be: CONTENT://CONTACTS/PEOPLE/2
Here are some examples of queries: