2013-12-10 42 views
9

對於從我的地址簿中給定的號碼,我需要查找該號碼是否啓用了whatsapp。 (這個想法是選擇短信/ WhatsApp發起一個文本意圖)如何檢查android電話簿上的聯繫人是否啓用了whatsapp?

可以說,我有一個聯繫人下的兩個數字,我需要知道哪一個已啓用whatsapp。

Nexus 4上的「People」應用程序顯示了兩個聯繫電話號碼, 並且稍後還有一個CONNECTIONS部分,該部分僅顯示WhatsApp可能的聯繫人。

有沒有一種查找方式(比如People應用程序的功能)?

回答

11

如果你想知道,如果這種接觸有WhatsApp的:

String[] projection = new String[] { RawContacts._ID }; 
String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)"; 
String[] selectionArgs = new String[] { "THE_CONTACT_DEVICE_ID", "com.whatsapp" }; 
Cursor cursor = activity.getContentResolver().query(RawContacts.CONTENT_URI, projection, selection, selectionArgs, null); 
boolean hasWhatsApp = cursor.moveToNext()); 
if (hasWhatsApp){ 
    String rowContactId = cursor.getString(0) 
} 

,並尋找到該聯繫人的號碼有WhatsApp的

projection = new String[] { ContactsContract.Data.DATA3 }; 
selection = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? "; 
selectionArgs = new String[] { "vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId }; 
cursor = CallAppApplication.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, "1 LIMIT 1"); 
String phoneNumber = null; 
if (cursor.moveToNext()) { 
    phoneNumber = cursor.getString(0); 
} 
+2

什麼是「THE_CONTACT_DEVICE_ID」? –

+0

來自聯繫人表的用戶的contact_id – idog

0

使用@的idog的方法,我提高了代碼工作更輕鬆。 contactID是一個要傳遞的字符串變量。如果聯繫人沒有WhatsApp返回null,否則返回contactID已作爲變量傳遞。

public String hasWhatsapp(String contactID) { 
    String rowContactId = null; 
    boolean hasWhatsApp; 

    String[] projection = new String[]{ContactsContract.RawContacts._ID}; 
    String selection = ContactsContract.Data.CONTACT_ID + " = ? AND account_type IN (?)"; 
    String[] selectionArgs = new String[]{contactID, "com.whatsapp"}; 
    Cursor cursor = getActivity().getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null); 
    if (cursor != null) { 
     hasWhatsApp = cursor.moveToNext(); 
     if (hasWhatsApp) { 
      rowContactId = cursor.getString(0); 
     } 
     cursor.close(); 
    } 
    return rowContactId; 
} 
相關問題