How does Android achieve data sharing between applications?

Source: Internet
Author: User

An application can completely expose its own data, which is invisible to the outside world. It does not need to see how the data exposed by the application is stored, or whether it uses databases or files, or through the Internet, all these are not important. What is important is that the outside world can use this set of standards and unified interfaces to deal with the data in this program, for example: add (insert) delete, query, and update require certain permissions.

How to expose application data? Android provides ContentProvider. A program can completely expose its data by implementing an abstract interface of the Content provider, and Content providers exposes data in a way similar to a table in a database. Content providers stores and retrieves data, which can be accessed by all applications. This is the only way to share data between applications. To make the data of an application public, you can create a Content provider of your own or add your data to an existing Content provider, the premise is that you have the same data type and the permission to write 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 ID is in <Provider> the authorities attribute of the element is described as follows:

<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. For example: People. CONTENT_URIContentResolver Introduction

After reading these introductions, you will surely understand that ContentResolver uses URI to query the data provided in ContentProvider. In addition to 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 the 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:

Package moandroid. showcontact;

Import android. app. ListActivity;

Import android. database. Cursor;

Import android. OS. Bundle;

Import android. provider. Contacts. Phones;

Import android. widget. ListAdapter;

Import android. widget. SimpleCursorAdapter;

Public class showcontact extends ListActivity {

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

Cursor c = getContentResolver (). query (Phones. CONTENT_URI, null );

StartManagingCursor (c );

ListAdapter adapter = new SimpleCursorAdapter (this,

Android. R. layout. simple_list_item_2, c,

New String [] {Phones. NAME, Phones. NUMBER },

New int [] {android. R. id. text1, android. R. id. text2 });

SetListAdapter (adapter );

}

}

Then, in AndroidManifest. XML, <application>Add the following license before the element:

<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 contacts are displayed on the page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.