Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking phone number exist in the contact list in android

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! error log

like image 327
Jesbin MJ Avatar asked Mar 22 '13 08:03

Jesbin MJ


1 Answers

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;
    }
}
like image 53
Linga Avatar answered Oct 04 '22 02:10

Linga