Due to security issues, it is NOT allowed and it is NOT possible to pre-select the user if you want to send a WhatsApp message from an Android application.
So the only thing possible is what you say, the application opens and the client must select the user who wants to send the message:
public void enviaMensajeWhatsApp(String msj) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, msj);
startActivity(Intent.createChooser(waIntent, "Compartir con:"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(this, "WhatsApp no esta instalado!", Toast.LENGTH_SHORT)
.show();
}
}
Executing the previous method:
enviaMensajeWhatsApp("Mi mensaje es abcdef 1234567890");
open the WhatsApp application and we have to select the user to send the message:
If you want to pre-select a user to send a message, you can do it using a Intent.ACTION_VIEW
, but you require that the defined number of the contact must be registered in your phone.
The number must contain the country code and the area code, starting with the "+" sign, for example my country Mexico +52
, area code for Mexico City
55
and my phone 1234567890
:
String msj = "Mi mensaje es abcdef 1234567890";
String numeroTel = "+52551234567890";
Intent intent = new Intent(Intent.ACTION_VIEW);
String uri = "whatsapp://send?phone=" + numeroTel + "&text=" + msj;
intent.setData(Uri.parse(uri));
startActivity(intent);
This will open the contact window to send and the message automatically, in this case also for security a human user has to activate the sending: