我正在尝试以编程方式选择手机中可用的联系人,并且我正在使用以下代码
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
但问题是如何使用联系人页面中的复选框一次选择多个联系人?
最佳答案
您必须以编程方式读取联系人并将它们显示在Activity
的ListView
中。在 ListView
项目中使用 CheckBox
并允许选择多个项目。为 ListView
找一个简单的示例/教程,然后从那里开始。
创建自定义 ListView
而不是使用 Intent(Intent.ACTION_GET_CONTENT);
有几个原因:
- 可能无法按照您的要求选择多个。
- 即使您找到了选择倍数的方法,它也会有所不同 每个操作系统版本和设备,可能无法在所有版本和设备上运行。
- 如果在任何设备上安装了多个应用程序
处理
ACTION_GET_CONTENT
,然后将向 用户,他将不得不选择其中之一。用户的选择 可能不支持选择多个联系人。
这是一个读取系统联系人的示例:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = myActivity.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int itype = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
final boolean isMobile =
itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE ||
itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;
// Do something here with 'phoneNumber' such as saving into
// the List or Array that will be used in your 'ListView'.
}
phones.close();
}
}
关于android - 如何使用复选框从电话中选择多个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15620805/