进行 whatsapp 视频通话

Place a whatsapp video call

我使用此代码从我的应用程序发送纯 whatsapp 短信:

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

如何从我的应用程序执行 whatsapp 视频通话?

假设您已经检索到联系电话。

第一步:您需要从联系人中获取对应的whatsapp联系人id。

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

第 2 步: 您使用 whatsapp 视频意图拨打电话。

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

注意:显然查询代码应该在后台线程上。以上只是关于如何发起 whatsapp 视频通话的工作总结。

哦,别忘了添加读取联系人权限

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

并在运行时向您的用户请求它,因为它被归类为 "dangerous" 权限。