Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Contact ID from Android Contacts database is not working as intended

I have the following code.

        int phoneContactID = new Random().nextInt();

    Cursor contactLookupCursor =  context.getContentResolver().query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(contactNumber)), new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},null,null,null);     

    try 
    {
        contactLookupCursor.moveToFirst();
        while(contactLookupCursor.moveToNext())
        {
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
         }
    } 
    finally 
    {
        contactLookupCursor.close();
    }       

The problem in the above code is that even if I give a existing number in emulator contacts, its not returning any results. I was testing it an hour back and it was working fine, and now when I tested it again, its not returning anything. I am not sure if anything is wrong with the code. What I am trying to do is get a ID that matches a single with multiple numbers. For instance say there is a contact name called "A" and A has two numbers. Essentially the contact ID for A should be 1 regardless of which number I refer to. Is my assumption correct ?

UPDATE : I did some more tests. Let's say if a number is stored without the country code in the contacts database like 222-222-2222. A search using the below code returns contact id only when I pass 2222222222 or 222-222-2222. And if the same number is stored like 12222222222 a valid contact id is received only if I search number is 12222222222.

        String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
    Uri contactUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(contactNumber));
    Cursor c = context.getContentResolver().query(contactUri, projection, null, null, null);
    if (c.moveToFirst()) {
        phoneContactID = c.getInt(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
    }

I am not sure if I am doing something wrong here. Any help would be appreciated.

like image 468
vikramjb Avatar asked Nov 29 '22 10:11

vikramjb


1 Answers

public static int getContactIDFromNumber(String contactNumber,Context context)
{
    contactNumber = Uri.encode(contactNumber);
    int phoneContactID = new Random().nextInt();
    Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
        while(contactLookupCursor.moveToNext()){
            phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
            }
        contactLookupCursor.close();

    return phoneContactID;
}

The code that is working now.

like image 123
vikramjb Avatar answered Dec 06 '22 10:12

vikramjb