Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Contact Phone Number From URI in Android

I am trying to get the contact's phone number after I have retrieved their ID number from the built-in activity. However, whenever I query the database using the cursor in my code below -- I get zero rows returned even though there is a mobile number for the contact I have selected.

Can anyone point me in a better direction or show an example of how to get the contact's phone number AFTER getting their userID?

My code:

    private Runnable getSMSRunnable() {
    return new Runnable() {
    public void run() {
    Intent i = new Intent(Intent.ACTION_PICK,
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(i, CONTACTS_REQUEST_CODE);
   }
  };
 }

Returns the Log output

content://com.android.contacts/data/6802

From which i pass the ID (6802) into a method which is designed to return the phone number from the ID with the given type (in this case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)

public static String getContactPhoneNumberByPhoneType(Context context, long contactId, int type) {
    String phoneNumber = null;

    String[] whereArgs = new String[] { String.valueOf(contactId), String.valueOf(type) };

    Log.d(TAG, String.valueOf(contactId));

    Cursor cursor = context.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? and "
                    + ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);

    int phoneNumberIndex = cursor
            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

    Log.d(TAG, String.valueOf(cursor.getCount()));

    if (cursor != null) {
        Log.v(TAG, "Cursor Not null");
        try {
            if (cursor.moveToNext()) {
                Log.v(TAG, "Moved to first");
                Log.v(TAG, "Cursor Moved to first and checking");
                phoneNumber = cursor.getString(phoneNumberIndex);
            }
        } finally {
            Log.v(TAG, "In finally");
            cursor.close();
        }
    }

    Log.v(TAG, "Returning phone number");
    return phoneNumber;
}

Which returns null for a phone number -- which means it cannot find the row that I am trying to access -- which means that something is wrong with my query -- however if I check a contact that has a mobile phone number -- how could I get a 0 row query?

Any help would be greatly appreciated. Thank you so much!

like image 420
hwrdprkns Avatar asked Jul 30 '10 10:07

hwrdprkns


2 Answers

I found the answer.

The reason I was not getting any rows from the cursor was because I was using the line

ContactsContract.CommonDataKinds.Phone.CONTACT_ID

"The id of the row in the Contacts table that this data belongs to."

Since I was getting the URI from contacts table anyways -- this was not needed and the following should have been substituted. The ID was the one corresponding to the contact in the phone table not the raw contact.

ContactsContract.CommonDataKinds.Phone._ID

Exchanging the lines returned the correct results in the query. Everything seems to be working well at the moment.

like image 53
hwrdprkns Avatar answered Oct 24 '22 02:10

hwrdprkns


This should work, (maybe try losing the type)

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

        if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Do something with phones
        } 
        pCur.close();
    }

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

like image 21
Pentium10 Avatar answered Oct 24 '22 02:10

Pentium10