Android讀取通訊錄聯絡人

2021-07-06 11:01:15 字數 2319 閱讀 8652

cursor phonenumbercursor;

string phoneprojection = ;

phonenumbercursor = contentresolver.query(contactscontract.commondatakinds

.phone

.content_uri, phoneprojection, null, null, null);

contactscontract.contacts.content_uri表中儲存的是聯絡人的姓名,通過下面的查詢來獲得聯絡人的姓名:

cursor namecursor;

string nameprojection = ;

namecursor = contentresolver.query(contactscontract.contacts

.content_uri, nameprojection, null, null, null);

兩個表靠_id欄位關聯,這樣我們就可以利用兩個表的資訊來得到聯絡人的姓名和**號碼。

為了使用方便,我們可以定義聯絡人物件的資料結構,最常用的是姓名和**字段,所以我們暫時只新增這兩個字段:

class personinfo
根據定義好的聯絡人物件,從兩個表中讀取對應的字段構建personinfo物件

//正規表示式,判斷是否是手機號

private boolean ismobilephonenumber(string phonenumber) $");

matcher matcher = pattern.matcher(phonenumber);

return matcher.matches();

}//格式化**號碼結構

private string phonenumberformat(string rawphonenumber)

if (rawphonenumber.contains(" "))

if (rawphonenumber.startswith("+86"))

return rawphonenumber;

}//讀取聯絡人**號碼

private void getphonenumlist() ;

phonenumbercursor = contentresolver.query(contactscontract.commondatakinds

.phone

.content_uri, phoneprojection, null, null, null);

if (phonenumbercursor == null)

if (phonenumbercursor.getcount() == 0)

if (phonenumbercursor.movetofirst())

} while (phonenumbercursor.movetonext());

if (!phonenumbercursor.isclosed()) }}

//讀取聯絡人姓名

private void buildpersoninfo() ;

namecursor = contentresolver.query(contactscontract.contacts

.content_uri, nameprojection, null, null, null);

if (namecursor == null)

if (namecursor.getcount() == 0)

int _idindex = namecursor.getcolumnindex(contactscontract.contacts._id);

int _nameindex = namecursor.getcolumnindex(contactscontract.contacts

.display_name);

for (personinfo obj : personinfoarraylist)

} while (namecursor.movetonext());}}

namecursor.close();

}

通過上面兩個方法,我們就可以得到構建好的聯絡人列表,在處理的過程中,我們針對讀取出來的手機號碼進行了簡單的格式化,去掉手機號中除了數字之外的不必要字元,同時針對處理後的號碼進行正則匹配,保證讀取出來的都是手機號碼。

核心的**就是從兩個表中讀取對應的資訊,然後進行組合。通訊錄的模組比較常用,我們可以將上述實現封裝成乙個類,對外只提供乙個介面,直接返回personinfoarraylist物件。

android讀取通訊錄聯絡人問題

string cols cursor cursor getcontentresolver query contactscontract.commondatakinds.phone.content uri,cols,null,null,null for int i 0 i cursor.getcoun...

訪問通訊錄中的聯絡人和新增聯絡人

1.在 androidmanifest.xml 中獲取通訊錄的讀寫功能 2.由於本 寫的是測試 因此也要在 androidmanifest.xml中加入測試啟動功能 3.獲取聯絡人資訊的函式constact public void constact throws exception phones.c...

手機通訊錄聯絡人恢復,誤刪的聯絡人怎麼找回

手機通訊錄聯絡人恢復,誤刪的聯絡人怎麼找回?手機上儲存了很多的 手機時不時的會提示 記憶體空間不足,及時清理 這aeittovcyz時開啟手機管家就能將裡面的資料清的一乾二淨了。那麼如果重要的聯絡人誤刪了該怎麼恢復呢?第二步 介面上顯示了9個功能塊,選擇 聯絡人恢復 一欄即可。第三步 軟體正在對資料...