android-make whatsapp call

israelbenh picture israelbenh · Jul 29, 2016 · Viewed 15.1k times · Source

I want to make a WhatsApp call to a specific user. I tried this and it doesn't work:

Uri uri = Uri.parse("callto:" + phoneNUmber);
Intent i = new Intent(Intent.ACTION_CALL, uri);
i.setPackage("com.whatsapp");
startActivity(i);

I know how to create a WhatsApp message, the code is similar and it works:

Uri uri = Uri.parse("smsto:" + phoneNUmber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);

Answer

Adnan Khan picture Adnan Khan · Jul 30, 2016

Simple solution is, Query ContactContract.Data for the _id and MIME type.

ContentResolver resolver = context.getContentResolver();  
cursor = resolver.query(
            ContactsContract.Data.CONTENT_URI,
            null, null, null,
            ContactsContract.Contacts.DISPLAY_NAME);

//Now read data from cursor like 

while (cursor.moveToNext()) {
      long _id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data._ID));
      String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
      String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));

      Log.d("Data", _id+ " "+ displayName + " " + mimeType );

}

The output will be like the following

12561 Snow vnd.android.cursor.item/vnd.com.whatsapp.profile

12562 Snow vnd.android.cursor.item/vnd.com.whatsapp.voip.call

Now save in DB or somewhere else only those _Ids whose MIME type is vnd.android.cursor.item/vnd.com.whatsapp.voip.call

And then you initiate Whatsapp call with those contacts like this way

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);

// the _ids you save goes here at the end of /data/12562     
intent.setDataAndType(Uri.parse("content://com.android.contacts/data/_id"),
    "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
intent.setPackage("com.whatsapp");
 
startActivity(intent);