Showing contacts with phone numbers only using the ACTION_PICK intent on an Android device

My goal is to display only contacts with a phone number for the user and allow the user to select multiple contacts that I want to save locally.

I used various options instead of ContactsContract.Contacts.CONTENT_URI below. But I get a lot of contacts (many of them are undesirable only with email identifiers).

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contact_selector); ((Button)findViewById(R.id.btnphonecontactlist)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(contactIntent, 1); } }); } 

If I pass ContactsContract.Contacts.CONTENT_URI as a parameter to the method described above, and in the case of the method below, the String [] handler for the query method as projection parameters (which is commented out), the method does not work with java.lang. IllegalArgumentException. If I pass null in the method below, then no matter which contact I choose, I will not find a single column associated with a phone number or email.

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if(data != null) { Uri uri = data.getData(); if(uri != null) { Cursor c = null; try { c = getContentResolver().query(uri, null // new String[] { //ContactsContract.CommonDataKinds.Phone.NUMBER, //ContactsContract.CommonDataKinds.Phone.TYPE}, , null, null, null); if(c != null && c.moveToFirst()) { String number = c.getString(0); String type = c.getString(1); } } finally { if(c != null && !c.isClosed()) c.close(); } } } } 

Is there a way to display only the contacts visible to the user, usually when the user goes to the phone book and has phone numbers?

I tried to go through all the threads in stackoverflow and other sites, but could not find a solution that solves the problem around this, although many people posted this problem. I didn’t work very much with the Android platform, and maybe I missed some small details, and I believe that this requires an easy way.

Please offer. Appreciate your help.

Thanks.

+7
source share
5 answers

Please use below code

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); startActivityForResult(intent, 1); 
+18
source
 *-> Add a permission to read contacts data to your application manifest. <uses-permission android:name="android.permission.READ_CONTACTS"/> -> Use Intent.ACTION_PICK in your Activity Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT); -> Then Override the onActivityResult() and retrieve the ID,Phone number and Name in the data. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // check whether the result is ok if (resultCode == RESULT_OK) { // Check for the request code, we might be usign multiple startActivityForReslut switch (requestCode) { case RESULT_PICK_CONTACT: Cursor cursor = null; try { String phoneNo = null ; String name = null; Uri uri = data.getData(); cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); phoneNo = cursor.getString(phoneIndex); textView2.setText(phoneNo); } catch (Exception e) { e.printStackTrace(); } break; } } else { Log.e("MainActivity", "Failed to pick contact"); } } This will work check it out* 
+2
source

The following code will do what you want.

 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult(Intent.createChooser(intent, "Contact"), PICK_CONTACT); 
+1
source
 Uri uri = Uri.parse("content://contacts"); Intent intent = new Intent(Intent.ACTION_PICK, uri); intent.setType(Phone.CONTENT_TYPE); startActivityForResult(intent, REQUEST_CODE); 
+1
source

Use this:

 Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people")); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); startActivityForResult(intent, 1); 
0
source

All Articles