[英]How to make a repeating multiple interval timer in Android?
I have searched through Google, SO and Android developers but I need some further help for decision making.我已经通过 Google、SO 和 Android 开发人员进行了搜索,但我需要一些进一步的决策帮助。 This has been discussed in discrete subjects but I'd like to have a bit of help with insight/design-decisions.
这已经在离散主题中进行了讨论,但我想对洞察力/设计决策有一些帮助。
What I'd like to accomplish is a kind of timer that plays an alarm after, say 45 minutes, and plays an alarm, say 15 minutes after that.我想要完成的是一种计时器,它会在 45 分钟后播放闹钟,然后在 15 分钟后播放闹钟。 When the last alarm has fired, the cycle restarts indefinitely.
当最后一个警报触发时,循环无限期地重新开始。 This would sequentially look like Start -> 45min countdown -> Alarm -> 15min countdown -> Alarm -> go back to 45min countdown to repeat cycle.
这将依次看起来像开始 -> 45 分钟倒计时 -> 闹钟 -> 15 分钟倒计时 -> 闹钟 -> 返回 45 分钟倒计时以重复循环。
This is an attempt at constructing a Work/Break-cycle app but I'm having both specific problems as well as probable design-flaws.这是构建工作/休息周期应用程序的尝试,但我遇到了特定问题和可能的设计缺陷。
My goal, in short:我的目标,简而言之:
Currently I'm (in general, there are a few specifics that differ) using a Service that implements a Runnable with a delay matching the duration of the different timers.目前我(一般来说,有一些不同的细节)使用一个服务,该服务实现了一个 Runnable,其延迟与不同计时器的持续时间相匹配。 When the first delay is up (after 45 minutes), another Runnable is initialized and run (set to 15 minutes).
当第一个延迟结束时(45 分钟后),另一个 Runnable 被初始化并运行(设置为 15 分钟)。 This works pretty well.
这很好用。 I make use of a partial wakelock for the Service and most of the times get this to run for about a cycle, but then things silently seem to stop.
我为服务使用了部分唤醒锁,并且大多数时候让它运行大约一个周期,但随后事情似乎无声无息地停止了。 If I close the application and start it again, the cycle restarts.
如果我关闭应用程序并再次启动它,循环将重新开始。
Now, apart from this I'd like to update a UI with the amount of minutes left on each timer, which means I have to somehow, regularly, make "posts" to the UI in order to see a visual progress.现在,除此之外,我想使用每个计时器剩余的分钟数来更新 UI,这意味着我必须以某种方式定期向 UI 发布“帖子”,以便查看视觉进度。
Am I going about this the right way?我会以正确的方式解决这个问题吗? Should I have another approach on this and what would be a suitable one?
我应该对此有另一种方法吗?什么是合适的方法?
Use the AlarmManager
to schedule alarms, passing in whatever Intent
you'd like to occur at the the time you've scheduled the alarm for.使用
AlarmManager
来安排警报,在您安排警报的时间传递您希望发生的任何Intent
。 When 45 minute alarm goes off, schedule another alarm and when that one goes off, schedule another 45 minute alarm, and so on.当 45 分钟闹钟响起时,安排另一个闹钟,当那个闹钟响起时,安排另一个 45 分钟闹钟,依此类推。 EDIT
编辑
To update the UI each minute to tell how much time is left do something like this in your Activity:要每分钟更新 UI 以判断还剩多少时间,请在您的 Activity 中执行以下操作:
public class MyActivity extends Activity {
int minutes = 45;
TextView tv;
public void onCreate(Bundle state) {
super.onCreate(state);
//set the content, grab your textview, etc...boilerplate
Handler handler = new Handler();
//schedule alarm here and post runnable as soon as scheduled
handler.post(r)
}
//somewhere else in your Class
Runnable r = new Runnable() {
@Override
public void run() {
if(!minutes == 0)
tv.setText(minutes--);
handler.postDelayed(this, 60000); //run the runnable in a minute again
else
handler.removeCallbacks(this);
}
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.