Android編程操作聯(lián)系人的方法(查詢(xún),獲取,添加等)
本文實(shí)例講述了Android編程操作聯(lián)系人的方法。分享給大家供大家參考,具體如下:
Android系統(tǒng)中的聯(lián)系人也是通過(guò)ContentProvider來(lái)對(duì)外提供數(shù)據(jù)的,我們這里實(shí)現(xiàn)獲取所有聯(lián)系人、通過(guò)電話號(hào)碼獲取聯(lián)系人、添加聯(lián)系人、使用事務(wù)添加聯(lián)系人。
獲取所有聯(lián)系人
1. Android系統(tǒng)中的聯(lián)系人也是通過(guò)ContentProvider來(lái)對(duì)外提供數(shù)據(jù)的
2. 數(shù)據(jù)庫(kù)路徑為:/data/data/com.android.providers.contacts/database/contacts2.db
3. 我們需要關(guān)注的有3張表
raw_contacts:其中保存了聯(lián)系人id
data:和raw_contacts是多對(duì)一的關(guān)系,保存了聯(lián)系人的各項(xiàng)數(shù)據(jù)
mimetypes:為數(shù)據(jù)類(lèi)型
4. Provider的authorites為com.android.contacts
5. 查詢(xún)r(jià)aw_contacts表的路徑為:contacts
6. 查詢(xún)data表的路徑為:contacts/#/data
這個(gè)路徑為連接查詢(xún),要查詢(xún)“mimetype”字段可以根據(jù)“mimetype_id”查詢(xún)到mimetypes表中的數(shù)據(jù)
7. 先查詢(xún)r(jià)aw_contacts得到每個(gè)聯(lián)系人的id,在使用id從data表中查詢(xún)對(duì)應(yīng)數(shù)據(jù),根據(jù)mimetype分類(lèi)數(shù)據(jù)
示例:
//查詢(xún)所有聯(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查詢(xún)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);
//查詢(xún)聯(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);
}
}
通過(guò)電話號(hào)碼獲取聯(lián)系人
1. 系統(tǒng)內(nèi)部提供了根據(jù)電話號(hào)碼獲取data表數(shù)據(jù)的功能,路徑為:data/phones/filter/*
2. 用電話號(hào)碼替換“*”部分就可以查到所需數(shù)據(jù),獲取“display_name”可以獲取到聯(lián)系人顯示名
示例:
//根據(jù)電話號(hào)碼查詢(xún)聯(lián)系人名稱(chēng)
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是自動(dòng)生成的
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)系人得時(shí)候是分多次訪問(wèn)Provider,如果在過(guò)程中出現(xiàn)異常,會(huì)出現(xiàn)數(shù)據(jù)不完整的情況,這些操作應(yīng)該放在一次事務(wù)中
2. 使用ContentResolver的applyBatch(String authority,ArrayList<ContentProviderOperation> operations) 方法可以將多個(gè)操作在一個(gè)事務(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ù)中對(duì)多個(gè)操作批量執(zhí)行
resolver.applyBatch("com.android.contacts", operations);
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
- android獲取聯(lián)系人示例分享
- Android獲取聯(lián)系人頭像的方法
- Android獲取手機(jī)聯(lián)系人信息
- Android獲取手機(jī)聯(lián)系人電話號(hào)碼并返回結(jié)果
- Android獲取聯(lián)系人姓名和電話代碼
- Android實(shí)現(xiàn)獲取聯(lián)系人電話號(hào)碼功能
- android如何獲取聯(lián)系人所有信息
相關(guān)文章
Android 解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件問(wèn)題
這篇文章主要介紹了Android 解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-11-11
從"Show?tabs"了解Android?Input系統(tǒng)
這篇文章主要介紹了從"Show?tabs"了解Android?Input系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2023-01-01
Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)圓形進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn)
這篇文章主要介紹了Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-04-04
Android 圖片處理避免出現(xiàn)oom的方法詳解
本篇文章主要介紹了Android 圖片處理避免出現(xiàn)oom的方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

