Android Intent to call Whatsapp from code

前端 未结 3 1295
暖寄归人
暖寄归人 2021-01-28 14:29

I am using this code to call WhatsApp directly from my Call Logs app. This works well, but only if the phone number includes a valid country code. For example calling WhatsApp

相关标签:
3条回答
  • 2021-01-28 14:58

    WhatsApp for an specific phone number

    val whatsAppIntent = Intent(Intent.ACTION_VIEW)
    val encodedText = URLEncoder.encode("Helo World", "UTF-8")
    whatsAppIntent.data = Uri.parse("http://api.whatsapp.com/send?phone=$phoneNumber&text=$encodedText")
    
    0 讨论(0)
  • 2021-01-28 15:05
      Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/html");
            sharingIntent.setPackage("com.whatsapp");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<p>https://play.google.com/store/apps/details?id=" + context.getPackageName() + "</p>"));
            context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
    
    0 讨论(0)
  • 2021-01-28 15:10

    Here is the complete solution. May be useful for people who are trying the same from their apps. Thanks to Sourav Ganguly for the link.

    android-make whatsapp call

    1. Retreive the ID for the selected Contact.
    2. Get all the RAW Contact Id's for the contact id from ContactsContract.RawContacts
    3. Query the ContactsContract.Data table for all Raw Contacts and retrieve the column ContactsContract.Data._ID
    4. Since a contact may have several phone numbers active on WhatsApp, you may want to check additionally the column ContactsContract.Data.DATA3 to filter out the phone number you want to match
    5. To start a WhatsApp conversation use vnd.android.cursor.item/vnd.com.whatsapp.profile and vnd.android.cursor.item/vnd.com.whatsapp.voip.call if you want to start a WhatsApp call
    6. Use the ID from step 3 to start WhatsApp. Here is sample code:

      String data = "content://com.android.contacts/data/" + dataId;
                  String type = "vnd.android.cursor.item/vnd.com.whatsapp.profile";
                  Intent sendIntent = new Intent();
                  sendIntent.setAction(Intent.ACTION_VIEW);
                  sendIntent.setDataAndType(Uri.parse(data), type);
                  sendIntent.setPackage("com.whatsapp");
      startActivity(sendIntent);
      
    0 讨论(0)
提交回复
热议问题