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

允我心安 提交于 2019-11-29 07:46:44
vikramjb
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++;
            }
walla

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!