在android中存储倒计时器值
当倒计时器暂停时,您将如何存储该值? 然后当你想恢复计时器时最终会重新启动一个新的计时器 与此存储值。我很困惑如何保持价值。 我是java和android编程的初学者。 任何人都可以指导我吗?
As the Count Down timer pauses, how would you store the value?
which then when you want to resume the timer eventually will restart a new timer
with this stored value. I am so confused how to hold the value.
I am a beginner in java and android programming.
could any one guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Android 有内置的倒计时类,即 CountDownTimer 类。你可以用这个。
http://developer.android.com/reference/android/os/CountDownTimer.html
Android has inbuilt class for count down i.e. CountDownTimer class . You can use this.
http://developer.android.com/reference/android/os/CountDownTimer.html
倒计时器没有
pause()
方法。我能想到的解决方法是尝试在onTick()
获取时间。Countdown timer doesn't have a
pause()
method. Workaround that i can think off is try getting the time atonTick()
.那么您有一个显示倒计时 timr 的应用程序,并且它在暂停后从同一时刻继续倒计时?在这种情况下,您必须保存您的
onPause() 方法中某个位置的剩余时间(最佳位置将是共享首选项)
或者您是否希望有真正的倒计时时间并在到期后触发某些事件,即使您的应用程序处于非活动状态?然后在所需的时间安排闹钟,并将安排的时间存储在某处(共享首选项是理想的选择)。在这种情况下,您可以忘记自己计时,只需使用实时时间和存储的时间差定期更新 UI
So you have an application displaying count down timr, and like that it continues countdown from very same moment after being paused? In this case you have to save your
remaining time in some location in onPause() method ( best location would be shared preference )
Or do you like to have real countdown time and fire some event after it expires, even if your application is inactive? Then schedule an alarm at desired time, and store scheduled time somewhere (shared preferences would be ideal). In this case you can forget keeping time yourself, just update UI periodically using real time and stored time difference