按(标准化)电话号码查询联系人

发布于 2024-11-18 19:49:52 字数 605 浏览 4 评论 0原文

我真的很惊讶我在任何地方都找不到答案。所以问题就在这里。

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....*/

当 params 中的电话号码格式为“777 777 7777”时,我没有得到任何记录。当它是“777-777-7777”时,它会获取记录。更糟糕的是 TelephonyManager.EXTRA_INCOMING_NUMBER 给我的号码为“7777777777”。如何查询这个电话号码?文档说 Phone.NUMBER 是用户输入它的方式。好吧,这对我没有任何好处!

I'm really surprised I can't find the answer anywhere. So here's the problem.

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....*/

When the phone number in params is formated as "777 777 7777" I get no records. When it is "777-777-7777" it get records. Even worse still the TelephonyManager.EXTRA_INCOMING_NUMBER gives me the number as "7777777777". How do I query for this phone number? The docs say Phone.NUMBER is how the user ENTERED it. Well, that doesn't do me any good!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-11-25 19:49:52

试试这个:

 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);

Try this :

 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();

Edited (According to Your Comment)

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文