您將不得不以編程方式閱讀聯繫人,並在您的Activity
中的ListView
中顯示它們。在ListView
項目中使用CheckBox
s,並允許選擇多個項目。找到一個簡單的示例/教程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();
}
}
也許這會有所幫助:https://github.com/1gravity/Android-ContactPicker(我是作者) – 2016-05-28 06:44:42