欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-10-29 16:09:28
本文实例讲述了android编程实现通讯录中联系人的读取,查询,添加功能。分享给大家供大家参考,具体如下: 先加二个读和写权限:

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

先加二个读和写权限:

<uses-permission android:name="android.permission.read_contacts" />
<uses-permission android:name="android.permission.write_contacts" />

具体代码:

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<contentprovideroperation> operations = new arraylist<contentprovideroperation>();
    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程序设计有所帮助。