android-data Storage (Content Provider, calling system contacts ContentProvider implementing queries and adding contacts)

Source: Internet
Author: User

1. ReviewThe previous study of the Android file storage, including storage on the SD card;
2. Focus(1) Learn about one of the four components Content Provider(2) realize the contact person in the inquiry communication record(3) To implement new contacts to the communication record.
3. Introduction(1) ContentProvider is more complex than other methods, but its function is revolutionary in other ways. (2) It enables data manipulation across applications. (3) using the contentresolver object Delete, UPDATE, insert, query and other methods to operate ContentProvider the object, (4) Let the ContentProvider object's method go to the data operation.

4. Example-- call the communication record to implement the contact's query and add--4.1

4.2 Adding permissionsAdd in the Androidmainfest.xml file, read the contacts and add the permissions of the contact person
    <uses-permission android:name= "Android.permission.READ_CONTACTS"/>    <uses-permission android:name= " Android.permission.WRITE_CONTACTS "/>


4.3 query Contact Implementation
/* * Query contact */private void SelectContact () {/** * use Contentresolver to implement this and ContentProvider parameters exactly the same * * The URI of the contact provides: Contac TsContract.Contacts.CONTENT_URI *///Query Contact Contentresolver resolver = getcontentresolver (); cursor cursor = resolver.query (Contacts.content_uri, new string[] {contacts._id,contacts.display_name}, NULL, NULL, NULL); String str = "Contact: \ n"; if (cursor! = NULL) {while (Cursor.movetonext ()) {String id=cursor.getstring (cursor.getcolumnindex (contacts._id));    String name=cursor.getstring (Cursor.getcolumnindex (contacts.display_name));     str+= "id=" +id+ "name=" +name; Querying the phone via ID Cursor c=resolver.query (phone.content_uri,new string[]{phone.number,phone.type},phone.contact_id+ "=?",   New String[]{id},null);   if (C!=null) {while (C.movetonext ()) {String num=c.getstring (C.getcolumnindex (Phone.number));   String type=c.getstring (C.getcolumnindex (Phone.type));   str+= "num=" +num+ "type=" +type;   } c.close (); }str+= "\ n";} Cursor.close ();} Tv_show.settext (str);}


4.4 Implementing Add a contact
/** * First performs a null-value insertion to Rawcontacts.content_uri to get the Rawcontactid returned by the system * This is the data that is inserted later, only the null value is inserted to make the inserted contact visible in the Address book.        */public void Testinsert () {contentvalues values = new Contentvalues (); First, a null insertion is performed to Rawcontacts.content_uri to get the Rawcontactid Uri returned by the system Rawcontacturi = This.getcontentresolver (). Insert (R        Awcontacts.content_uri, values);                Long Rawcontactid = Contenturis.parseid (Rawcontacturi);        Go to the data table into the name information values.clear ();        Values.put (data.raw_contact_id, Rawcontactid);        Values.put (Data.mimetype, Structuredname.content_item_type);        Values.put (Structuredname.given_name, "I am a Test contact");                This.getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, values);        To the data table into the telephone information values.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (Data.mimetype, Phone.content_item_type); ValUes.put (Phone.number, "10000000010001");        Values.put (Phone.type, phone.type_mobile);        This.getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, values);        Enter the data form into the email values.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (Data.mimetype, Email.content_item_type);        Values.put (Email.data, "[email protected]");        Values.put (Email.type, email.type_work);    This.getcontentresolver (). Insert (Android.provider.ContactsContract.Data.CONTENT_URI, values); }


5. Summary(1) Do not forget to add permission when dealing with correspondence record;(2)Content Provider to achieve data manipulation across applications;
6.demo free points Download http://download.csdn.net/detail/lablenet/9055113

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

android-data Storage (Content Provider, calling system contacts ContentProvider implementing queries and adding contacts)

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.