Android – multiple choice contact intentions
•
Android
I'm trying to open contact intention and let the user select multiple contact intention call:
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts / phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
Using this method, the user can only select one contact. How can I let him select many contacts and get all the numbers he chooses?
resolvent:
Two do this!
1. Set the maximum selection limit of intention:
public static final int REQUEST_CODE_PICK_CONTACT = 1;
public static final int MAX_PICK_CONTACT= 10;
private void launchMultiplePhonePicker() {
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT);
phonebookIntent.putExtra("FromMMS", true);
startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK) {
if(requestCode == REQUEST_CODE_PICK_CONTACT) {
Bundle bundle = data.getExtras();
String result= bundle.getString("result");
ArrayList<String> contacts = bundle.getStringArrayList("result");
Log.i(TAG, "launchMultiplePhonePicker bundle.toString()= " + contactsPick.toString() );
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Note: it may vary on each operating system version and device, and may not apply to all operating system versions and devices
or
>To do so:
Programmatically read contacts and display them in the active listview, then use the check boxes in the listview items and allow multiple items to be selected
For example, how to read system contacts:
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();
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码