跨多个活动的计时器
我有一个带有多项活动的 Android 应用程序。我需要在第一个活动开始时启动计时器,并在最后一个活动开始时结束计时器并显示经过的时间。我如何使用 Asynctask 来执行此操作。
i have an android app with several activities. I need to start a timer when the first activity starts and end the timer when the last activity starts and show the elapsed time. How can i use Asynctask to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子类
Application
,声明一个public static long started;
,它将由中的第一个 Activity 分配给
System.currentTimeMillis();
onCreate() 或 onResume()。让最后一个Activity
读取该值并计算增量。如果您绝对想使用 AsyncTask(我认为在这种情况下无关紧要),那么您至少需要一个中介服务。
Subclass
Application
, declare apublic static long started;
which will be assigned toSystem.currentTimeMillis();
by the first Activity inonCreate()
oronResume()
. Have the lastActivity
read the value and calculate delta.If you absolutely want to use
AsyncTask
(which I think is irrelevant in this case), you'll at least need a mediatoryService
for that.使其静态。
为什么要用定时器来计时?使用currentTimeMillis()会更好。
Make it static.
Why you use timer to count time? Will be better to use currentTimeMillis ().