Android operation contact

Source: Internet
Author: User

Contacts in the Android system also provide external data through contentprovider. Here we can obtain all contacts, obtain contacts through phone numbers, add contacts, and use transactions to add Contacts.

Get all contacts

1. Contacts in the Android system provide data externally through contentprovider.

2. Database path:/data/COM. Android. providers. Contacts/database/contacts2.db

3. We need to pay attention to three tables.

Raw_contacts: the contact ID is saved.

Data: it is a many-to-one relationship with raw_contacts. It stores various contact data.

Mimetypes: Data Type

4. The authorites of the provider is com. Android. Contacts.

5. The path for querying the raw_contacts table is contacts.

6. The path for querying the data table is contacts/#/Data

This path is for connection query. to query the "mimetype" field, you can query the data in the mimetypes table according to "mimetype_id ".

7. First query raw_contacts to get the ID of each contact. Then, use the ID to query the corresponding data from the data table and classify the data according to mimetype.

Example:

// Query all contacts public void testgetall () {contentresolver resolver = getcontext (). getcontentresolver (); Uri uri = Uri. parse ("content: // COM. android. contacts/contacts "); cursor idcursor = resolver. query (Uri, new string [] {"_ id"}, null); While (idcursor. movetonext () {// obtain the idint id = idcursor in the raw_contacts table. getint (0); // query the data uri = URI in the data table based on the obtained ID. parse ("content: // COM. android. contacts/contacts/"+ ID +"/Data "); cursor datacursor = resolver. query (Uri, new string [] {"data1", "mimetype"}, null); stringbuilder sb = new stringbuilder (); sb. append ("ID =" + id); // query the while (datacursor. movetonext () {string data = datacursor. getstring (0); string type = datacursor. getstring (1); If ("Vnd. android. cursor. item/name ". equals (type) Sb. append (", name =" + data); else if ("Vnd. android. cursor. item/phone_v2 ". equals (type) Sb. append (", phone =" + data); else if ("Vnd. android. cursor. item/email_v2 ". equals (type) Sb. append (", email =" + data);} system. out. println (SB );}}

 

Obtain a contact by phone number

1. The system provides the ability to obtain data in the data table based on the phone number. The path is data/phones/filter /*

2. Replace "*" with the phone number to find the required data. Get "display_name" to get the display name of the contact.

Example:

// Query the contact name by phone number. Public void testgetname () {contentresolver resolver = getcontext (). getcontentresolver (); Uri uri = Uri. parse ("content: // COM. android. contacts/data/phones/filter/1111 "); cursor c = resolver. query (Uri, new string [] {"display_name"}, null, null); While (C. movetonext () {system. out. println (C. getstring (0 ));}}

 

Add contact

1. Insert the ID to the raw_contacts table first. The path is raw_contacts.

2. Get the ID and insert the data to the data table. The path is data.

Example:

// Add the contact public void testinsert () {contentresolver resolver = getcontext (). getcontentresolver (); Uri uri = Uri. parse ("content: // COM. android. contacts/raw_contacts "); contentvalues values = new contentvalues (); // insert a record other than ID to raw_contacts that is all null, ID is the automatically generated long id = contenturis. parseid (resolver. insert (Uri, values); // Add the contact name uri = Uri. parse ("content: // COM. android. contacts/Data "); values. put ("raw_contact_id", ID); values. put ("data2", "FHM"); values. put ("mimetype", "Vnd. android. cursor. item/Name "); resolver. insert (Uri, values); // Add the contact number values. clear (); // clear the previous data values. put ("raw_contact_id", ID); values. put ("data1", "18600000000"); values. put ("data2", "2"); values. put ("mimetype", "Vnd. android. cursor. item/phone_v2 "); resolver. insert (Uri, values); // Add the contact email values. clear (); values. put ("raw_contact_id", ID); values. put ("data1", "zxx@itcast.cn"); values. put ("data2", "1"); values. put ("mimetype", "Vnd. android. cursor. item/email_v2 "); resolver. insert (Uri, values );}

 

Use transactions to add Contacts

1. the provider is accessed multiple times when a contact is added. If an exception occurs during the process, data is incomplete. These operations should be placed in a transaction.

2. Use the contentresolver's applybatch (string authority, arraylist <contentprovideroperation> operations) method to execute multiple operations in one transaction.

3. Document location:


File: // F:/Android-SDK-Windows/docs/reference/Android/provider/contactscontract.rawcontacts.html

Example:

// Use a transaction to add the contact public void testinsertbatch () throws exception {contentresolver resolver = getcontext (). getcontentresolver (); arraylist <contentprovideroperation> operations = new arraylist <contentprovideroperation> (); contentprovideroperation operation1 = contentprovideroperation //. newinsert (URI. parse ("content: // COM. android. contacts/raw_contacts "))//. withvalue ("_ id", null )//. build (); operations. add (operation1); contentprovideroperation operation2 = contentprovideroperation //. newinsert (URI. parse ("content: // COM. android. contacts/Data "))//. withvaluebackreference ("raw_contact_id", 0 )//. withvalue ("data2", "zzh ")//. withvalue ("mimetype", "Vnd. android. cursor. item/Name ")//. build (); operations. add (operation2); contentprovideroperation operation3 = contentprovideroperation //. newinsert (URI. parse ("content: // COM. android. contacts/Data "))//. withvaluebackreference ("raw_contact_id", 0 )//. withvalue ("data1", "18612312312 ")//. withvalue ("data2", "2 ")//. withvalue ("mimetype", "Vnd. android. cursor. item/phone_v2 ")//. build (); operations. add (operation3); contentprovideroperation operation4 = contentprovideroperation //. newinsert (URI. parse ("content: // COM. android. contacts/Data "))//. withvaluebackreference ("raw_contact_id", 0 )//. withvalue ("data1", "zq@itcast.cn ")//. withvalue ("data2", "2 ")//. withvalue ("mimetype", "Vnd. android. cursor. item/email_v2 ")//. build (); operations. add (operation4); // execute Resolver in batch for multiple operations in the transaction. applybatch ("com. android. contacts ", operations );}

 

 

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.