Get contact name given a phone number in Android

前端 未结 4 423
礼貌的吻别
礼貌的吻别 2020-12-05 12:16

I am trying to retrieve contact names given the contact phone number. I made a function that should work in all API versions, by I can\'t make it work in 1.6 and I can\'t se

4条回答
  •  情书的邮戳
    2020-12-05 12:33

    public static String getContactName(Context context, String phoneNumber)
    {
        ContentResolver cr = context.getContentResolver();
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
        Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
        if (cursor == null)
        {
            return null;
        }
        String contactName = null;
        if(cursor.moveToFirst()) 
        {
            contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }
    
        if(cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    
        return contactName;
    }
    

提交回复
热议问题