Greezer
Greezer

Reputation: 515

Android query phonenumber to obtain rawcontactID

I would like to query on phonenumber to obtain the rawcontactID.

The only thing I know of the contact is the given phonenumber, but for my function I need to have the rawcontactID. I got a working code but now I did use 2 seperate queries. What I would like to have is 1 query that can do both just to save some query time.

my code:

    Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber));       
    String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };     
    Cursor cursor = contentResolver.query(uri, columns, null, null, null); 

if(cursor!=null) { 
    int clenght = cursor.getCount();
    while(cursor.moveToNext()){ 
     //contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)); 
     id = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID)); 

    } 
    cursor.close(); 
}

Cursor pCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data.RAW_CONTACT_ID}, ContactsContract.Data.CONTACT_ID+" = "+ id, null, null); 
if(pCur!=null) { 
    int clenght = pCur.getCount();
    while(pCur.moveToNext()){ 
     //contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)); 
     id = pCur.getString(pCur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)); 

    } 
    pCur.close(); 
}

thanks in advance

Edit:

My code above works fine, but I am still looking for increasing speed for large number of contacts. Therefore I will give a bounty if someone comes with a solution to combine my queries.

Upvotes: 4

Views: 1569

Answers (2)

Jason Hessley
Jason Hessley

Reputation: 1628

private String[] getRawContactIdFromNumber(String givenNumber){
        List<String> rawIds = new ArrayList<String>();
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID},ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+ givenNumber +"'",null, ContactsContract.CommonDataKinds.Phone.NUMBER);

        while (phones.moveToNext())
        {
          rawIds.add( phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID)));
          Log.v("contacts","Given Number: " + givenNumber + "Raw ID: " +rawIds.get(rawIds.size() - 1));
        }
        phones.close();
        String[] ret = new String[0];

        return rawIds.toArray(ret);
    }

Edited to only include the raw id in the cursor for efficiency. Also changed return type to array in case multiple contacts have the same number.

Upvotes: 2

Nikhil
Nikhil

Reputation: 16196

Please try

String phonenumber = "input your phone number";
        Cursor pCur = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                new String[] { ContactsContract.Data.RAW_CONTACT_ID,
                        Phone.CONTACT_ID }, Phone.NUMBER + " = " + phonenumber,
                null, null);


if (pCur != null) {
                while (pCur.moveToNext()) {
                    String contactID = pCur.getString(pCur
                            .getColumnIndex(Phone.CONTACT_ID));
                    String Rowid = pCur.getString(pCur
                            .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
                    Log.e("RAW_CONTACT_ID", Rowid);
                    Log.e("CONTACT_ID", contactID);
                }
                pCur.close();
            }   

Now you can get Both CONTACT_ID & RAW_CONTACT_ID in single query.

Upvotes: 0

Related Questions