Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Contacts mobile number only

There have been several questions on SO regarding getting Contacts numbers using the Contacts API but I'd like to know if there is a way to identify that the number retrieved is a mobile number.

The following code is often shown as a way to get a Contact's phone numbers, as it gets a list of one or more phone numbers:

String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";

    Cursor cursor = null;
    Cursor phones = null;
    try
    {
        cursor =  managedQuery(intent.getData(), projection, selection, null, null);
        while (cursor.moveToNext()) 
        {           
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
            while (phones.moveToNext()) 
            {               
                String pdata = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                Log.v("DATA",pdata);
            }                           
       }  
    }
    catch (NullPointerException npe)
    {
        Log.e(TAG, "Error trying to get Contacts.");
    }
    finally
    {
        if (phones != null)
        {
            phones.close();
        }
        if (cursor != null)
        {
            cursor.close();
        }           
    } 

Whilst, this works okay, is there any way to easily identify that the phone number is a mobile type (apart from trying to pattern match with Regex).

I suspect there must be a related piece of data, so that native apps can classify the phone number - as in the picture below:

Phone types in Contact info

like image 728
John J Smith Avatar asked Aug 18 '11 21:08

John J Smith


2 Answers

I stumbled upon a blog article which gives a pretty good explanation of using the ContactsContract api here.

So, in my example above, I changed part of my code above to this:

while (phones.moveToNext()) 
{                   
     int phoneType = phones.getInt(phones.getColumnIndex(Phone.TYPE));
     if (phoneType == Phone.TYPE_MOBILE)
     {
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
          phoneNumber = phoneNumber.replaceAll("\\s", "");
          break;
     }
}

This loops around all the phones for an individual contact and if the type is Phone.TYPE_MOBILE then it takes this one.

Hope this helps someone with the same issue I had.

like image 92
John J Smith Avatar answered Oct 11 '22 23:10

John J Smith


The original query cursor = managedQuery(intent.getData(), projection, selection, null, null); should be able to handle filter for you. I am trying to do the same thing. I will post my findings when complete. If anyone knows how to do this using the selection clause in the third parameter. I would love to know the answer.

like image 33
musterjunk Avatar answered Oct 11 '22 22:10

musterjunk