0

I want my app to perform a task, in onCreate, once when the app is launched. But, in a random time interval. I want it to pick any time from 1-60 seconds.

currently I am using CountDownTimer

  new CountDownTimer(20000, 10000) {
        //currently time delay is 2000 milliseconds
        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // do your task here
                    Toast.makeText(getBaseContext(), "x", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onTick(long millisUntilFinished) {
        }
    }.start();

but this is pre-defined, not random. How do I make one?

1 Answers1

0

How about:

Handler handler = new Handler();
int delay = (int) (Math.random() * 59) + 1;
handler. postDelayed(myTask, delay * 1000);

anything from here should work, just replace the delay with the random one: How to call a method after a delay in Android

Community
  • 1
  • 1
JGS
  • 51
  • 6