Displaying contacts only with phone numbers using ACTION_PICK intent in Android device
Asked Answered
D

5

8

My goal is to only display contacts with phone number to user and let user select few contacts which I want to store locally.

I have used various options in place of ContactsContract.Contacts.CONTENT_URI in below method. But I am getting lot many of the contacts (many are junk with only email ids) displayed.

    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 parameter for above method and in case of below handler method, the String[] for the query method as projection parameters (which are shown commented), the method fails with java.lang.IllegalArgumentException. If I pass null in below method, then whatever contact I select, I don't find any column related to 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 any way to display only contacts visible to user usually when user goes to phone book and which has phone numbers available?

I tried going through all the threads in stackoverflow and other sites, but could not find any solution which resolve issue around this though many people have posted the issue. I haven't worked much with the Android platform and I might have missed out certain minor details and I believe there must be an easy way to achieve this.

Kindly suggest. Appreciate your help.

Thanks.

Doable answered 1/3, 2013 at 8:59 Comment(0)
D
20

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);
Diction answered 1/3, 2013 at 9:7 Comment(5)
Thanks. It worked. I just jumped to coding without reading much of the basics. I used ContactsContract.Contacts.CONTENT_TYPE but now I see there are such other options and the difference between them.Doable
Though I did not see email info returned for the contact. Isn't there any string parameter which can bring all the contact related info, including home, mobile numbers and emails?Doable
After going through more details and reading at below post at #8613031 it becomes very clear on what to be required to get the email and phone number both. To put it in short, after retrieving _id for the selected contact, from the intent displayed from above example, we need to re-query using contact resolver, passing the _id and appropriate Uri for Phone number and email to retrieve required details. Also link explains about how to handle scenario if the given contact has multiple phone numbers.Doable
Like this is what type we use to get contacts who have mailid's?Lance
didn't understand your questionDiction
S
3
*-> 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*
Substitutive answered 10/2, 2016 at 11:52 Comment(0)
O
1

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);
Ovi answered 16/10, 2014 at 18:26 Comment(0)
S
1
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
Shelly answered 20/3, 2015 at 8:11 Comment(0)
S
0

Use this:

Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/people"));
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
Sericin answered 2/1, 2016 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.