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

前端之家收集整理的这篇文章主要介绍了Android:如何通过电话号码获取联系人ID?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在 Android(API 1.6)甜甜圈中做到这一点

解决方法

尝试这段代码,对我来说很好……
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);

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

猜你在找的Android相关文章