0
我試圖獲取與android上給定電話號碼關聯的所有聯繫人姓名。如何使用電話號碼獲取所有聯繫人姓名?
public void getContactName(Context context, String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri, new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
System.out.println("ID : " + contactId + " Name : " + contactName);
}
cursor.close();
}
}
但我真的得到重複值
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3401 Name : Ann
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 3480 Name : Issac
10-02 16:47:06.585 23359-23359/? I/System.out﹕ ID : 2819 Name : Sam
這有什麼錯的代碼?如何通過傳遞電話號碼獲取所有聯繫人姓名?
加上'TYPE'和'NUMBER'你的投影和使用'DatabaseUtils.dumpCursor' – pskink
@pskink'光標光標= context.getContentResolver()查詢(URI,新String [] {ContactsContract.PhoneLookup.DISPLAY_NAME,ContactsContract.PhoneLookup ._ID,ContactsContract.PhoneLookup.TYPE,ContactsContract.PhoneLookup.NUMBER},null,null,null); DatabaseUtils.dumpCursor(cursor);'這是正確的嗎?我得到了相同的結果 – Selvam
我沒有看到logcat – pskink