ArticleDirectory
- What is uri?
- Contentresolver description
- Create a contentresolver instance
- Summary
How Android implements applicationsProgramShared data? An application can completely expose its own data, which is invisible to the outside world and does not need to see the data exposed by this application.
It doesn't matter how it is stored, whether it is used for databases, files, or the Internet, it is important that the outside world can use this set of standards and unified interfaces to communicate with the data in this program.
For example, adding (insert), deleting (delete), querying (query), and modifying (update) require certain permissions.
How to expose application data? Android provides contentprovider. A program can implement a content
The abstract interface of provider completely exposes its own data, and content providers exposes data in a way similar to a table in a database. Content
Providers stores and retrieves data that can be accessed by all applications. This is the only way to share data between applications. To make the application data public, you can use either of the following methods:
Method: Create a content provider of your own or add your data to an existing content
Provider, provided that you have the same data type and the permission to write to the content provider.
How can I obtain data exposed by other applications through a set of standard and unified interfaces? Android provides contentresolver, and external programs can access the data provided by contentprovider through the contentresolver interface.
This article describes how to obtain data shared by other applications, such as information in the phone book of Android phones.
What is uri?
Before learning how to obtain contentresolver, you must know the following terms: URI. Uri is the definition of network resources. It is given a broader meaning in Android. Let's take a look at the following example:
Divide it into four parts: A, B, C, and D:
A: The standard prefix. It cannot be changed if a content provider controls the data;
B: URI identifier, which defines which content provider provides the data. For third-party applications, to ensure the uniqueness of the URI identifier, it must be a complete, lowercase class name. This identifier is described in the authorities attribute of the <provider> element:
<Provider name = ". transportationprovider" authorities = "com. example. transportationprovider"...>
C: path. content provider uses these paths to determine the type of data to be generated. The URI may not include paths or multiple data types;
D: If the URI contains the ID of the record to be retrieved; if there is no ID, all records are returned;
Because the URI is usually long and sometimes error-prone, it is hard to understand. Therefore, some helper classes are defined in Android and some constants are defined to replace these long strings, such as: people. content_uri
Contentresolver description
After reading these introductions, you will surely understand that contentresolver uses URI to query the data provided in contentprovider. Except URI
You must also know the name of the data segment to be obtained and the Data Type of the Data Segment. If you need to obtain a specific record, you must know the ID of the current record, that is, the D part of the URI.
As mentioned above, content providers exposes data in a way similar to tables in a database, so contentresolver will also use database-like operations to obtain data from content providers. The following describes the main interfaces of contentresolver:
Return Value |
Function Declaration |
Final URI |
Insert(Uri url, contentvalues values) inserts a row into a table at the given URL. |
Final int |
Delete(Uri url, string where, string [] selectionargs) deletes row (s) specified by a content Uri. |
Final cursor |
Query(URI Uri, string [] projection, string selection, string [] selectionargs, String sortorder) query the given Uri, returning a cursor over Result set. |
Final int |
Update(URI Uri, contentvalues values, string where, string [] selectionargs) Update row (s) in a content Uri. |
Do you think the operation is basically the same as that of the database? This is the case. For detailed parsing, refer to the description in Android SQLite parsing, not here.
Last question: how to obtain contentresolver? Call getcontentresolver (), for example, contentresolver Cr = getcontentresolver ();
Create a contentresolver instance
The above describes how to obtain and use contentresolver, start es, and create a complete instance as follows:
Open showcontent. Java and modify it as follows:
PackageMoandroid. showcontact;
ImportAndroid. App. listactivity;
ImportAndroid. database. cursor;
ImportAndroid. OS. Bundle;
ImportAndroid. provider. Contacts. Phones;
ImportAndroid. widget. listadapter;
ImportAndroid. widget. simplecursoradapter;
Public ClassShowcontactExtendsListactivity {
Protected VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Cursor c = getcontentresolver (). Query (phones.Content_uri,Null,Null,Null,Null);
Startmanagingcursor (C );
Listadapter adapter =NewSimplecursoradapter (This,
Android. R. layout.Simple_list_item_2, C,
NewString [] {phones.Name, Phones.Number},
New Int[] {Android. R. Id.Text1, Android. R. Id.Text2});
Setlistadapter (adapter );
}
}
Add the following license before the <Application> element in androidmanifest. xml:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
Finally, run the program. After the simulator is started, click menu to return to the home interface, open the contacts select contacts tab, and add two contacts. Return to home and select moandroid. showcontact to run. The newly added two contacts are displayed on the interface as follows:
Summary
The use of contentresolver greatly facilitates the sharing of data between applications, and how to completely expose the data of applications to their applications, in the next article, data sharing between Android applications -- contentprovider.