Android編程實(shí)現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例

本文實(shí)例講述了Android編程實(shí)現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能。分享給大家供大家參考,具體如下:

我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、靈川ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的靈川網(wǎng)站制作公司

先加二個(gè)讀和寫權(quán)限:

<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";
  //查詢所有聯(lián)系人的姓名,電話,郵箱
  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)) { //手機(jī)
          sb.append(",phone=" + data1);
        }
      }
      cursor1.close();
      Log.i(TAG, sb.toString());
    }
    cursor.close();
  }
  //查詢指定電話的聯(lián)系人姓名,郵箱
  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();
  }
  //添加聯(lián)系人,使用事務(wù)
  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);
  }
}

完整實(shí)例代碼點(diǎn)擊此處本站下載。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

網(wǎng)站標(biāo)題:Android編程實(shí)現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例
URL標(biāo)題:http://bm7419.com/article28/pciijp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)外貿(mào)建站、響應(yīng)式網(wǎng)站品牌網(wǎng)站制作、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作