標籤:android 通訊錄 cursor
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));// 查看該連絡人有多少個電話號碼。如果沒有這傳回值為0int 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;}