Android:如何通过电话号码获取联系人ID?

时间:2011-06-27 16:03:35

标签: android contacts phone-number

我需要在Android(API 1.6)甜甜圈

中执行此操作

1 个答案:

答案 0 :(得分:20)

尝试这段代码,对我来说很好......

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