7

I have phone number. Is there any way to check whether the phone number exists in contacts database in the device or not? Depending on that I need have move further in my app. Please suggest or if any one can have sample code snippet please provide.

The below is the code I wrote:

public boolean contactExists(Activity _activity, String number) {
    String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
    Cursor cur = _activity.getContentResolver().query(number, mPhoneNumberProjection, null, null, null);
    try {
        if (cur.moveToFirst()) {
            return true;
        }
    } finally {
        if (cur != null)
            cur.close();
    }
    return false;
}// contactExists

Thanks in Advance...

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
  • hi @user1306412 in my case this function always return false, i do as answeres,you give me some suggestion in it ? – Nirav Mehta Aug 20 '14 at 11:43

3 Answers3

23
public boolean contactExists(Activity _activity, String number) {
    if (number != null) {
        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
        Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
        try {
            if (cur.moveToFirst()) {
                return true;
            }
        } finally {
            if (cur != null)
                cur.close();
        }
        return false;
    } else {
        return false;
    }
}// contactExists

Handled nullpointer exception.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Ishu
  • 5,357
  • 4
  • 16
  • 17
  • heloo @Ishu my condition always gone on false, i checked if it reurn true i insert record otherwise not but it all time return false if not duplicate then also – Nirav Mehta Aug 20 '14 at 11:09
  • @NiravMehta - I suggest that you don't go copying other peoples' code without trying to understand it. – Stephen C Jun 26 '15 at 13:26
  • @Ishu - dealing with a null argument like that is a bad idea. It is better to let the NPE happen, and then when it does track down where / why you are calling `contactExists` with a `null` argument. It is always better to fix the root cause bug than to hide it. – Stephen C Jun 26 '15 at 13:29
  • How can I check contact by _ID? This code always returns `false` if parameter `number` assigned as _ID. – Konstantin Konopko Nov 01 '17 at 18:58
3

A minor change in your code :: You need to have lookupUri..

public boolean contactExists(Activity _activity, String number) {
    Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
    Cursor cur = _activity.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
    try {
        if (cur.moveToFirst()) {
            return true;
        }
    } finally {
        if (cur != null)
            cur.close();
    }
    return false;
}// contactExists
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Ishu
  • 5,357
  • 4
  • 16
  • 17
0

I tried the code above on an ice cream device (SIII) and it didnt work so after some search i ended up creating this method (which is working nicely )

    private boolean isContact(String incommingNumber) {
        Cursor cursor =null;
        String name = null;
        try {
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incommingNumber));
             cursor = MainService.this.getContentResolver().query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
            }
        } finally {
            if(cursor!=null){
                cursor.close();
            }
        }
        return Util.hasValue(name);
    }
A.Alqadomi
  • 1,529
  • 3
  • 25
  • 33