Android 獲取通訊錄聯系人
public static Map<String,String> getAllCallRecords(Context context) {
Map<String,String> temp = new HashMap<String, String>();
Cursor c = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC");
if (c.moveToFirst()) {
do {
// 獲得聯系人的ID號
String contactId = c.getString(c
.getColumnIndex(ContactsContract.Contacts._ID));
// 獲得聯系人姓名
String name = c
.getString(c
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 查看該聯系人有多少個電話號碼。如果沒有這返回值為0
int phoneCount = c
.getInt(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String number=null;
if (phoneCount > 0) {
// 獲得聯系人的電話號碼
Cursor phones = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones.moveToFirst()) {
number = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
temp.put(name, number);
} while (c.moveToNext());
}
c.close();
return temp;
} </pre><br />
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!