How to set a timer in android

前端 未结 13 1847
天命终不由人
天命终不由人 2020-11-22 06:43

What is the proper way to set a timer in android in order to kick off a task (a function that I create which does not change the UI)? Use this the Java way: http://docs.ora

13条回答
  •  别那么骄傲
    2020-11-22 07:04

    this example start the timer unitl destroyed in Kotlin

    private lateinit var timerTask: TimerTask
    
     timerTask = object : TimerTask() {
            override fun run() {
                Log.d("KTZ", "$minutes:$seconds");
                timeRecordingLiveData.postValue("$minutes:$seconds")
                seconds += 1;
                if (seconds == 60) {
                    Log.d("KTZ", "$minutes:$seconds");
                    timeRecordingLiveData.postValue("$minutes:$seconds")
                    seconds = 0;
                    minutes += 1;
                }
            }
    
        }
    

    Cancel the timertask in onDestroy()

    timerTask.cancel()
    

提交回复
热议问题