Majid Rostamian
Majid Rostamian

Reputation: 74

Place a whatsapp video call

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?

Upvotes: 1

Views: 3428

Answers (1)

Mehdi
Mehdi

Reputation: 775

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.

Upvotes: 6

Related Questions