Android编程实现通讯录中联系人的读取,查询,添加功能示例

本文实例讲述了Android编程实现通讯录中联系人的读取,查询,添加功能。分享给大家供大家参考,具体如下:

我们提供的服务有:成都网站建设、成都网站制作、微信公众号开发、网站优化、网站认证、灵川ssl等。为上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的灵川网站制作公司

先加二个读和写权限:




具体代码:

package com.eboy.test;
import java.util.ArrayList;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
public class ContectTest extends AndroidTestCase {
  private static final String TAG = "TestContact";
  //查询所有联系人的姓名,电话,邮箱
  public void TestContact() throws Exception {
    Uri uri = Uri.parse("content://com.android.contacts/contacts");
    ContentResolver resolver = getContext().getContentResolver();
    Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
    while (cursor.moveToNext()) {
      int contractID = cursor.getInt(0);
      StringBuilder sb = new StringBuilder("contractID=");
      sb.append(contractID);
      uri = Uri.parse("content://com.android.contacts/contacts/" + contractID + "/data");
      Cursor cursor1 = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);
      while (cursor1.moveToNext()) {
        String data1 = cursor1.getString(cursor1.getColumnIndex("data1"));
        String mimeType = cursor1.getString(cursor1.getColumnIndex("mimetype"));
        if ("vnd.android.cursor.item/name".equals(mimeType)) { //是姓名
          sb.append(",name=" + data1);
        } else if ("vnd.android.cursor.item/email_v2".equals(mimeType)) { //邮箱
          sb.append(",email=" + data1);
        } else if ("vnd.android.cursor.item/phone_v2".equals(mimeType)) { //手机
          sb.append(",phone=" + data1);
        }
      }
      cursor1.close();
      Log.i(TAG, sb.toString());
    }
    cursor.close();
  }
  //查询指定电话的联系人姓名,邮箱
  public void testContactNameByNumber() throws Exception {
    String number = "18052369652";
    Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + number);
    ContentResolver resolver = getContext().getContentResolver();
    Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null);
    if (cursor.moveToFirst()) {
      String name = cursor.getString(0);
      Log.i(TAG, name);
    }
    cursor.close();
  }
  //添加联系人,使用事务
  public void testAddContact() throws Exception {
    Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
    ContentResolver resolver = getContext().getContentResolver();
    ArrayList operations = new ArrayList();
    ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
      .withValue("account_name", null)
      .build();
    operations.add(op1);
    uri = Uri.parse("content://com.android.contacts/data");
    ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
      .withValueBackReference("raw_contact_id", 0)
      .withValue("mimetype", "vnd.android.cursor.item/name")
      .withValue("data2", "龚小永")
      .build();
    operations.add(op2);
    ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
      .withValueBackReference("raw_contact_id", 0)
      .withValue("mimetype", "vnd.android.cursor.item/phone_v2")
      .withValue("data1", "13539777967")
      .withValue("data2", "2")
      .build();
    operations.add(op3);
    ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri)
    .withValueBackReference("raw_contact_id", 0)
    .withValue("mimetype", "vnd.android.cursor.item/email_v2")
    .withValue("data1", "asdfasfad@163.com")
    .withValue("data2", "2")
    .build();
  operations.add(op4);
    resolver.applyBatch("com.android.contacts", operations);
  }
}

完整实例代码点击此处本站下载

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。


当前名称:Android编程实现通讯录中联系人的读取,查询,添加功能示例
转载来于:http://ybzwz.com/article/pciijp.html