2

I've been searching the internet for quite a while now and still couldn't find a way to send a message automatically. So far what my app can does is open up the Whatsapp to a certain contact and write down the message I want to send. Here is my code :

This code is to check if Whatsapp is installed on the device

private boolean checkWA(String uri) {
        PackageManager packageManager = getPackageManager();
        boolean appInstalled;

        try {
            packageManager.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            appInstalled = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            appInstalled = false;
        }
        return  appInstalled;
    }

This code is to intent to Whatsapp (couldn't auto send message)

public void onClickWhatsApp() {

        boolean installed = checkWA("com.whatsapp");
        if (installed){
            String url = "https://api.whatsapp.com/send/?phone=6282167975500&text=" + "ADDON ERROR";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setPackage("com.whatsapp");
            i.setData(Uri.parse(url));
            startActivity(i);
        }
        else {
            String url = "https://api.whatsapp.com/send/?phone=6282167975500&text=" + "ADDON ERROR";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    }

Does anyone know how to do the auto send part? Thanks.

Prototype7
  • 31
  • 4
  • 1
    You probably can't. Intentionally - that'd be quite annoying /spammy / a security leak if any app can more or less silently (maybe some screen flashes) send a bunch of messages to random people without any chance for the operator of the phone to put a stop to it. – rzwitserloot Nov 30 '20 at 02:37
  • According to this answer https://stackoverflow.com/a/49654893/12982457 , it is possible. I just couldnt figure out how to use the code. – Prototype7 Nov 30 '20 at 03:10
  • That extensive answer both [A] explains how, and [B] highlights how you are using the accessibility API to do this. Android won't let you without your app asking for accessibility rights, which you'd then be abusing, which would get your app banned from the app store. So, no, you can't do this. – rzwitserloot Nov 30 '20 at 05:24
  • I am not planning to launch the app to any marketplace. This function will be used to send message to myself (on a different phone number) when my app detected a connection failure. – Prototype7 Nov 30 '20 at 06:37
  • Ah, so that was your problem. I suggest you use pushover instead. $5 app, one-off payment, and it has features to make this nicer: You can make your phone blare an alarm even if it is on silent, for example. It's all about having automated tools send you notifications. Alternatively, telegram has an API that lets any device (even servers) send messages (as a bot). If you want to stick with your whatsapp plan, the question you linked to is precisely waht you need to do. I don't think it's useful if I copy/pasted it. – rzwitserloot Nov 30 '20 at 13:49
  • I think the best option is to use Telegram. Not only it is free but also easy to use. – Prototype7 Dec 02 '20 at 07:38

0 Answers0