最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程操作聯(lián)系人的方法(查詢,獲取,添加等)

 更新時間:2016年01月04日 10:44:49   作者:傅榮康  
這篇文章主要介紹了Android編程操作聯(lián)系人的方法,包括針對聯(lián)系人的查詢,獲取,添加等操作,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Android編程操作聯(lián)系人的方法。分享給大家供大家參考,具體如下:

Android系統(tǒng)中的聯(lián)系人也是通過ContentProvider來對外提供數(shù)據(jù)的,我們這里實現(xiàn)獲取所有聯(lián)系人、通過電話號碼獲取聯(lián)系人、添加聯(lián)系人、使用事務(wù)添加聯(lián)系人。

獲取所有聯(lián)系人

1. Android系統(tǒng)中的聯(lián)系人也是通過ContentProvider來對外提供數(shù)據(jù)的

2. 數(shù)據(jù)庫路徑為:/data/data/com.android.providers.contacts/database/contacts2.db

3. 我們需要關(guān)注的有3張表

raw_contacts:其中保存了聯(lián)系人id
data:和raw_contacts是多對一的關(guān)系,保存了聯(lián)系人的各項數(shù)據(jù)
mimetypes:為數(shù)據(jù)類型

4. Provider的authorites為com.android.contacts

5. 查詢raw_contacts表的路徑為:contacts

6. 查詢data表的路徑為:contacts/#/data

這個路徑為連接查詢,要查詢“mimetype”字段可以根據(jù)“mimetype_id”查詢到mimetypes表中的數(shù)據(jù)

7. 先查詢raw_contacts得到每個聯(lián)系人的id,在使用id從data表中查詢對應(yīng)數(shù)據(jù),根據(jù)mimetype分類數(shù)據(jù)

示例:

//查詢所有聯(lián)系人
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()) {
    //獲取到raw_contacts表中的id
    int id = idCursor.getInt(0);
    //根據(jù)獲取到的ID查詢data表中的數(shù)據(jù)
    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);
    //查詢聯(lián)系人表中的
    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);
  }
}

通過電話號碼獲取聯(lián)系人

1. 系統(tǒng)內(nèi)部提供了根據(jù)電話號碼獲取data表數(shù)據(jù)的功能,路徑為:data/phones/filter/*

2. 用電話號碼替換“*”部分就可以查到所需數(shù)據(jù),獲取“display_name”可以獲取到聯(lián)系人顯示名

示例:

//根據(jù)電話號碼查詢聯(lián)系人名稱
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));
  }
}

添加聯(lián)系人

1. 先向raw_contacts表插入id,路徑為:raw_contacts
2. 得到id之后再向data表插入數(shù)據(jù),路徑為:data

示例:

//添加聯(lián)系人
ublic void testInsert() {
ContentResolver resolver = getContext().getContentResolver();
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentValues values = new ContentValues();
// 向raw_contacts插入一條除了ID之外, 其他全部為NULL的記錄, ID是自動生成的
long id = ContentUris.parseId(resolver.insert(uri, values));
//添加聯(lián)系人姓名
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);
//添加聯(lián)系人電話
values.clear(); // 清空上次的數(shù)據(jù)
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);
//添加聯(lián)系人郵箱
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);

使用事務(wù)添加聯(lián)系人

1. 在添加聯(lián)系人得時候是分多次訪問Provider,如果在過程中出現(xiàn)異常,會出現(xiàn)數(shù)據(jù)不完整的情況,這些操作應(yīng)該放在一次事務(wù)中

2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以將多個操作在一個事務(wù)中執(zhí)行

3. 文檔位置:

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

示例:

//使用事務(wù)添加聯(lián)系人
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);
  // 在事務(wù)中對多個操作批量執(zhí)行
  resolver.applyBatch("com.android.contacts", operations);
}

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

相關(guān)文章

最新評論

仲巴县| 浪卡子县| 阳城县| 赣州市| 德保县| 翁牛特旗| 乐昌市| 锦州市| 万载县| 古浪县| 宁安市| 济南市| 洱源县| 会东县| 天等县| 昭通市| 夹江县| 南漳县| 枞阳县| 太和县| 淳安县| 吉首市| 营口市| 达尔| 额敏县| 屏东县| 富川| 综艺| 祁东县| 南汇区| 平利县| 波密县| 湖州市| 嵩明县| 昌邑市| 西贡区| 清远市| 万载县| 岐山县| 手游| 巴青县|