简体   繁体   中英

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

我需要在Android(API 1.6)甜甜圈中做到这一点

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM