contentprovider-Curd of the contact person

Source: Internet
Author: User

1. Search for contacts

Returns a arraylist

//get a Cursor object by managing the URI of a contactCursor cursor= getcontentresolver (). Query (ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL); ArrayListNewArraylist();  while(Cursor.movetonext ()) {String contactId=cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id)); String ContactName=cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME)). Trim (); String Contactphone=NULL; //get a Cursor object by managing the URI of the contact phoneCursor cursorforphone=getcontentresolver (). Query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,NULL, contactscontract.commondatakinds.phone.contact_id+ "=" +contactid,NULL,NULL); if(Cursorforphone.movetofirst ()) {Contactphone=cursorforphone.getstring (Cursorforphone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.NUMBER)). ReplaceAll ("", "" ");            } cursorforphone.close (); //Delete Invalid contacts            if(contactphone==NULL){                intId=getcontentresolver (). Delete (ContactsContract.RawContacts.CONTENT_URI, contactscontract.contacts._id+ "=" + ContactId,NULL); Toast.maketext (contactlist. This, "Invalid contact" +contactname+ "emptied" +ID, Toast.length_short). Show (); Continue; } HashMap<string, string> contact=NewHashmap<string, string>(); Contact.put ("UID", contactId); Contact.put ("Name", ContactName); Contact.put ("Phone", Contactphone);        Contactdata.add (contact);        } cursor.close (); returnContactdata;

The above demo also contains the deletion of the contact person

2. Add a Contact

String contactname=Name.gettext (). toString (). Trim (); String Contactphone=Phone.gettext (). toString (). Trim (); ArrayList<ContentProviderOperation> operation=NewArraylist<contentprovideroperation>(); //add a piece of data to the Raw_contacts primary tableContentprovideroperation op1=Contentprovideroperation.newinsert (Rawcontacts.content_uri). Withvalue ("Account_id",NULL). build ();                Operation.add (OP1); //Add a contact name type to the data tableContentprovideroperation op2=Contentprovideroperation.newinsert (Data.content_uri). Withvaluebackreference (data.raw_contact_id, 0). Withvalue (Data.mimetype, Structuredname.content_item_type). Withvalue (Structuredname.        Given_name, ContactName). Build ();        Operation.add (OP2); //Add a phone type of data to the database tableContentprovideroperation op3=Contentprovideroperation.newinsert (Data.content_uri). Withvaluebackreference (data.raw_contact_id, 0). Withvalue (Data.mimetype, Phone.content_item_type). Withvalue (Phone.number, Contactpho        NE). Withvalue (Phone.type, Phone.type_mobile). Build ();                Operation.add (OP3); Try{            //perform the above operation, the method will open the transactionGetcontentresolver (). Applybatch ("Com.android.contacts", operation); Toast.maketext (addcontact. This, "added success! ", Toast.length_short). Show (); } Catch(RemoteException |operationapplicationexception e)        {E.printstacktrace (); }

The action for a contact is, in fact, an operation for data/data/com.android.providers.contacts/databases/contacts2.db

The main needs to understand the table: Contacts,raw_contacts,data,mimetype, for the operation of the call record, you need the file under the calls and other table operations.

The URI involved:

Contacts.content_uri

Corresponds to Contacts table, mainly contact person's main information

The above figure only captures part of the content

Data.content_uri

Corresponds to data in the database table

It can be seen that some phone data types are wrong and become the name type, which is why the phone number is not displayed before

CommonDataKinds.Phone.CONTENT_URI

corresponding to the data table, the type is the telephone number

CommonDataKinds.Email.CONTENT_URI

corresponding to the data table, the type is the telephone number

Rawcontacts.content_uri

Corresponds to the data in the Raw_contacts table

This is a ContentProvider app in Android, and many apps share their data through ContentProvider.

contentprovider-Curd of the contact person

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.