gpt4 book ai didi

android - 检查来电号码是否存储在联系人列表中

转载 作者:太空狗 更新时间:2023-10-29 16:16:10 26 4
gpt4 key购买 nike

在我的 android 应用程序中,当有来电时,我想显示我的自定义 ui,我能够做到这一点。不,我想检查传入号码是否来自联系人。下面是我这样做的代码,但它为存储在我的联系人列表中的传入号码返回 null。

public String findNameByNumber(String num){
Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(num));

String name = null;
Cursor cursor = mContext.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();
callyName.setText(name+" Calling..");
}
return name;
}

我接到一个号码 say+917878787878 的来电,但在我的联系人中,此联系人存储为名称 XYZ,号码为 78 78 787878,其格式是因为号码之间有空格。也尝试排除 +91,但仍然它返回空值。那么我怎样才能找到以任何格式存储的号码。它可能与国家代码一起存储,也可能不与国家代码一起存储。

提前致谢。

最佳答案

试试这个代码(使用 PhoneLookup.CONTENT_FILTER_URI 而不是 Phones):

String res = null;
try {
ContentResolver resolver = ctx.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor c = resolver.query(uri, new String[]{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(CommonDataKinds.Phone.DISPLAY_NAME));
}
c.close();
}
} catch (Exception ex) {
/* Ignore */
}
return res;

正如文档所说 ContactsContract.PhoneLookup :表示查找电话号码结果的表格,例如来电显示。要执行查找,您必须将要查找的号码附加到 CONTENT_FILTER_URI。此查询经过高度优化。

关于android - 检查来电号码是否存储在联系人列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26859338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com