簡體   English   中英

Android:如何通過電話號碼獲取聯系人ID?

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

我需要在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, 
        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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM