1
votes

I' d like to send a whatsapp message by clicking on a button to a number that comes from the Android Activity (that in turn fetches from a server). The number to which I have to send a new is NOT an existing contact on my phone. I know how to open Whatsapp app from my app. The following piece of code deals with opening whatsapp from an Adapter:

Intent sendIntent = new Intent();
sendIntent.setPackage("com.whatsapp");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

this code opens Whatsapp but I don't know how to pass it the number to which I have to send the message

1
Check if the answer I posted below meets your need.Alok Nair
1. You need add number in your Contact list, 2. Refresh WhatsApp contacts, thats the only work around for this situation.NullByte

1 Answers

0
votes

Try this

public void onClickWhatsApp(View view) {

    PackageManager pm=getPackageManager();
    try {

        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "YOUR TEXT HERE";

        PackageInfo info=pm.getPackageInfo("com.whatsapp",     PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code 
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

   } catch (NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
   }  

}