2013-12-10 14 views
9

Per un dato numero dalla mia rubrica, ho bisogno di cercare se il numero ha WhatsApp abilitato. (L'idea è di scegliere SMS/WhatsApp per l'inizializzazione di un testo)Come verificare se un contatto sulla rubrica di Android ha abilitato whatsapp?

Diciamo, ho due numeri sotto un contatto, E ho bisogno di sapere quale è abilitato a whatsapp.

L'app "Persone" sul Nexus 4 mostra entrambi i numeri di contatto, E anche un po 'più in basso ha una sezione COLLEGAMENTI, che mostra solo il possibile contatto di WhatsApp.

C'è un modo per cercare (come l'app di People)?

risposta

11

Se vuoi sapere se questo contatto ha 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) 
} 

E per trovare a quale numero di questo contatto ha 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

Che cos'è "THE_CONTACT_DEVICE_ID"? –

+0

contact_id dell'utente dalla tabella dei contatti – idog

0

Usando @ il metodo di idog, ho migliorato il codice per lavorare Più facile. contactID è una variabile stringa da passare. Se il contatto non ha WhatsApp restituisce null, altrimenti restituisce con contactID che è stato passato come variabile.

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; 
} 
Problemi correlati