开发者

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

开发者 https://www.devze.com 2023-04-03 12:34 出处:网络
I have the following code. int phoneContactID = new Random().nextInt(); Cursor contactLookup开发者_如何学编程Cursor =context.getContentResolver().query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILT

I have the following code.

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

    Cursor contactLookup开发者_如何学编程Cursor =  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.


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.


Ok..Sorry i miss interpreted your question I had not done it.But i think follwing code might work Give it a try and lemme know:

Cursor phone_cursor = cr.query( 
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null,
                        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", 
                        new String[]{"YOUR NUMBER GOES HERE"}, null); 
                while (phone_cursor.moveToNext()) 
                { 

//here you can extract the inofrmation you want


phone_cursor.getString(             phone_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                }


Peform the steps in the follwing manner:

//This method will return a cursor which will contain whole info

private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,      
        };

        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

Next you can iterate through this Cursor to get the name and ID as Follows:

while (cursor.moveToNext()) 
            {
                namess[i]= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                id[i] = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                i++;
            }


vikramjb answer is great, I tried to do it in a clearer way (by using the instructions here https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html):

private Long GetContactIdFromNumber(ContentResolver contentResolver, String number)
{
    Uri contactLookup = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor contactLookupCursor = contentResolver.query(contactLookup,new String[] {ContactsContract.PhoneLookup._ID}, null, null, null);
    if (contactLookupCursor.moveToNext()){
        long ret = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
        contactLookupCursor.close();
        return ret;
    }
    contactLookupCursor.close();
    return 0l;
}

BTW, in api versions (above 21) there is the PHONE_ACCOUNT_ID column in CallLog.Calls: https://developer.android.com/reference/android/provider/CallLog.Calls.html

0

精彩评论

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