开发者

Retrieve Contact Phone Number From URI in Android

开发者 https://www.devze.com 2023-01-09 18:47 出处:网络
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

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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消