Send a message via Whatsapp programatically

Asked

Viewed 3,548 times

4

  • 1

    What errors did you get when trying these tutorials?

  • 1

    It’s more productive to publish the code you’re working on here.

  • Sorry for the delay. It just won’t. It doesn’t work. I run and nothing happens. The message doesn’t arrive at the destination, and make no mistake! The Code, is the same as the tutorial...

  • This is for php, but it may help: http://answall.com/questions/10110/script-que-sendingmessage-whatsapp

1 answer

3


Try this

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> resolvedInfoList = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolvedInfoList) {
    if (resolveInfo.activityInfo.packageName.startsWith(getString(R.string.whatsapp_package_name))) {
        intent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);        
        break;
    }
}
intent.setPackage(getString(R.string.whatsapp_package_name));//com.whatsapp
String text = "Texto a ser compartilhado";
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(intent);

You better check if the user has Whatsapp installed

  • I was able to resolve with Insert in the bank. Then I make a call in the process with.Hatsapp and it sends the messages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.