1

I am using this code to have the user select a contact to send an sms to:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == PICK_CONTACT){
        if(resultCode == RESULT_OK){
            Uri contactData = data.getData();
            Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
            if(cursor != null) {
                cursor.moveToFirst();
                String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                if (hasPhone.equals("1")) {
                    Cursor phones = getContentResolver().query
                            (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = " + contactId, null, null);
                    while (phones != null && phones.moveToNext()) {
                        number = phones.getString(phones.getColumnIndex
                                (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                    }
                    if(phones!=null) phones.close();

                } else {
                    Toast.makeText(getApplicationContext(), "This contact has no phone number", Toast.LENGTH_LONG).show();
                }
                cursor.close();

            }

        }
    }

Is there any way to read the phone number type when the user selects a contact, and then take an action based on what type of phone number there is? For example if there is a cell number, return it as a string, if there is a landline, do nothing, and if there is both, return just the cell?

gpickart
  • 87
  • 8
  • you can use google libphonenumber [libphonenumber](https://github.com/googlei18n/libphonenumber) library to find out. – YLS Dec 07 '15 at 08:16

1 Answers1

2

Yes, you can use the column TYPE to determine the type of phone number.

In your while loop add an if statement to check the type of phone number:

                while (phones != null && phones.moveToNext()) {
                    int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    if(type != ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                        // It's not a cellphone number, just skip
                        continue;
                    }
                    number = phones.getString(phones.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                }
David Fang
  • 1,777
  • 1
  • 14
  • 19