Android programming operates contact methods of query get add etc

  • 2020-12-10 00:51:55
  • OfStack

This article illustrates how Android can program to manipulate contacts. To share for your reference, the details are as follows:

The contacts in Android system also provide data through ContentProvider. Here, we realize to get all contacts, get contacts by phone number, add contacts and add contacts by transaction.

Get all contacts

1. The contacts in the Android system also provide data through ContentProvider

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

3. There are three tables we need to focus on

raw_contacts: Where the contact id is saved
data: The relationship between raw_contacts and raw_contacts is many-to-one and holds various data of the contact
mimetypes: Is the data type

4. authorites of Provider is ES40en. android. contacts

5. Query raw_contacts table path: contacts

6. The path to query data table is contacts/#/data

This path is a join 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 id to query the corresponding data from 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, null, null);
  while (idCursor.moveToNext()) {
    // Access to the raw_contacts In the table id
    int id = idCursor.getInt(0);
    // According to what you get ID The query data Data in a table 
    uri = 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);
    // Query the contacts list 
    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);
  }
}

Get contacts by phone number

1. The system provides the function of obtaining the data of data table according to the telephone number. The path is: data/phones/filter/*

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

Example:


// Look up the contact name based on the 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 contacts

1. First insert id into raw_contacts table, path: raw_contacts
2. After getting id, insert data into data table, path: data

Example:


// Add contacts 
ublic void testInsert() {
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentValues values = new ContentValues();
//  to raw_contacts insert 1 In addition to the article ID outside ,  Everything else is NULL The record of , ID It's 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 contact number 
values.clear(); //  Clean up the data from last time 
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 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);

Add contacts using transactions

1. Access Provider for multiple times when adding contacts. If an exception occurs during the process, incomplete data will appear

2. applyBatch using ContentResolver (String authority,ArrayList < ContentProviderOperation > The operations) method allows multiple operations to be performed in a single transaction

3. Document location:

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

Example:


// Add contacts 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.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);
  //  Batch execution of multiple operations in a transaction 
  resolver.applyBatch("com.android.contacts", operations);
}

I hope this article has been helpful in Android programming.


Related articles: