进行 WhatsApp 语音/视频通话 [英] Making a WhatsApp voice/video call

查看:50
本文介绍了进行 WhatsApp 语音/视频通话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解如何直接从应用程序拨打 whatsapp 电话(视频和语音).我读了这篇文章:android-make whatsapp call 但我不明白.我希望用户能够从他们的联系人列表中选择一个联系人,然后将他们带到带有两个按钮的屏幕:视频通话和语音通话.联系人的电话号码也将显示为顶部的文本视图.他们可以单击任一按钮,应用程序将拨打 whatsapp 电话.我不确定如何获取特定联系人的 ID 并调用它.

I'm trying to find out how I can make a whatsapp call (both video and voice) straight from the app. I read this post: android-make whatsapp call but I don't understand it. I want the user to be able to select a contact from their contact list and then they are brought to a screen with two buttons: Video Call and Voice Call. The contact's phone number will also be shown as a textview on top. They can click either one of the buttons and the app will make a whatsapp call. I'm not sure how I can get a specific contact's id and call that.

如果有人能以另一种方式解释它,我将不胜感激.

If anyone could explain it in another way, I'd be very grateful.

谢谢

推荐答案

我刚刚弄清楚这意味着什么,所以我想我会与你们分享这个,以防其他人也感到困惑.如果其中某些内容不是 100% 正确,我们深表歉意.

I've just figured out what it means so I thought I would share this with you guys in case anyone else was confused as well. Apologies if some of it is not 100% correct.

为了发送 whatsapp 电话/视频电话,您需要获取联系人的 ID.不只是任何 ID,具有适用于 whatsapp 的特定 mimetype 的 ID.这些 mimetypes 是 vnd.android.cursor.item/vnd.com.whatsapp.video.callvnd.android.cursor.item/vnd.com.whatsapp.voip.callem>

In order to send a whatsapp call/video call, you need to get the ID of the contact. Not just any ID, the ID with a specific mimetype that is suitable for whatsapp. These mimetypes are vnd.android.cursor.item/vnd.com.whatsapp.video.call or vnd.android.cursor.item/vnd.com.whatsapp.voip.call

要查询 ID,您必须使用内容解析器.ContactsContract.Data.CONTENT_URI 从联系人获取数据并将其发送回.您可以通过指定投影进一步减少它查询的内容.投影是您希望它返回的列,例如联系人姓名、电话号码.如果将其保留为 null,则不会过滤掉任何数据.它是可选的.如果您确实使用了投影,请确保包含要返回的以下 2 列:ContactsContract.Data._ID、ContactsContract.Data.DISPLAY_NAME、ContactsContract.Data.MIMETYPE.

To query the IDs, you have to use a content resolver. The ContactsContract.Data.CONTENT_URI gets the data from the Contacts and sends it back. You can further cut down what it queries back by specifying the projection. The projection are the columns you want it to return such as the name of the contact, the phone number. If you leave it as null, it won't filter any data out. It's optional. If you do use the projection, make sure that you include these 2 columns to be returned: ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.MIMETYPE.

// here is how to make a projection. you have to use an array. My example only returns the ID, Name of Contact and Mimetype. 

String[] projection = = new String[] {ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.Data.MIMETYPE};


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

光标取回信息后,您可以使用此代码进行浏览.当光标移动到下一个联系人时,它将 ID、显示名称和 mimetype 存储在 3 个单独的变量中.它使用 cursor.getColumnIndex() 来获取适当的列,然后使用 cursor.getLong() 来获取列的实际值.

After the cursor gets back the information you use this code to go through it. What is does is as the cursor moves to the next contact, it stores the ID, display name and mimetype in 3 separate variables. It uses the cursor.getColumnIndex() to get the appropriate column back and then the cursor.getLong() to get the actual value of the column.

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


                if (mimeType.equals("vnd.android.cursor.item/vnd.com.whatsapp.voip.call") || mimeType.equals("vnd.android.cursor.item/vnd.com.whatsapp.video.call")) {
                     // store in database

if (mimeType.equals("vnd.android.cursor.item/vnd.com.whatsapp.voip.call")) {
String voiceCallID = Long.toString(_id);

    }
    else{
    String videoCallID = Long.toString(_id);
    }

     }

   }
}

您还需要检查 mimetype 是 vnd.android.cursor.item/vnd.com.whatsapp.video.call(用于视频通话)还是 vnd.android.cursor.item/vnd.com.whatsapp.voip.call(用于语音通话)为此,我使用了一个 if 语句,如果它为真,则将其存储到您的数据库中.光标将穿过每个联系人的每个 mimetype.所以它将通过一次视频通话 mimetype 和一次语音通话 mimetype.

You also need to check if the mimetype is either the vnd.android.cursor.item/vnd.com.whatsapp.video.call (for video call) or vnd.android.cursor.item/vnd.com.whatsapp.voip.call (for voice call) For that I used an if statement, if it was true, store it into your database. The cursor will go through each mimetype of each contact. So it will go through the video call mimetype once and the voice call mimetype once.

然后为了whatsapp呼叫某人,检索语音或视频的ID并将其放入id参数中.确保它是正确的并且你调用了正确的方法,否则它将无法工作.

Then in order to whatsapp call someone, retrieve the ID for the voice or video and put it in the id parameters. Make sure it's correct and you call the right method otherwise it will not work.

    public void voiceCall(String id){
                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);

}

视频通话:

public void videoCall(String id){
                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.video.call");
                intent.setPackage("com.whatsapp");

                startActivity(intent);

}

就是这样!如果有任何错误或可以更简单解释的地方,请评论!

That's It! If there are any bits that are wrong or that could be explained simpler, comment!

这篇关于进行 WhatsApp 语音/视频通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆