我的应用程序是一个带闹钟类型app的计时器。 当计时器到期时,我想将应用程序的主要活动恢复到视图中,如果用户已导航,但应用程序仍在运行。
我展示了我想放置必要代码的位置:
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;
}
}
}
}
答案 0 :(得分:4)
对我来说就是这样:
Intent intent = new Intent(Application.getContext(), AbstractFragmentActivity.instance.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.getContext().startActivity(intent);
答案 1 :(得分:1)
你应该把你的计时器放在Service中,这样你就可以确保Android在后台运行时不会杀死你的应用程序。 (您可以将整个MyCount类放在服务中)。
从服务中,您始终可以将您的应用程序(活动)带到前面:
Intent intent = new Intent(getBaseContext(), TimerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);
答案 2 :(得分:0)
请参阅http://developer.android.com/guide/appendix/faq/framework.html#4:
如何在启动活动之前检查活动是否已在运行?
如果新活动未运行,或者如果已经在后台运行活动堆栈,则启动新活动的一般机制是在startActivity()调用中使用NEW_TASK_LAUNCH标志。