Methods of Android programming to operate contacts (query, get, add, etc.)

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

Get permissions for all contacts:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

get all contacts

1. Contacts in the Android system also provide external data through ContentProvider

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

3. There are 3 tables we need to pay attention to

raw_contacts: which stores the contact id
data: and raw_contacts is a many-to-one relationship, saves the data of the contact
mimetypes: is the data type

4. The authorites of the Provider are 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. 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 the 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()) {
    //Get the id in the raw_contacts table
    int id = idCursor.getInt(0);
    //Query the data in the data table according to the obtained ID
    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 in the contact table
    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 data in the data table according to the phone number. The path is: data/phones/filter/*

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

Example:

 

//Query the contact name according to 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 the id into the raw_contacts table, the path is: raw_contacts
2. After getting the id, insert the data into the data table, the path is: data

Example:

//Add contacts
public void testInsert() {
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentValues values = new ContentValues();
// Insert a record into raw_contacts that is all NULL except for the ID, the ID 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 contact phone
values.clear(); // clear 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 email
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 Contacts Using Transactions

 

1. When adding a contact, you need to access the Provider several times. If an exception occurs during the process, the data will be incomplete. These operations should be placed in one transaction.

2. Use the applyBatch(String authority,ArrayList<ContentProviderOperation> operations) method of ContentResolver to execute multiple operations in one 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", "[email protected]") //
      .withValue("data2", "2") //
      .withValue("mimetype", "vnd.android.cursor.item/email_v2") //
      .build();
  operations.add(operation4);
  // Batch execute multiple operations in a transaction
  resolver.applyBatch("com.android.contacts", operations);
}

 

?

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326323906&siteId=291194637