Android:检查联系人列表中是否存在电话号码? (从电话中检索电话号码)

问题描述:

我制作了 BroadcastReceiver 来接收给我打电话的人的电话号码

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. 如何检查接收到的电话号码是否在我的手机上联系人列表?

    您是否有提示要知道此电话号码是否存在于联系人列表中而没有加载联系人列表?

    我不需要更多信息,就好像这部电话一样数字存在。

  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.

如果不可能,并且必须加载联系人列表,如何在 BroadcastReceiver $ c $上进行操作c>?

当我尝试执行 getContentResolver 时,它不起作用,因为我正在使用 BroadcastReceiver 不在 Activity ...

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

为您提供帮助

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;
}