0

Om Button click, I am showing contact chooser box. From where I choose any of the contact. And then showing contact's phone number.

But the issue is, sometimes I don't see the phone number after selecting a contact. I don't know the reason, but I guess it's because the chooser is showing facebook, watsapp contacts as well.

How can I filter only Phone and SIM contact to be shown in chooser?

I am creating the chooser like:

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

Handing and showing the number like:

protected 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, new String[]{ 
ContactsContract.CommonDataKinds.Phone.NUMBER,  
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);

if (c != null && c.moveToFirst()) {
String number = c.getString(0);
int type = c.getInt(1);
showSelectedNumber(type, number);
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}

public void showSelectedNumber(int type, String number) {
    Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show();      
}

can anyone help me here. So that whenever I choose a contact, It must show the number? Advance Thanks

ravi tiwari
  • 521
  • 1
  • 8
  • 24

1 Answers1

0

this is what i use in my app.

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);

    if (Build.VERSION.SDK_INT <= 4) {
        intent.setType(Contacts.People.CONTENT_ITEM_TYPE);
    } else {
        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    }
Zhenghong Wang
  • 2,117
  • 17
  • 19