這是一個兩個步驟:
- 從電話號碼獲取非接觸式ID
- 獲取請求的數據類型(電子郵件,電話等),從非接觸式ID
注意:
*多個聯繫人可能有相同的電話號碼,在這個例子中,我只是採取第一個contact-id返回,並忽略其餘
*請確保您導入以下所有從ContactsContract
或ContactsContract.CommonDataKinds
// Step 1:
Uri lookUpUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incoming_number));
String[] projection = new String[] { PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME }
Cursor cur = getContentResolver().query(lookUpUri,projection,null,null,null);
if (cur.moveToFirst()){
long id = cur.getLong(0);
String name = cur.getString(1);
Log.d(TAG, "found contact: " + id + " - " + name);
// Step 2:
String selection = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "', '" + Organization.CONTENT_ITEM_TYPE + "')";
String projection = new String[] { Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3 };
Cursor cur2 = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
while (cur2 != null && cur2.moveToNext()) {
String mime = cur2.getString(0); // email/phone/company
String data = cur2.getString(1); // the actual info, e.g. +1-212-555-1234
int type = cur2.getInt(2); // a numeric value representing type: e.g. home/office/personal
String label = cur2.getString(3); // a custom label in case type is "TYPE_CUSTOM"
String kind = "unknown";
String labelStr = "";
switch (mime) {
case Phone.CONTENT_ITEM_TYPE:
kind = "phone";
labelStr = Phone.getTypeLabel(getResources(), type, label);
break;
case Email.CONTENT_ITEM_TYPE:
kind = "email";
labelStr = Email.getTypeLabel(getResources(), type, label);
break;
case Organization.CONTENT_ITEM_TYPE:
kind = "company";
labelStr = Organization.getTypeLabel(getResources(), type, label);
break;
}
Log.d(TAG, "got " + kind + " - " + data + " (" + labelStr + ")");
}
if (cur2 != null) {
cur2.close();
}
} else {
Log.d(TAG, "contact not found");
}
cur.close();
感謝類,但它會顯示所有聯繫人的詳細信息...我想要一個特定數量的細節。爲什麼這將是查詢。我有CONTACT_ID – manjari
哎呦,看到第2步中的新選擇,現在應該工作 – marmor
感謝它的完美運作。 – manjari