3

So my goal is to click a button, disable it and start a timer, once the timer is up enable the button. Simple right? You would do something like this.

button1.onClick { 

button1.setEnabled(false);
    new CountDownTimer(60000, 1000) { //Set Timer for 5 seconds
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                   button1.setEnabled(true);
            }
        }.start()

}

However.. If the user closes the app while the timer is running the button will be enabled again, restarting the timer. so instead of having to wait for 60 seconds the user can just close the app and open it within 10 seconds.

So my question is, how do I disable the button for 60 seconds and keep it disabled even if the user closes and opens the app until 60 seconds has passed?

Mark Denom
  • 987
  • 1
  • 8
  • 24
  • 1
    You need to put CountDownTimer inside a Service, follow this link to know how to do it https://stackoverflow.com/questions/32028134/how-to-keep-a-countdowntimer-running-even-if-the-app-is-closed? – Bach Vu Feb 11 '19 at 04:00

2 Answers2

2

You have to persist that information within a data store that keeps it even when the app is switched off.

One way to do that would be to use https://developer.android.com/training/data-storage/shared-preferences

You have to get a timestamp when starting the timer, or compute the "end time" for the timer. You then save that information, and whenever the app starts up, you first check if your preferences contain such a time stamp. And if so, you check whether you are still in that "timed" window.

Thr key things to remember: you have to remove the persisted information when the timer is up, and: if somebody changes the system clock in the mean time, you got a problem, too. Dealing with that is possible, but requires more effort.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I tried doing it that way but I didn't have any success, is it possible for you to write a simple example? – Mark Denom Feb 11 '19 at 03:54
  • If you have "not working code" consider putting up a [mcve] in your question. – GhostCat Feb 11 '19 at 03:59
  • Yeah I'm sorry I would but I scrapped that part a few hours ago. – Mark Denom Feb 11 '19 at 03:59
  • How do I put a timestamp in the shared preferences? Do I do it as a string? – Mark Denom Feb 11 '19 at 04:04
  • I am sorry, but I don't have access to an android setup, my answer is on the "conceptual" side of things. I would go step by step: first write + read sample data to shared preferences, then write a timestamp. And so on. But probably that other answer about the service already solves your issue. – GhostCat Feb 11 '19 at 04:04
-3

Try this with Handler.

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btn.setEnabled(false);

         new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                    btn.setEnabled(true);
                    Log.d(TAG,"resend1");

            }
        },10000);// set time as per your requirement 
        }
    });
shekhar pande
  • 1,172
  • 1
  • 11
  • 21