[英]Android : Check phone number present in Contact List ? (Phone number retrieve from phone call)
I make a BroadcastReceiver
to receive Phone number of the person who call me我制作了一个BroadcastReceiver
来接收给我打电话的人的电话号码
<intent-filter>
<action
android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
How to check if the phone number receive is on my contact list ?如何检查收到的电话号码是否在我的联系人列表中?
Do you have a tip to know if this phone number exist on contact list with out loading contact list ?您有什么提示可以在不加载联系人列表的情况下知道此电话号码是否存在于联系人列表中?
I don't want more information, just if this phone number exist.我不想要更多信息,只要这个电话号码存在。
If it's not possible, and I must load contact list, how to do it on BroadcastReceiver
?如果不可能,我必须加载联系人列表,如何在BroadcastReceiver
上执行此操作?
When I try to do getContentResolver
, it's not working because I'm on BroadcastReceiver
and not inside Activity
...当我尝试执行getContentResolver
,它不起作用,因为我在BroadcastReceiver
而不是在Activity
...
Thanks for your help谢谢你的帮助
public boolean contactExists(Context context, String number) {
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
cur.close();
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
}
我认为重要的是说您需要在清单文件中添加以下内容。
<uses-permission android:name="android.permission.READ_CONTACTS" />
for 1 you should have a look at the recommended ContactsContract.PhoneLookup provider对于 1 你应该看看推荐的ContactsContract.PhoneLookup provider
A table that represents the result of looking up a phone number, for example for caller ID.表示查找电话号码的结果的表格,例如来电显示。 To perform a lookup you must append the number you want to find to CONTENT_FILTER_URI.要执行查找,您必须将要查找的数字附加到 CONTENT_FILTER_URI。 This query is highly optimized.这个查询是高度优化的。
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor mycursor=resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
if (mycursor!=null && mycursor.moveToFirst()) {
// record exists
}
for 2 you can use the context
from the onReceive method to call methods that belong to Context对于 2,您可以使用 onReceive 方法中的context
来调用属于 Context 的方法
ContentResolver cr=context.getContentResolver();
I suggest you to use Phone.CONTENT_FILTER_URI
instead of PhoneLookup.CONTENT_FILTER_URI
because PhoneLookup can be empty and you will get no result from time to time (tested on LG-P500, froyo)我建议你使用Phone.CONTENT_FILTER_URI
而不是PhoneLookup.CONTENT_FILTER_URI
因为 PhoneLookup 可以为空并且你有时不会得到结果(在 LG-P500 上测试,froyo)
The problem on my device happens for example when:我的设备上的问题例如在以下情况下发生:
PhoneLookup.CONTENT_FILTER_URI
to query for a contact使用PhoneLookup.CONTENT_FILTER_URI
查询联系人Not all devices seems to be affected并非所有设备似乎都受到影响
Using PhoneLookup.CONTENT_FILTER_URI
the returned cursor is always empty.使用PhoneLookup.CONTENT_FILTER_URI
返回的游标始终为空。 Using Phone.CONTENT_FILTER_URI
everything is ok (you find the contact if any).使用Phone.CONTENT_FILTER_URI
一切正常(如果有的话,您可以找到联系人)。
Therefore I suggest you to always use Phone.*
Uris except when you really need to use PhoneLookup.*
... Which usually is just address book synchronization related stuff (and most of the times is not what you are interested in).因此,我建议您始终使用Phone.*
Uris,除非您确实需要使用PhoneLookup.*
... 这通常只是与地址簿同步相关的东西(而且大多数时候不是您感兴趣的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.