Android Action Contacts

Source: Internet
Author: User

the contacts in the Android system are also available through ContentProvider, where we get all the contacts, get contacts by phone number, add contacts, add contacts using transactions.

Get all Contacts

1. The contacts in the Android system are also available through the ContentProvider for external data .

2. The database path is:/data/data/com.android.providers.contacts/database/contacts2.db

3. There are 3 tables that we need to focus on

raw_contacts: The contact ID is saved

Data : And Raw_contacts is a many-to-one relationship that preserves the contacts ' individual

mimetypes: For data type

4. The authorites of provider is Com.android.contacts

5. The path to query the Raw_contacts table is: Contacts

6. The path to query the data table is: Contacts/#/data

This path is a connection query, the query "mimetype" field can be queried according to "MIMETYPE_ID" to the data in the Mimetypes table

7. Query raw_contacts to get the ID of each contact, use the ID to query the data table, according to mimetype classification data

Example:

[HTML]View Plain copy print?
  1. //Query all contacts
  2. Public void Testgetall () {
  3. Contentresolver Resolver = GetContext (). Getcontentresolver ();
  4. Uri URI = Uri . Parse ("content://com.android.contacts/contacts");
  5. Cursor Idcursor = Resolver . Query (URI, new string[] {"_id"}, NULL, NULL, NULL);
  6. While (Idcursor.movetonext ()) {
  7. //Gets the ID in the Raw_contacts table
  8. int ID = Idcursor . GetInt (0);
  9. //Query the data table based on the acquired ID
  10. uri = uri. Parse ("content://com.android.contacts/contacts/" + ID + "/data");
  11. Cursor Datacursor = Resolver . Query (URI, new string[] {"Data1", "mimetype"}, NULL, NULL, NULL);
  12. StringBuilder SB = New StringBuilder ();
  13. sb.append ("ID=" + ID);
  14. //query in the Contact table
  15. While (Datacursor.movetonext ()) {
  16. String Data = Datacursor . getString (0);
  17. String type = Datacursor . getString (1);
  18. if ("Vnd.android.cursor.item/name". Equals (type))
  19. sb.append (", name=" + data);
  20. else if ("Vnd.android.cursor.item/phone_v2". Equals (type))
  21. sb.append (", phone=" + data);
  22. else if ("Vnd.android.cursor.item/email_v2". Equals (type))
  23. sb.append (", email=" + data);
  24. }
  25. System.out.println (SB);
  26. }
  27. }
//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, NULL, NULL), while (Idcursor.movetonext ()) {//Get to Raw_co The Idint ID in the ntacts table = idcursor.getint (0); The Data Uri = Uri.parse ("content://com.android.contacts/contacts/" + ID + "/data") is queried according to the obtained ID. Cursor datacursor = Resolver.query (URI, new string[] {"Data1", "mimetype"}, NULL, NULL, NULL); StringBuilder sb = new StringBuilder () sb.append ("id=" + ID);//Querying the Contact table for while (Datacursor.movetonext ()) {String data = dat Acursor.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);}} 


get contacts by phone number

1. The system provides the ability to obtain data from a phone number, the path is: data/phones/filter/*

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

Example:

[HTML]View Plain copy print?
    1. //query contact name by phone
    2. public void Testgetname () {
    3. Contentresolver Resolver = GetContext (). Getcontentresolver ();
    4. uri uri = uri . Parse ("content:// Com.android.contacts/data/phones/filter/1111 ");
    5. Cursor c = resolver . Query (URI, new String [] {"Display_name"}, NULL, NULL, NULL);
    6. while (C.movetonext ()) {
    7. System.out.println (c.getstring (0));
    8. }
    9. }
Query contact name based on 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, NULL), while (C.movetonext ()) {System.out.print ln (c.getstring (0));}}

Add a contact person

1. Insert the ID into the Raw_contacts table first, the path is: Raw_contacts

2. After obtaining the ID and then inserting the data into the table, the path is:

Example:

[HTML]View Plain copy print?
  1. //Add a contact
  2. ublic void Testinsert () {
  3. Contentresolver Resolver = GetContext (). Getcontentresolver ();
  4. Uri URI = Uri . Parse ("content://com.android.contacts/raw_contacts");
  5. contentvalues Values = New contentvalues ();
  6. //raw_contacts insert a record with all the NULL except the ID, the ID is automatically generated
  7. Long ID = Contenturis . Parseid (Resolver.insert (URI, values));
  8. //Add contact person's name
  9. uri = uri. Parse ("Content://com.android.contacts/data");
  10. values.put ("raw_contact_id", id);
  11. values.put ("Data2", "FHM");
  12. values.put ("MimeType", "Vnd.android.cursor.item/name");
  13. Resolver.insert (URI, values);
  14. //Add Contact phone
  15. values.clear ();//Clear the last data
  16. values.put ("raw_contact_id", id);
  17. values.put ("Data1", "18600000000");
  18. values.put ("Data2", "2");
  19. values.put ("MimeType", "Vnd.android.cursor.item/phone_v2");
  20. Resolver.insert (URI, values);
  21. //Add Contact Mailbox
  22. values.clear ();
  23. values.put ("raw_contact_id", id);
  24. values.put ("Data1", "[email protected]");
  25. values.put ("Data2", "1");
  26. values.put ("MimeType", "Vnd.android.cursor.item/email_v2");
  27. Resolver.insert (URI, values);
//add contact public void Testinsert () {Contentresolver resolver = GetContext (). Getcontentresolver (); Uri uri = uri.parse ("content://com.android.contacts/raw_contacts"); Contentvalues values = new Contentvalues ();//Inserts a raw_contacts in addition to the ID, all other records that are null, ID is the auto-generated long id = Contenturis.parseid (Resolver.insert (URI, values)); Add 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 Contact phone values.clear (); Empty the last 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 Contact Mailbox Values.clear (); Values.put ( "raw_contact_id", id), Values.put ("Data1", "[email protected]"), Values.put ("Data2", "1"); Values.put ("MimeType "," Vnd.android.cursor.item/email_v2 "); Resolver.insert (URI, values);} 


Add a contact using a transaction

1. When adding a contact is a multiple access to provider, if there is an exception in the process, there will be incomplete data situation, these operations should be placed in a transaction

2. Using the Contentresolver applybatch (String authority,arraylist<contentprovideroperation> operations) Method can execute multiple operations in a single transaction

3. Document Location:

file:///F:/android-sdk-windows/docs/reference/android/provider/ContactsContract.RawContacts.html

Example:

[HTML]View Plain copy print?
  1. //Use transactions to add contacts
  2. Public void Testinsertbatch () throws Exception {
  3. Contentresolver Resolver = GetContext (). Getcontentresolver ();
  4. ArrayList < contentprovideroperation > operations = new ArrayList < Contentprovideroperation>();
  5. contentprovideroperation Operation1 = contentprovideroperation //
  6. . Newinsert (Uri.parse ("content://com.android.contacts/raw_contacts"))//
  7. . Withvalue ("_id", NULL)//
  8. . Build ();
  9. Operations.add (operation1);
  10. contentprovideroperation Operation2 = contentprovideroperation //
  11. . Newinsert (Uri.parse ("Content://com.android.contacts/data"))//
  12. . Withvaluebackreference ("raw_contact_id", 0)//
  13. . Withvalue ("Data2", "Zzh")//
  14. . Withvalue ("MimeType", "vnd.android.cursor.item/name")//
  15. . Build ();
  16. Operations.add (operation2);
  17. contentprovideroperation Operation3 = contentprovideroperation //
  18. . Newinsert (Uri.parse ("Content://com.android.contacts/data"))//
  19. . Withvaluebackreference ("raw_contact_id", 0)//
  20. . Withvalue ("Data1", "18612312312")//
  21. . Withvalue ("Data2", "2")//
  22. . Withvalue ("MimeType", "VND.ANDROID.CURSOR.ITEM/PHONE_V2")//
  23. . Build ();
  24. Operations.add (operation3);
  25. contentprovideroperation Operation4 = contentprovideroperation //
  26. . Newinsert (Uri.parse ("Content://com.android.contacts/data"))//
  27. . Withvaluebackreference ("raw_contact_id", 0)//
  28. . Withvalue ("Data1", "[email protected]")//
  29. . Withvalue ("Data2", "2")//
  30. . Withvalue ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2")//
  31. . Build ();
  32. Operations.add (operation4);
  33. //batch execution of multiple operations in a transaction
  34. Resolver.applybatch ("com.android.contacts", operations);
  35. }
Use transaction Add 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 "," [email  Protected]//.withvalue ("Data2", "2")//.withvalue ("MimeType", "Vnd.android.cursor.item/email_v2")//.build (); O Perations.add (OPERATION4);//batch execution of Resolver.applybatch ("com.android.contacts", operations) for multiple operations in a transaction;}


Android Action Contacts

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.