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