18

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

sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
NicoMinsk
  • 1,716
  • 6
  • 28
  • 53

4 Answers4

35
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;
}
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
ekawas
  • 6,594
  • 3
  • 27
  • 39
  • 1
    I tried this code, and it works when I have the "exact" number, but depending on country code, other prefixes it doesn't always find the number, even when a number exists, and calling this number will call the same person (number in the contacts: 5556, number received: 15555215556) – Gavriel Mar 06 '16 at 06:17
  • unfortunately no – Gavriel Aug 31 '18 at 14:02
9

I think it's important to say that you need to add the following in your manifest file.

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
McDowell
  • 107,573
  • 31
  • 204
  • 267
kkudi
  • 1,625
  • 4
  • 25
  • 47
3

for 1 you should have a look at the recommended 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. 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

ContentResolver cr=context.getContentResolver();
Pentium10
  • 204,586
  • 122
  • 423
  • 502
3

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)

The problem on my device happens for example when:

  1. switch to airplane mode
  2. use the default message application to send a sms (will be queued).
  3. use PhoneLookup.CONTENT_FILTER_URI to query for a contact

Not all devices seems to be affected

Using PhoneLookup.CONTENT_FILTER_URI the returned cursor is always empty. Using Phone.CONTENT_FILTER_URI everything is ok (you find the contact if any).

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

Marco Bettiol
  • 531
  • 3
  • 12