在一个简单的Android应用程序中,我需要从通讯录中使用联系人选择器获取电话号码。
要打开联系人选择器,我使用以下代码:
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);一切正常,但在联系人Picker中我可以看到来自gmail的联系人,如果我在手机应用程序中使用Contact Picker,我只能看到Addess book的联系人。如何将联系人选择器显示为电话应用程序(没有gmail联系人)?
发布于 2011-09-21 18:44:14
这帮助我只过滤电话号码:
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
ArrayList<String> phoneNumber = new ArrayList<String>();
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor
.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
phoneNumber.add(name);
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
hasPhone = "true";
} else {
hasPhone = "false";
}
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = null;
phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
phoneNumber
.add(phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
phones.close();
}
}
cursor.close();https://stackoverflow.com/questions/7498031
复制相似问题