1

Well, I am trying to create a CountDownTimer for my app. I already searched on the internet but couldn't find a good solution for my problem.

This Timer should keep running after the user cloese the app or switched to another. So I will need a Service class I think. Then the Timer should be connected to a TextView which refreshes every second. It should count down from 30:00 to 00:00.

Any help appreciated :D

Tester
  • 139
  • 2
  • 8
  • 2
    Possible duplicate of [How to run CountDownTimer in a Service in Android?](https://stackoverflow.com/questions/22496863/how-to-run-countdowntimer-in-a-service-in-android) – Rishabh Maurya May 27 '17 at 18:20
  • this already has an answer here :https://stackoverflow.com/questions/22496863/how-to-run-countdowntimer-in-a-service-in-android – Rishabh Maurya May 27 '17 at 18:21
  • Thank you very much Rishabh :) I already found that post but thought was confused because I read something about a Service class. – Tester May 29 '17 at 20:35

2 Answers2

0

you could use a thread for it in your service

 new Thread(new Runnable() {
    public void run() {
       //do stuff here
            }
        });
    }
}).start();

or visit this or this for more info.

Simo
  • 345
  • 4
  • 12
0

You don't need any type of service here. CountDownTimer class automatically runs on a different thread and gives the callback in UI thread. CountDownTimer creates a thread on it's own.

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        binding.nameTxt.setText("seconds remaining: " + millisUntilFinished / 1000);
        //here you can have your logic to set text to edittext
        }

    public void onFinish() {
        binding.nameTxt.setText("done!");
        //here you can have your logic to set text to edittext when the timer is stopped.
    }
}.start();
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
  • Thank you very much :D I have already seen this video https://www.youtube.com/watch?v=ZqqP69rJVmg but as I read something about the Service class for background services I was confused because the findViewById command doesn't work in a Service. Thank you very much again :) – Tester May 29 '17 at 20:37
  • Hi again. Could you show me the complete Service class? "new CountDownTimer" gives out an error.. Sorry, I am quite new in Service classes. And how is it possible to connect the EditText of my MainActivity with this Service? Thank you very much :) – Tester Jun 09 '17 at 15:16