Android編程操作聯(lián)系人的方法(查詢,獲取,添加等)
本文實(shí)例講述了Android編程操作聯(lián)系人的方法。分享給大家供大家參考,具體如下:
Android系統(tǒng)中的聯(lián)系人也是通過ContentProvider來對外提供數(shù)據(jù)的,我們這里實(shí)現(xiàn)獲取所有聯(lián)系人、通過電話號(hào)碼獲取聯(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)系人的各項(xiàng)數(shù)據(jù)
mimetypes:為數(shù)據(jù)類型
4. Provider的authorites為com.android.contacts
5. 查詢r(jià)aw_contacts表的路徑為:contacts
6. 查詢data表的路徑為:contacts/#/data
這個(gè)路徑為連接查詢,要查詢“mimetype”字段可以根據(jù)“mimetype_id”查詢到mimetypes表中的數(shù)據(jù)
7. 先查詢r(jià)aw_contacts得到每個(gè)聯(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);
}
}
通過電話號(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)碼查詢聯(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是自動(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í)候是分多次訪問Provider,如果在過程中出現(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ù)中對多個(gè)操作批量執(zhí)行
resolver.applyBatch("com.android.contacts", operations);
}
希望本文所述對大家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?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Android?控件自動(dòng)貼邊實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法,涉及Android針對WiFi屬性操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11
淺析Android Studio 3.0 升級(jí)各種坑(推薦)
本文是小編給大家收藏整理的關(guān)于Android Studio 3.0 升級(jí)后遇到的一些坑,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-11-11
Android Drawerlayout側(cè)拉欄事件傳遞問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout側(cè)拉欄事件傳遞問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程實(shí)現(xiàn)變化的雙重選擇框功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)變化的雙重選擇框功能,結(jié)合實(shí)例形式分析了Android雙重選擇框功能的樣式布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
RecycleView實(shí)現(xiàn)各種尺寸圖片展示
這篇文章主要為大家詳細(xì)介紹了RecycleView實(shí)現(xiàn)各種尺寸圖片展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05

