How to select multiple contacts from your phone using checkboxes

I am trying to select the contacts available from the phone programmatically and I am using the code below

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); startActivityForResult(intent, 1); 

However, the question is, how can I select multiple contacts at a time using the checkbox on the contact page?

+4
source share
2 answers

You will need to read the program messages and display them in the ListView in the Activity . Use CheckBox in ListView items and enable multiple items. Find a simple example / tutorial for ListView and start from there.

There are several reasons why it is better to create a custom ListView instead of using Intent(Intent.ACTION_GET_CONTENT); :

  • You may not be able to select multiples as you requested.
  • Even if you find a way to choose multiplets, it will be every version of the OS and device and may not work on all of them.
  • If there are several applications installed on any device that can handle ACTION_GET_CONTENT , then the user will be presented with a ACTION_GET_CONTENT and he will have to choose one of them. User selection may not support multiple contact selection.

Here is an example that reads your 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(); } } 
+11
source
  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); } 
+3
source

All Articles