2

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.

Thanks

dthulke
  • 949
  • 9
  • 23
sixcookies
  • 357
  • 1
  • 7
  • 22

1 Answers1

7

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.

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

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

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

     }

   }
}

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.

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

}

For video call:

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!

sixcookies
  • 357
  • 1
  • 7
  • 22
  • A lot of what I did was learnt a combination from a bunch of different posts, websites and resources. I don't know if tis is the supposed way to do it but it worked for me. – sixcookies Mar 17 '19 at 13:03