我有一个问题。在我的应用程序中,我打开电话簿并提取您选择的人的姓名和号码。这是我的代码:
public void showContactsChooser(){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1001);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode){
case (1001):
if (resultCode == Activity.RESULT_OK){
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Cursor c = cr.query(contactData, null, null, null, null);
String phone = "";
if (c.moveToFirst()){
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + name + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
phone = 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:
break;
}
}
phones.close();
Toast.makeText(getApplicationContext(), name + " " + phone, Toast.LENGTH_SHORT).show();
Log.d("PICK", "Contact " + name + " " + phone);
}
}
}
}
}
在这段代码中,我打开电话簿,当您点击联系人姓名并获取电话号码时,请返回旧的Activiti。但我只能选一个人。我需要选择一些。我的问题:
我可以在电话簿中选择几个人,然后将其传递给应用程序。怎么做?
答案 0 :(得分:0)
据我所知,没有任何内置方法可以从联系人簿中选择多个联系人!
您可以做的是,获取联系人并在RecyclerView
或ListView
中使用自定义适配器复选框显示联系人。然后将这些选定的数据返回给您的活动。