首页 > 解决方案 > 进行whatsapp视频通话

问题描述

我使用此代码从我的应用程序发送普通的 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 视频通话?

标签: javaandroidandroid-intentwhatsapp

解决方案


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

Step1:您需要从联系人中获取相应的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" />

并在运行时向您的用户请求它,因为它被归类为“危险”权限。


推荐阅读