Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to get the contact id by the phone number?

I need to do it in Android (API 1.6)doughnut

like image 813
user811985 Avatar asked Jun 27 '11 16:06

user811985


1 Answers

try this piece of code, worked fine for me...

ContentResolver contentResolver = context.getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID};

Cursor cursor =  
   contentResolver.query(
        uri, 
        projection, 
        null, 
        null, 
        null);

if(cursor!=null) {
  while(cursor.moveToNext()){
    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
    String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
    Log.d(LOGTAG, "contactMatch name: " + contactName);
    Log.d(LOGTAG, "contactMatch id: " + contactId);
  }
  cursor.close();
}
like image 139
GalDude33 Avatar answered Oct 15 '22 15:10

GalDude33