Android:从电话号码中检索联系人姓名
我想检索与来电号码关联的联系人姓名。当我处理广播接收器中的传入号码时,具有带有来电呼叫者姓名的字符串将极大地帮助我的项目。
我认为这涉及使用 sql WHERE 子句作为过滤器的查询,但我需要对联系人进行排序吗?一个例子或提示会有很大的帮助。
I would like to retrieve the name of a contact associated with an incoming telephone number. As I process the incoming number in the broascastreceiver having a String with the name of the incoming caller would help my project greatly.
I would think this involves a query using the sql WHERE clause as a filter, but do I need to sort the contacts? An example or hint would be of great assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然这个问题已经得到解答,但这里是从号码获取联系人姓名的完整功能。希望它能帮助其他人:
[根据 Marcus 的评论更新]
您必须请求此权限:
Although this has already been answered, but here is the complete function to get the contact name from number. Hope it will help others:
[Updating based on Marcus's comment]
You will have to ask for this permission:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
为此,您需要使用所描述的优化的 PhoneLookup 提供程序。
将权限添加到
AndroidManifest.xml
:然后:
For that you need to use the optimized PhoneLookup provider as described.
Add the permission to
AndroidManifest.xml
:Then:
这非常有帮助,这是我用于检索呼叫者的姓名、id 和照片的最终代码:
...然后只需使用“输入”(他们的照片作为输入流)、“姓名”和“contactId”执行您想要的操作。
以下是列出您有权访问的大约 15 列的文档,只需将它们添加到上面代码开头附近的投影即可:
http://developer.android.com/reference/android/provider/ContactsContract .PhoneLookup.html
This was very helpful, here's my final code for retrieving the caller's Name, id, and Photo:
... then just do whatever you want with "input" (their photo as an InputStream), "name", and "contactId".
And here are the docs listing the ~15 or so columns you have access to, just add them to the projection near the start of the code up above:
http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html
此版本与 Vikram.exe 的答案相同,带有避免 ANR 的代码
This version is the same as Vikram.exe's answer with code to avoid the ANR
通过以下方法传递您接到电话的联系号码。此方法将检查联系人是否保存在您的手机中。如果联系人已保存,则它将返回联系人姓名,否则将返回字符串未知数字
将此代码添加到您的广播接收器类中
以获取源代码访问 此链接
Pass the contact number from which you are getting the call in the following method. This Method will check whether the contact is saved in your mobile or not. If the contact is saved then it will return the contact name otherwise it return a string unknown number
Add this code in your Broadcast receiver class
to get Source code visit this link
为此,我们可以使用
PhoneLookup
提供程序通过手机号码获取姓名或联系方式。将权限添加到
AndroidManifest.xml
:在您的 Activity 中添加以下自定义 kotlin 方法,并使用所需的手机号码调用该方法。
有关更多信息: https://developer.android.com/training/contacts-provider /检索名称
For that, we can use the
PhoneLookup
provider to get the names or contact details using the mobile number.Add the permission to
AndroidManifest.xml
:Add the following custom kotlin method in your activity and call the same with the required mobile number.
For more: https://developer.android.com/training/contacts-provider/retrieve-names