Is there a simple way to check if an incoming caller is a contact in Android?

孤街醉人 提交于 2019-12-09 06:36:52

问题


When an Android phone receives a call it automatically checks if the call exists in its own contact database. I was wondering if there is a simple way to access that information. I have a PhoneStateListener that performs certain actions during a ringing state, and I want to check if the incoming caller is in the contacts list.

Is there a way to do this without going through the Contacts ContentProvider?


回答1:


The phone app uses the contacts ContentProvider too; I'm not sure why you would want to avoid that. Besides, it's the only publicly-accessible way of accessing that information.

Resolving a number to a name (pre 2.0, in this case) is simple enough anyway:

Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(number));

String name = null;
Cursor cursor = context.getContentResolver().query(uri, 
                    new String[] { Phones.DISPLAY_NAME }, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
    name = cursor.getString(cursor.getColumnIndex(Phones.DISPLAY_NAME));
    cursor.close();
}



回答2:


Here is the code for 2.0 and later

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);        

    if (cursor != null && cursor.moveToFirst()) {
        String name = cursor.getString(0);
        cursor.close();
    } 


来源:https://stackoverflow.com/questions/2193664/is-there-a-simple-way-to-check-if-an-incoming-caller-is-a-contact-in-android

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