-2

How can I send a whatsapp message to a number at a particular time automatically from my app? is there an android intent to it?

String toNumber="91XXXXXXXXXX";

    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_VIEW);

    try {
        String url = "https://api.whatsapp.com/send?phone="+ toNumber +"&text=" + URLEncoder.encode(whatsAppMessage, "UTF-8");
        intent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_STREAM, whatsAppImage);
        intent.setType("image/jpeg");
        intent.setPackage("com.whatsapp");
        intent.setData(Uri.parse(url));


        if(Calendar.HOUR==10){
            startActivity(intent);
        }

    }
    catch (Exception e){
        e.printStackTrace();
    }

1 Answers1

0

So, your question is really about how to execute some code at a particular time, not really about whatsapp?

The simple answer is to start a timer, have it fire every second, check the current time in the timer code. If the time is 22:00:00 (10PM) or 10:00:00 (10AM), call your function.

You don't want to just check Calendar.HOUR==10 because that will be true for every second of that hour, so, you may send out many more messages than you want to.

Keep in mind, this will only happen if the app is running, and possibly only if it is running the the foreground. If you need this to happen 24 hours a day, then you probably want to create some type of system service and have it send out the messages.

//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
         //Called each time when 1000 milliseconds (1 second) (the period parameter)

         // Get calendar set to the current date and time
         Calendar cal = Calendar.getInstance();
         // ensures we're using the same current time
         Calendar cal2 = cal;

        // Set time of calendar to 22:00:00.000
         cal.set(Calendar.HOUR_OF_DAY, 22);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);

        // Check if current time is after or before 22:00:00.000 today
        if ((cal2.after(cal)) || (cal2.before(cal)) {
           return;
        }

        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_VIEW);

        try {
            String url = "https://api.whatsapp.com/send?phone="+ toNumber +"&text=" + URLEncoder.encode(whatsAppMessage, "UTF-8");
            intent.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_STREAM, whatsAppImage);
            intent.setType("image/jpeg");
            intent.setPackage("com.whatsapp");
            intent.setData(Uri.parse(url));

            startActivity(intent);

        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • There is no way to send the messages without having to press the send button on whatsApp? This code just takes me to the chat but for the functionality of my app it has to be sending it automatically at a different time. – Pratham Majithia Jul 02 '19 at 18:43