在我的 Android 应用程序中,当有来电时,我想显示我的自定义用户界面,并且我能够做到这一点。
不,我想检查传入号码是否来自联系人。
下面是我执行此操作的代码,但对于存储在我的联系人列表中的传入号码,它返回 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;
}
我有一个号码打来的电话,比如+917878787878,但在我的联系人中,此联系人存储为名称 XYZ,号码为 78 78 787878,其格式是因为号码之间有空格。也尝试排除 +91,但它仍然返回 null 。
那么我如何找到以任何格式存储的号码。它可能与国家/地区代码一起存储,也可能不与国家/地区代码一起存储。
提前致谢。
尝试使用此代码(使用 PhoneLookup.CONTENT_FILTER_URI 而不是电话):
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;
正如文档所说联系人合同.电话查找:表示查找电话号码的结果的表,例如来电显示。要执行查找,您必须将要查找的号码附加到 CONTENT_FILTER_URI。该查询经过高度优化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)