How does Android implement data sharing between applications?

Source: Internet
Author: User
Tags home screen

It is not important for an application to expose its own data to the outside world, to see how it is stored, to use a database or to use a file, or to access it online. It is important for the outside world to deal with this set of standards and a unified interface with the data in this program, such as adding (insert), deleting (delete), querying (query), modifying (update), and of course requiring certain permissions.

How do I expose an application's data? Android provides ContentProvider, a program can expose its own data completely by implementing an abstract interface with content provider, and content providers exposes data in a similar way to tables in the database. Content providers stores and retrieves data that allows all applications to access, which is the only way to share data between applications. To make your application's data public, there are 2 ways to create your own content provider or add your data to an existing content provider if you have the same data type and have write content Provider permissions.

How do you get data exposed by other applications through a set of standard and unified interfaces? Android provides Contentresolver, and outside programs can access the data provided by ContentProvider via the Contentresolver interface.

The current article mainly explains how to get data shared by other applications, such as getting information from the Android phone book. What is a URI?

Before learning how to get contentresolver, there is a noun that must be understood: the URI. A URI is the definition of a network resource, giving it a broader meaning in Android, first looking at an example, as follows:

Divide it into a,b,c,d 4 parts:

A: Standard prefix, used to indicate that a content provider control the data, can not be changed;

B:uri identification, which defines which content provider provides this data. For a third-party application, to ensure the uniqueness of the URI identity, it must be a full, lowercase class name. This identification 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 what type of data is currently required, may not include the path in the URI, or it may include multiple;

D: If the URI contains, represents the ID of the record that needs to be fetched, and if there is no ID, returns all;

Because URIs are usually long and sometimes error-prone, it's hard to understand. So, there are some helper classes defined in Android, and some constants are defined in place of these long strings, such as: people.content_uricontentresolver introduction

After reading these introductions, you must understand that Contentresolver is a URI to query the data provided in ContentProvider. In addition to URIs, you must know the name of the data segment you want to get, and the data type of the data segment. If you need to get a specific record, you must know the ID of the current record, which is the D part of the URI.

As mentioned earlier, the content providers is exposed to data in a similar way as a table in a database, so Contentresolver will also use a database-like operation to retrieve data from the content providers. Now briefly introduce the main interface of Contentresolver, as follows:

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, return ing a Cursor over the result set.

Final int

Update (URI Uri, contentvalues values, String where, string[] selectionargs) update row (s) in a content Uri.

See here, do you feel the same as the operation of the database? This is the case, please refer to the instructions in the Android SQLite parsing article for detailed explanation.

One last question: How do I get contentresolver? Call Getcontentresolver (), for example: contentresolver CR = Getcontentresolver (); Making Contentresolver Examples

The above is a complete description of how to obtain, use Contentresolver, start eclipes, make a complete example as follows:

Open the Showcontent.java and modify the following:

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, null,null, 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 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 screen, open contacts Select Contacts tab, add 2 contact information. Return to Home, select Moandroid.showcontact Run, just add 2 contact information will be displayed in the interface

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.