1

I use this code to send a plain whatsapp text message from my app:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);

How can I perform a whatsapp video call from my application?

Mehdi
  • 765
  • 9
  • 19

1 Answers1

6

Assuming you already retrieved the contact number.

Step1: you need to fetch the corresponding whatsapp contact id from the contacts.

String contactNumber = "Your Contact Number"; // to change with real value

Cursor cursor = context.getContentResolver ()
    .query (
        ContactsContract.Data.CONTENT_URI,
        new String [] { ContactsContract.Data._ID },
        ContactsContract.RawContacts.ACCOUNT_TYPE + " = 'com.whatsapp' " +
            "AND " + ContactsContract.Data.MIMETYPE + " = 'vnd.android.cursor.item/vnd.com.whatsapp.video.call' " +
            "AND " + ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + contactNumber + "%'",
        null,
        ContactsContract.Contacts.DISPLAY_NAME
    );

if (cursor == null) {
    // throw an exception
}

long id = -1;
while (cursor.moveToNext()) {
    id = cursor.getLong (cursor.getColumnIndex (ContactsContract.Data._ID));
}

if (!cursor.isClosed ()) {
    cursor.close ();
}

Step2: You make the call using the whatsapp video intent.

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

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

Note: Obviously the querying code should be on a Background thread. The above is just a working summary of how to fire a whatsapp video call.

Oh, and don't forget to add the read contact permission

<uses-permission android:name="android.permission.READ_CONTACTS" />

and request it to your users at runtime as it's classified as a "dangerous" permission.

Mehdi
  • 765
  • 9
  • 19
  • @MajidRostamian remove all the spaces and all the `-`. Also make sure to remove the `+` at the beginning of the phone number. (don't forget to mark my answer as accepted if it worked for you :-)) – Mehdi Jun 27 '18 at 22:13
  • its not work.This error is displayed: whatsapp has stopped. – Majid Rostamian Jun 27 '18 at 22:34
  • It means you're passing wrong data. Put some logs in there and see the `id` you're sending. – Mehdi Jun 27 '18 at 22:37
  • @MajidRostamian hey! so you managed to integrate it in your code? – Mehdi Jul 02 '18 at 22:09
  • I'm getting `java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/data/999 typ=vnd.android.cursor.item/vnd.com.whatsapp.voip.call pkg=com.whatsapp cmp=com.whatsapp/.accountsync.CallContactLandingActivity }` Does it mean it's no longer exported? Any idea how to solve that? – Egal Mar 16 '20 at 17:33
  • @Yigal I am not sure, android has introduced many security restrictions in the past 2 years since android O I believe, regarding some permissions (telephony permissions being included), so that wouldn't surprise me. But I don't have an answer at the moment, sorry. – Mehdi Mar 16 '20 at 18:50
  • 1
    You should require permission: – AndroidCoolestRulest Sep 16 '20 at 07:31