如何查找联系人的名字从Android上他们的电话号码?他们的、电话号码、联系人、名字

由网友(窒息的瞬间)分享简介:我想用一个内容提供商从联系人数据库中获取发件人的姓名。I am trying to get the sender's name from the contacts database using a content provider.现在的问题是我不知道如何实现它。就像现在,我只能拉从smsMessage的电话号码。...

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

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.

例如使用情况,请参阅文件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 。欲了解更多详情,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.

阅读全文

相关推荐

最新文章