【发布时间】:2015-09-03 00:34:31
【问题描述】:
我的应用程序是一个带有闹钟类型应用程序的计时器。 当计时器到期时,如果用户已导航离开,但应用仍在运行,我希望重新显示应用的主要活动。
我显示我想在哪里放置必要的代码:
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
/** code here to bring the app back to the front / top of stack, in its current state */
timeDisplay.setText("Expired.!");
ReminderFlashingScreen.setVisibility(View.GONE);
statustab.setBackgroundResource(R.drawable.redtab);
seeker.setProgress(0);
reset.setBackgroundResource(R.drawable.resetbtnoff);
playExpiredAlarm();
alarmAnimation.start();
flasher.setVisibility(View.VISIBLE);
StopAlarmButtonAnimation.start();
StopAlarmButton.setVisibility(View.VISIBLE);
expired = true;
}
public void onTick(long millisUntilFinished) {
remindertimepref = prefs.getString("reminderTime", "<unset>");
remindtime = Integer.parseInt(remindertimepref.trim());
timeDisplay.setText(formatTime(millisUntilFinished));
seeker.setProgress((int) (millisUntilFinished / 1000 / 60 ) + 1);
if (used == true){
reminder_time = ((int) (millisUntilFinished / 1000));
if (reminder_time == remindtime){
reminderalarm();
used = false;
}
}
}
}
【问题讨论】:
-
如果您的用户导航到内存更密集的地方并且系统决定终止您的应用程序怎么办?我的意思是,我认为您想将 Timer 放入服务中。然后,您可以在计时器完成时发送您的应用正在侦听的广播。更简单、更清洁!
-
我需要阅读的东西.. android 新手,我的第一个想法是:如果应用程序被杀死,那么保持计数..?我知道如何保存当前状态,但没有任何作用,因为计时器会暂停。您的解决方案听起来像是我应该探索的路线,任何关于我应该搜索/调查的指针,不胜感激。
-
考虑使用android AlarmManager developer.android.com/intl/de/reference/android/app/… 而不是自己记录时间,至少在收到onPause 调用后使用它。这将为用户节省大量电池和 CPU 使用率。
-
谢谢大家...您的意见给了我探索的途径。
-
@codinguser 如果“系统决定终止您的应用程序”,那么您的服务也会被终止。这就是为什么@Janusz 建议使用
AlarmManager是一个更好的建议。即使应用程序被终止,它仍然可以重新启动。
标签: android