Android Provider Use parsing

Source: Internet
Author: User

First, the basic concept

One of Android's four main components, content provider, is to achieve (cross-application) data sharing across applications.

In this case, the process communication problem, naturally used in Android is binder, but because the content provider provides a large amount of data is generally not able to directly transfer.

So here is a method called anonymous shared memory for data transfer, in different processes only need to pass a file descriptor.

There is a more intuitive understanding of the content provider:

ContentProvider provides a mechanism for sharing data before an application.
1) provides a unified interface for storing and retrieving data.
2) encapsulate the data without worrying about the details of the data store.
3) Android provides default contentprovider for some common data (including audio, video, images, contacts, etc.).

Ii. Definition of content provider

If you need to use content provider first you need to make a statement in Androidmanifest

<provider android:authorities="List"  //The unique ID of the providerandroid:directbootaware=["true"|"false"] android:enabled=["true"|"false"]//can be instantiated by the systemandroid:exported=["true"|"false"]//whether the provider can be used by other applicationsandroid:granturipermissions=["true"|"false"] Android:icon="drawable Resource"Android:initorder="integer"Android:label="string Resource"android:multiprocess=["true"|"false"] Android:name="string"android:permission="string"android:process="string"android:readpermission="string" //Read Permissionsandroid:syncable=["true"|"false"] Android:writepermission="string">//Write Permissions    . . .</provider>

Note that the most important thing in provider's properties is authorities, which is uniquely capable of identifying a provider,contentprovider through the address in the URI to find the corresponding provider, Where the URI contains the value of the authorities attribute in the provider you need to look for.
The defined provider is finally packaged and compiled into the packagemanagerservice.

When defining a provider in Java code, it is necessary to inherit its parent class ContentProvider, and to implement additions and deletions method.

 Public classXxxxprovider extends ContentProvider {@Override PublicBoolean onCreate () {//TODO auto-generated Method Stub        return false; } @Override PublicCursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) { //TODO auto-generated Method Stub        return NULL; } @Override PublicString getType (Uri uri) {//TODO auto-generated Method Stub        return NULL; } @Override Publicuri insert (URI uri, contentvalues values) {//TODO auto-generated Method Stub        return NULL; } @Override Public intDelete (Uri Uri, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return 0; } @Override Public intupdate (URI Uri, contentvalues values, String selection, string[] selectionargs) {//TODO auto-generated Method Stub        return 0; }}

Third, the use of ContentProvider

In the activity if you want to use provider to achieve additions and deletions, the first need to obtain contentprovider, the general process for the context to obtain contentresolver, Then through the Contentresolver to Activitymanagerservice query the corresponding provider, if not then go to Packagemanagerservice to find:

1) First, each context class will contain a Contentresolver sub-object applicationcontentresolver.
2) Get the database data of CP by calling Applicationcontentresolver's Main method query.
3) The procedure called Contentresolver first calls the core method of the Acquireprovider (). The Acquireprovider () method is an abstract method, and its implementation is implemented by subclasses.
4) through the Acquireprovider () method of subclasses to realize that the main implementation is to be done by the Activitythread class.
5) The Activitythread class will make a judgment that if a local instance of the CP object is saved, the object instance will be returned directly, and if not saved, the AMS object will be accessed to find an instance of the CP object that gets an object. When the object instance is found, it is saved locally for quick access later.
6) If not found in AMs, will continue to go deep into the PMS to find out all the provider.
7) After acquiring the CP object instance, it is returned through the layer layer, and finally the query method of the CP object is called to get the corresponding data.

First, read and write permission declarations are required in the manifest of the application, and the definition of this declaration is the same as the required permission attribute value for read and write in the previous provider definition:

<use-permission android:name="android.permission.READ_SMS"/><use-permission Android:name="android.permission.WRITE_SMS"/>

When you get an action method in the activity that Contentresolver calls, you need to pass in the corresponding parameter:

Contentresolver.query (Uri Uri, string[] projection,string selection, string[] selectionargs,string);

URI: The corresponding URI is passed in to find the corresponding provider, the same as the authorities value defined in manifest provider
Projection: Select the object property value you want to return, and sometimes you don't need to return the value of the object all.

Selection/selectionargs: Query criteria

ORDER BY: How to sort objects returned

Similar to other delete, insert, and update operations. The main thing is to pass in the correct URI to find the corresponding provider.

Android Provider Use parsing

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.