【发布时间】:2017-03-16 16:16:28
【问题描述】:
我正在使用此处列出的代码Check Incoming number is stored in Contacts list or not android 来检查联系人中是否存在传入号码。此代码并不总是给出正确的结果。
在这个或其他更好的检查方法中是否需要一些更正?
代码:
String res = null;
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String a = uri.getLastPathSegment();
Cursor c = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone._ID + "=?", new String[]{a}, null);
if (c != null) { // cursor not null means number is found contactsTable
if (c.getCount() > 0) {
if (c.moveToFirst()) { // so now find the contact Name
res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//res = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}
c.close();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return res;
【问题讨论】:
-
"此代码并不总是给出正确的结果。"那么为什么不提供一些细节呢?解释为什么您还需要发布重复的问题。
-
为什么你捕捉到异常并忽略它,这样你将永远得到准确的结果。
-
@Panache 你不知道他是怎么赚到5962分的这需要一两年多的努力,不要怪别人浪费时间对你来说,要有礼貌并检查以下答案。