About Android Content Provider

Source: Internet
Author: User

Content provider is one of the four components of Android, and is the same as activity and service, and needs to be registered before use;

There are a lot of applications in the Android system, when sharing data between different applications, you can use the content provider, because it provides a unified interface for storing and reading data;

(1) Many of the data built into the Android system are using Content Provider, which are then used by developers to invoke, such as audio, video, images, contacts, etc.;

(2) When a program needs to expose its own data to other programs for use, the program can be implemented by providing content provider, while other programs can manipulate content provider exposed data through contentresolver;

(3) The application open its own data through ContentProvider, the application does not need to start, other applications can operate open data, including adding and removing changes to the operation;

(4) When an app inherits the ContentProvider class and overrides the method used to provide data and store data, it can share its data with other apps. Although the use of other methods can also share data, but the way data access will vary depending on how the data is stored, such as: the use of file-based data sharing, the need for file operations to read and write data, the use of sharedpreferences shared data, You need to read and write data using the Sharedpreferences API. The benefit of using ContentProvider to share data is to unify the way data is accessed.

Steps for using the ContentProvider class

1, custom MyProvider extends ContentProvider sub-class, in this class to rewrite the operation for adding or removing changes;

2. Register the class in the Androidmanifest.xml;

3, set the URI address of the shared data;

4, in other classes or other items call Contentresolver object in the Add and remove methods, according to the address provided by the URI to operate the data;

For example: Get the phone address of the system;

The code is very simple, because we do not customize our content provider class, so we only use Contentresolver to manipulate the data on the line;

 Packagecom.xiaozhang.contentprovidertest;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.provider.ContactsContract;ImportAndroid.provider.ContactsContract.PhoneLookup;ImportAndroid.database.Cursor;ImportAndroid.graphics.Color;ImportAndroid.widget.TextView;ImportAndroid.content.ContentResolver; Public classMainactivityextendsActivity { Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); TextView TextView=NewTextView ( This); String String=Getquerydata ();        Textview.settextcolor (Color.gray);        Textview.settext (string); Textview.settextsize (20.0f);    Setcontentview (TextView); }     Publicstring Getquerydata () {string string= ""; intID = 1; //Get Contentresolver ObjectContentresolver CR =Getcontentresolver (); //get the cursor starting in the phone bookcursor cursor = cr.query (ContactsContract.Contacts.CONTENT_URI,NULL,                NULL,NULL,NULL); //Move Down         while(Cursor.movetonext ()) {//get name of contact person            intNamefieldcolumnindex =cursor. Getcolumnindex (phonelookup.display_name); String Contact=cursor.getstring (Namefieldcolumnindex); //Get Phone numberString ContactId =cursor.getstring (cursor. Getcolumnindex (contactscontract.contacts._id)); Cursor Phone=cr.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=" + ContactId,NULL,NULL);  while(Phone.movetonext ()) {String PhoneNumber=phone. getString (Getcolumnindex (Contactscontract.commond                AtaKinds.Phone.NUMBER)); String+ = (id + "-----" + Contact + "-----" + phonenumber + "\ n"); ID++;        }} cursor.close (); returnstring; }}

Then add the permissions in the Androidmanifest.xml;

<android:name= "Android.permission.READ_CONTACTS"/>

About Android Content Provider

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.