使用CountDown Timer或基于Android中的Handler自行滚动

使用CountDown Timer或基于Android中的Handler自行滚动,第1张

概述这个帖子: http://android-developers.blogspot.co.uk/2007/11/stitch-in-time.html 描述了如何使用Handler实现计时器.我可以使用CountDown计时器实现相同的功能: http://developer.android.com/reference/android/os/CountDownTimer.html 这将在正在播放音乐 这个帖子:
http://android-developers.blogspot.co.uk/2007/11/stitch-in-time.html

描述了如何使用Handler实现计时器.我可以使用CountDown计时器实现相同的功能:
http://developer.android.com/reference/android/os/CountDownTimer.html

这将在正在播放音乐的前台服务中运行,以便在指定的时间段后停止音乐(想想音乐椅).屏幕锁定时,时间需要可靠,我认为在前台服务中运行它应该确保这一点,是这种情况吗?

我从这篇文章中得知:
Why does CountDown Timer in Android use a Handler?

CountDown计时器是使用处理程序实现的,所以我的想法是它可能无关紧要,但在开始编码之前,我认为我会寻求人群的智慧!

谢谢,安德鲁

解决方法 我最终选择了一个Handler并且工作正常:

//required importimport androID.os.Handler;//class wIDe variable... are these consIDered bad? private Handler timeHandler = new Handler();//wherever in your code you want to begin the timer or reset ittimeHandler.removeCallbacks(updateTime);timeHandler.postDelayed(updateTime,100);    //this is the callback which will be called every 100ms or whatever value you gave in postDelayedprivate Runnable updateTime = new Runnable() {            public voID run() {                timeHandler.postDelayed(this,100);                currentTimeMillis = currentTimeMillis + 100;                //do whatever you need to do every 100ms here... or whenever currentTimeMillis reaches the value you're waiting for.        }

我添加了一个wakeLock来保持cpu唤醒,如果屏幕被锁定并且还使用了前台服务.在我迄今为止所做的测试中,它是准确的.

总结

以上是内存溢出为你收集整理的使用CountDown Timer或基于Android中的Handler自行滚动全部内容,希望文章能够帮你解决使用CountDown Timer或基于Android中的Handler自行滚动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1127756.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存