Tried everything on Stack Overflow, now i am being able to take the msg frwd. But the msg is not sent until you actually click a no. from whatsApp contact list ..... Plz help been stuck here for few days.... Thing is that i am trying to build an app, in which when I give a number then msg should be sent to it using whatsapp. With this code from Satheesh's answer here msg is being frwded and when I select a number it sends the pre-defined msg to it. But I want that the msg should be directly sent without whatsApp waiting for user to click on a number..
//checks if whats app is installed or not..
private boolean whatsappInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
//Main Process
boolean isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp");
if (isWhatsappInstalled) {
String a="91*********0";// the nuber to which msg is to be sent
Uri uri = Uri.parse("smsto:" + a);
Intent sendIntent = new Intent(Intent.ACTION_SEND, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test message"+a);//msg to be sent
//sendIntent.putExtra("sms_body", "ydyeryyerf");
sendIntent.setType("text/plain");// type of msg->text
sendIntent.putExtra("chat",true);
sendIntent.setPackage("com.whatsapp");// picks whats app
// startActivity(sendIntent);
startActivity(Intent.createChooser(sendIntent, a));//starts whats app
} else {
// should redriect to play store to download whatsApp
Toast.makeText(getApplicationContext(), "WhatsApp not Installed",
Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("market://details?id=com.whatsapp");
Intent playStore = new Intent(Intent.ACTION_VIEW, uri);
startActivity(playStore);
}