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

后端 未结 2 1733
不思量自难忘°
不思量自难忘° 2021-02-09 15:05

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

2条回答
  •  悲&欢浪女
    2021-02-09 15:29

    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();
    }
    

提交回复
热议问题