Android Programming Operator Contact Methods (query, get, add, etc.) _android

Source: Internet
Author: User

This example describes the Android programming approach to contact. Share to everyone for your reference, specific as follows:

The contacts in the Android system also provide data externally via ContentProvider, where we implement to get all the contacts, get contacts by phone numbers, add contacts, and add contacts using transactions.

Get all Contacts

1. The contact person in the Android system also provides data externally through ContentProvider.

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

3. We need to focus on 3 tables

Raw_contacts: The Contact ID is saved
Data: And Raw_contacts is a many-to-many relationship, saving all of the information of the contact person
Mimetypes: For data type

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

5. Query Raw_contacts the path of the table is: Contacts

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

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

7. First query raw_contacts get the ID of each contact, in the use of IDs from the data table to query the corresponding information, according to mimetype classification data

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, NULL, NULL);
    while (Idcursor.movetonext ()) {//get the id int id = idcursor.getint (0) in the Raw_contacts table;
    Queries the data table's URI by the acquired ID = uri.parse ("content://com.android.contacts/contacts/" + ID + "/data");
    Cursor datacursor = Resolver.query (URI, new string[] {"Data1", "mimetype"}, NULL, NULL, NULL);
    StringBuilder sb = new StringBuilder ();
    Sb.append ("id=" + ID);
      The while (Datacursor.movetonext ()) {String data = datacursor.getstring (0) in the Query Contact table;
      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 Contact by phone number

1. The system provides the function of obtaining data table according to telephone 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:

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.println (c.getstring (0));
  }
}

Add a Contact

1. Insert ID first to the Raw_contacts table, the path is: raw_contacts
2. The ID is then inserted into the data table, the path is: Data

Example:

Add contact ublic void Testinsert () {Contentresolver resolver = GetContext (). Getcontentresolver ();
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Contentvalues values = new Contentvalues ();
Inserts a record to raw_contacts that is all null except for the ID, which is automatically 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 a 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 a Contact mailbox 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);

Add a contact by using a transaction

1. When you add a contact, you can access provider multiple times, and if an exception occurs during the process, the data is incomplete and should be placed in a single transaction

2. Use of Contentresolver Applybatch (String authority,arraylist<contentprovideroperation> operations) Method can perform multiple operations in a single transaction

3. Document Location:

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

Example:

Add contacts by using transactions public void Testinsertbatch () throws Exception {Contentresolver resolver = GetContext (). Getcontentresolver
  ();
  arraylist<contentprovideroperation> operations = new arraylist<contentprovideroperation> (); Contentprovideroperation Operation1 = contentprovideroperation//. Newinsert (Uri.parse ("Content://com.android.conta
  Cts/raw_contacts "))//. Withvalue (" _id ", NULL)//. Build ();
  Operations.add (Operation1); Contentprovideroperation Operation2 = contentprovideroperation//. Newinsert (Uri.parse ("Content://com.android.conta Cts/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.conta Cts/data "))//. Withvaluebackreference (" raw_contact_id ", 0)//. WithvaLue ("Data1", "18612312312")//. Withvalue ("Data2", "2")//. Withvalue ("MimeType", "Vnd.android.cursor.item/pho")
  NE_V2 ")//. Build ();
  Operations.add (Operation3); Contentprovideroperation Operation4 = contentprovideroperation//. Newinsert (Uri.parse ("Content://com.android.conta Cts/data "))//. Withvaluebackreference (" raw_contact_id ", 0)//. Withvalue (" Data1 "," zq@itcast.cn ")//. W
  Ithvalue ("Data2", "2")//. Withvalue ("MimeType", "VND.ANDROID.CURSOR.ITEM/EMAIL_V2")//. Build ();
  Operations.add (OPERATION4);
Bulk execution of Resolver.applybatch ("com.android.contacts", operations) for multiple operations in a transaction;

 }

I hope this article will help you with your Android programming.

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.