checking phone number exist in the contact list in android

android, android-intent

Solution

Try this code:(use context)

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

Problem

i m trying to check the number exist in the contact list, i use the below code but i always getting error at this line ``` Cursor cur = this.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null); ``` can anyone help me to find out the problem, i had given the permission to read my contact in manifest ``` public String getContactName(String number) { String name = null; Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME }; Cursor cur = this.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null); try { if (cur.moveToFirst()) { name = cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)); return name; } } finally { if (cur != null) cur.close(); } return "unknown number"; } ``` this is my error log!

Original source