我的小测试应用程序使用联系人选择器选择一个联系人:
final Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, PICK_CONTACT);
在onActivityResult方法中,我解析结果,它类似于content://com.android.contacts/data/7557
。然后我查询这个uri来获取它的名字和查找键:
final String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY };
final Cursor c = getContentResolver().query(contactUri, projection, null, null, null);
if (c.moveToFirst()) {
final String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String lookupKey = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
Log.d(TAG, "picked contact, name: " + name + ", lookup key: " + lookupKey);
}
它工作正常,但不适用于某些联系人。某些联系人将导致空光标。 我检查了其他问题。这个似乎有同样的问题querying contacts - SOMETIMES returns empty cursor(但没有答案)。我尝试了这个问题中提出的解决方案的方法,Retrieve Contact Phone Number From URI in Android:
final String id = contactUri.getLastPathSegment();
final String[] whereArgs = new String[] { id };
final Cursor c2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone._ID + " = ?", whereArgs, null);
if (c2.moveToFirst()) {
final String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
final String lookupKey = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
}
但不幸的是问题仍然存在:某些联系人将导致空光标。 我究竟做错了什么?感谢任何人的帮助。