2 回答
TA贡献1735条经验 获得超5个赞
我检查了你的代码,看起来不错,请参阅:
val sec2 = 10L
val counting: CountDownTimer =
object : CountDownTimer(TimeUnit.SECONDS.toMillis(sec2), 1000) {
override fun onTick(millisUntilFinished: Long) {
Log.i("alarm rings in", (millisUntilFinished / 1000).toString())
}
override fun onFinish() {
Log.i("alarm done", "alarm is ringing")
}
}
counting.start()
2019-09-09 00:06:57.791 1258-1654/? I/HwAlarmManagerService: hwSetAlarm listenerTag: *job.delay*
2019-09-09 00:06:59.740 26157-26157/com.unest.android.dev I/alarm rings in: 9
2019-09-09 00:07:00.004 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: time_tick
2019-09-09 00:07:00.741 26157-26157/com.unest.android.dev I/alarm rings in: 8
2019-09-09 00:07:01.742 26157-26157/com.unest.android.dev I/alarm rings in: 7
2019-09-09 00:07:02.742 26157-26157/com.unest.android.dev I/alarm rings in: 6
2019-09-09 00:07:03.743 26157-26157/com.unest.android.dev I/alarm rings in: 5
2019-09-09 00:07:04.744 26157-26157/com.unest.android.dev I/alarm rings in: 4
2019-09-09 00:07:05.745 26157-26157/com.unest.android.dev I/alarm rings in: 3
2019-09-09 00:07:06.746 26157-26157/com.unest.android.dev I/alarm rings in: 2
2019-09-09 00:07:07.748 26157-26157/com.unest.android.dev I/alarm rings in: 1
2019-09-09 00:07:08.749 26157-26157/com.unest.android.dev I/alarm rings in: 0
2019-09-09 00:07:09.232 26157-26157/com.unest.android.dev I/alarm done: alarm is ringing
2019-09-09 00:07:12.378 1258-1379/? W/AlarmManager: mIsScreenOn is: true, WAKEUP alarm trigger action = com.google.android.location.reporting.ACTION_UPDATE_WORLD package name is: com.google.android.gms
2019-09-09 00:07:12.378 1258-1379/? W/AlarmManager: mIsScreenOn is: true, WAKEUP alarm talarm.operation == null,package name is: android listenerTag is: *job.delay* creator uid is: 1000
2019-09-09 00:07:12.411 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: *job.delay*
可能你设定了 1 秒,但终点被解雇了。尝试增加 edittext 中的秒数或设置 1000 毫秒以上的不同间隔
TA贡献1858条经验 获得超8个赞
根据CountDownTimer 类的javadoc(参见其构造函数),millisInFuture 参数的值小于 countDownInterval 参数的值。所以onFinish是在onTick之前执行的。
添加回答
举报