如何知道一个电话号码是否保存

时间:2019-05-26 15:08:22

标签: android phone-number

我要检查用户的联系人中是否保存了一个电话号码? 下面是我的代码,我不能使用它 如何查看“是否”?

 public String findNameByNumber(String num){
    Uri uri = Uri.withAppendedPath(android.provider.Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(num));
    String res = null;
    try {
        ContentResolver resolver = getContentResolver();

        Cursor c = resolver.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

        if (c != null) { // cursor not null means number is found contactsTable
            if (c.moveToFirst()) {   // so now find the contact Name
                res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            }
            c.close();
        }
    } catch (Exception ex) {
    /* Ignore */
    }
    return res;
}

0 个答案:

没有答案