本文介绍了安卓:检查手机号码present在联系人列表中? (从电话通话电话号码retreive)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好
我做一个BroadcastReceiver接受谁给我打电话的人的电话号码

 <意向滤光器>
<作用
    机器人:名称=android.intent.action.PHONE_STATE/>
&所述; /意图滤光器>
 

1 /如何检查电话号码接收我的联系人列表中?
你有一个提示要知道,如果在联系人列表与出装载的联系人列表中存在这个电话号码?
我不想要更多的信息,只是如果这个电话号码存在。

2 /如果它是不可能的,我必须加载的联系人列表,该怎么办呢上的BroadcastReceiver?
当我尝试做getContentResolver,它不工作,因为我在BroadcastReceiver的,而不是内部的活动......

感谢您的帮助

解决方案

 公共布尔contactEx​​ists(上下文的背景下,串号){
///号码是在电话号码
乌里lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.en code(数));
的String [] mPhoneNumberProjection = {PhoneLookup._ID,PhoneLookup.NUMBER,PhoneLookup.DISPLAY_NAME};
光标CUR = context.getContentResolver()查询(lookupUri,mPhoneNumberProjection,NULL,NULL,NULL);
尝试 {
   如果(cur.moveToFirst()){
      返回true;
}
} 最后 {
如果(CUR!= NULL)
   cur.close();
}
返回false;
}
 

Hello
I make a BroadcastReceiver to receive Phone number of the person who call me

<intent-filter>
<action
    android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

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

2/if it's not possible, and i must load contact list, how to do it on BroadcastReceiver ?
When i try to do getContentResolver, it's not working because i'm on BroadcastReceiver and not inside 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()) {
      return true;
}
} finally {
if (cur != null)
   cur.close();
}
return false;
}

这篇关于安卓:检查手机号码present在联系人列表中? (从电话通话电话号码retreive)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 14:40