Search code examples
androidkotlintimer

Is there a way to check to see if a timer is running with the Timer Java class?


I want to check and see if my Timer is still running after my Android app crashes or closes. If it is not running I would like to create a new timer. I set a name for the timer so is there a function to get the Timer by name?

In the onCreate() function of the MainActivity I would check to see if the timer is running and if not, I would just call the function to create a new one.

private fun createTimer(){
        Timer("SayHello",false).schedule(startTime.time){
          Log.d(TAG, "hello")
        }
}

Solution

  • is there a function to get the Timer by name

    No. They aren't stored anywhere to get them from, unless you do that yourself. Ok, technically you could find the thread by name because each Timer creates a thread, but you couldn't get back the timer object from it.

    But if "Android app crashes or closes", any timers it created certainly aren't running.