>我使用以下代码从给定数字中从数据库中获取数字,为此我在查询下方触发以获取数字,但它不返回数字。我的查询中有错误吗?请帮助我解决此问题。
我需要使用LIKE函数,因为一些数字存储了特殊的字符和空格。
contactNumber = contactNumber.replaceAll("[-+.^:,]", "");
contactNumber="%"+contactNumber+"%";
String strEmail = "";
String name = "";
Cursor cursor = ctx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[]{contactNumber}, null);
以下是使用 ContentProviders 时使用 LIKE 的方法:
getContentResolver().query(Phone.CONTENT_URI, null, Phone.NUMBER + " LIKE ?", new String[] { "%" + contactNumber + "%" }, null);
但是,按电话号码搜索的最佳方法是使用专用PhoneLookup
API:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
getContentResolver().query(uri, new String[]{PhoneLookup.CONTACT_ID, PhoneLookup.DISPLAY_NAME}, null, null, null);
它经过优化以更快地返回结果,并处理带有或不带有国家/地区代码的电话号码。