java – How to make a WhatsApp call from an Android app

Question:

I found the answer in the open spaces of en-SO, in the comments it says that the example is working. I try to do the same in my application, but the first time I try, it seems like WhatsApp starts and immediately closes. On subsequent attempts, nothing happens at all, the only reaction is a line in the logs:

V / WhatsApp: Total WhatsApp Contacts: 17

I do it like this:

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = ContactUriHelper.getUriFromPhoneNumber(contact.getPhone().getNumber(), getApplicationContext());
    intent.setDataAndType(Uri.parse("content://com.android.contacts/data/2021"), //Uri.parse("content://com.android.contacts/data/_id")
            "vnd.android.cursor.item/vnd.com.whatsapp.voip.call");
    intent.setPackage("com.whatsapp");
    startActivity(intent);

Uri tried to transmit both in a string and by the uri object itself – nothing was winterized.

Uri i get like this

 public static Uri getUriFromPhoneNumber(String phoneNumber, Context context) {
        Uri uri = null;
        String contactId = getContactIdByPhoneNumber(phoneNumber, context);
        if (!TextUtils.isEmpty(contactId)) {
            Cursor cursor = context.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data._ID},
                    ContactsContract.Data.DATA2 + "=? AND " + ContactsContract.Data.CONTACT_ID + " = ?",
                    new String[]{"Viber", contactId}, null);
            if (cursor != null) {
                Log.wtf(TAG, cursor.toString());
                while (cursor.moveToNext()) {
                    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID));
                    if (!TextUtils.isEmpty(id)) {
                        uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id);
                        Log.d(TAG, "URI: " + uri.toString());
                        break;
                    }
                }
                cursor.close();
            }
        }
        return uri;
    }

    private static String getContactIdByPhoneNumber(String phoneNumber, Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        String contactId = null;
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

        String[] projection = new String[]{ContactsContract.PhoneLookup._ID};

        Cursor cursor =
                contentResolver.query( uri, projection, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
            }
            cursor.close();
        }
        Log.wtf(TAG, "ContactID: " + contactId);
        return contactId;
    }

What could be the problem?

Answer:

The link says that you need to filter records by the mimetype "vnd.android.cursor.item/vnd.com.whatsapp.voip.call"
those.

public static Uri getUriFromPhoneNumber(String phoneNumber, Context context) {
 Uri uri = null;
 String contactId = getContactIdByPhoneNumber(phoneNumber, context);
 if (!TextUtils.isEmpty(contactId)) {
  Cursor cursor = context.getContentResolver().query(
   ContactsContract.Data.CONTENT_URI, new String[] {
    ContactsContract.Data._ID
   },
   ContactsContract.Data.MIMETYPE + "=? AND " + ContactsContract.Data.CONTACT_ID + " = ?",
   new String[] {
    "vnd.android.cursor.item/vnd.com.whatsapp.voip.call",
    contactId
   }, null);
  if (cursor != null) {
   Log.wtf(TAG, cursor.toString());
   while (cursor.moveToNext()) {
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Data._ID));
    if (!TextUtils.isEmpty(id)) {
     uri = Uri.parse(ContactsContract.Data.CONTENT_URI + "/" + id);
     Log.d(TAG, "URI: " + uri.toString());
     break;
    }
   }
   cursor.close();
  }
 }
 return uri;
}
Scroll to Top