我正在閱讀Android中聯繫人列表中的聯繫人姓名和電話號碼。我正在讀取名稱。但是,當我按姓名查找聯繫人電話號碼(在這種情況下,這些都是唯一的,這只是一個測試),它只適用於一個聯繫人,沒有爲他人獲取電話號碼,並獲得錯誤的電話號碼一個。閱讀聯繫人電話號碼適用於某些人而不是其他人?
這裏是我的getNumbers方法的代碼:
private String getNumber(String name){
String returnMe="";
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + getIntent().getStringExtra("name") + "'", null, null);
if(cursor.moveToFirst()){
String identifier = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone._ID + " = " + identifier, null, null);
while(phones.moveToNext()){
returnMe = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
switch(type){
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
System.out.println("mobile number found"); break;
default:
System.out.println("nothing found."); break;
}
}
}
return returnMe;
}
很高興我能幫到你。事實上,我認爲其他答案也適用,只使用聯繫人表,因此更簡單。聯繫人分爲3個表格:聯繫人,RawContacts和數據。每個表具有相應的類,例如, ContactsContract.Contacts。然後,您需要使用正確的鍵連接結果,就像您在SQL查詢中所做的那樣。確保你閱讀了關於所有https://developer.android.com/training/contacts-provider/index.html的文檔。 – 2014-11-11 05:12:45