我的應用程序是一個鬧鐘類型的應用程序的計時器。 當計時器到期時,如果用戶已離開,但該應用程序仍在運行,我希望將應用程序的主要活動重新放入視圖中。帶回應用程序
我告訴我哪裏想放置必要的代碼:
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;
}
}
}
}
?我的觀點是,我想你想把你的計時器變成一項服務。然後,您可以發送您的應用程序在定時器完成時正在監聽的廣播。更簡單,更清潔! – codinguser 2010-12-11 11:16:59
我需要閱讀的東西..新的android,我的第一個想法是:如果應用程序被殺害,什麼保持計數..?我知道如何保存當前狀態,但沒有任何用處,因爲定時器會暫停。你的解決方案聽起來像我應該探索的路線,任何我應該尋找/尋找的指針,讚賞。 – 11Monkeys 2010-12-11 11:27:38
考慮使用android AlarmManager http://developer.android.com/intl/de/reference/android/app/AlarmManager.html,而不是跟蹤自己的時間,至少在您收到onPause呼叫後使用它。這將爲用戶節省大量的電池和CPU使用量。 – Janusz 2010-12-11 12:56:07