如何查找联系人的名字从Android上他们的电话号码? [英] How to look-up a contact's name from their phone number on Android?

查看:130
本文介绍了如何查找联系人的名字从Android上他们的电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个内容提供商从联系人数据库中获取发件人的姓名。

I am trying to get the sender's name from the contacts database using a content provider.

现在的问题是我不知道如何实现它。就像现在,我只能拉从smsMessage的电话号码。我需要检查,看看是否电话号码是呼叫是在用户第一次接触,如果它显示的名字,如果没有则显示该号码。

The problem is I don't know how to implement it. Like now I can only pull the phone number from the smsMessage. I need to check to see if the phone number that is calling is in the users contacts first and if it is display the name if it is not then display the number.

推荐答案

是的,这是可能使用 ContactsContract.PhoneLookup.CONTENT_FILTER_URI 中的Andr​​oid 2.0及更高版本, Contacts.Phones.CONTENT_FILTER_URL 在Android 1.6及更早版本。

Yes, this is possible using ContactsContract.PhoneLookup.CONTENT_FILTER_URI in Android 2.0 and higher and Contacts.Phones.CONTENT_FILTER_URL in Android 1.6 and earlier.

例如使用情况,请参阅文件<一个href="http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html">ContactsContract.PhoneLookup.以下摘录:

For example usage, see the documentation for ContactsContract.PhoneLookup. Excerpt below:

// Android 1.6 and earlier (backwards compatible for Android 2.0+)
Uri uri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(phoneNumber));

// Android 2.0 and later
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

// Query the filter URI
String[] projection = new String[]{ PhoneLookup.DISPLAY_NAME, ...
Cursor cursor = context.getContentResolver().query(uri, projection, ...


更新:电话号码的格式并不重要。比较稳健,高度优化的Andr​​oid上;它使用一个名为本地sqlite的函数来完成 PHONE_NUMBERS_EQUAL 。欲了解更多详情,<一个href="http://$c$csearch.google.com/$c$csearch#search/&q=package:android.git.kernel.org%20PHONE_NUMBERS_EQUAL">search在codeBase的这种方法。顺便说一句,我什么是安全的直接使用该函数在自己的应用程序还不能确定,但​​我不会。


UPDATE: The format of the phone number does not matter. Comparison is robust and highly optimized on Android; it's done using a native sqlite function named PHONE_NUMBERS_EQUAL. For more details, search the codebase for this method. By the way, I'm not certain if it's safe to use that function directly in your own apps, but I wouldn't.

这篇关于如何查找联系人的名字从Android上他们的电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆