我真的很惊讶我无法在任何地方找到答案。所以这就是问题所在。
ContentResolver resolver = getContentResolver();
String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
String where = Phone.NUMBER+"=?";
String[] params = new String[] {"777 777 7777"};
Cursor cursor = resolver.query(Phone.CONTENT_URI, columns, where, params, null);
while (cursor.moveToNext()) { /* So on....*/
当参数中的电话号码格式为“777 777 7777”时,我没有记录。当它是“777-777-7777”时,它会得到记录。更糟糕的是,TelephonyManager.EXTRA_INCOMING_NUMBER给我的号码为“7777777777”。如何查询此电话号码?文档说Phone.NUMBER是用户输入它的方式。嗯,这对我没有任何好处!!!
答案 0 :(得分:5)
试试这个:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
if(cs.getCount()>0)
contactName=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
cs.close();
已编辑(根据您的评论)
String phoneNumber = "777 777 7777";
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cursor = resolver.query(uri, columns, null, null, null);