I am trying to open up a chat with the phone number (phone) by default when a user clicks a button. but it is not starting at all.
case R.id.nav_chat:
PackageManager packageManager = getApplicationContext().getPackageManager();
Intent whatsapp_intent = new Intent(android.content.Intent.ACTION_SEND);
try {
String phone = "123456789";
String message = "Hello, can you help with my issues?";
String url = "https://api.whatsapp.com/send?phone="+ phone +"&text=" + URLEncoder.encode(message, "UTF-8");
whatsapp_intent.setPackage("com.whatsapp");
whatsapp_intent.setData(Uri.parse(url));
if (whatsapp_intent.resolveActivity(packageManager) != null) {
getApplication().startActivity(whatsapp_intent);
}
} catch (Exception e){
e.printStackTrace();
}
break;
}
The answer to this question appears here originally https://stackoverflow.com/a/45821831/12632081