Верните приложение обратно
Мое приложение - это таймер с приложением типа будильника.
Когда истечет таймер, я хотел бы вернуть активную активность приложения, если пользователь переместился, но приложение все еще работает.
Покажу, где я хотел бы поместить необходимый код:
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;
}
}
}
}
Ответы
Ответ 1
Для меня это было так:
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);
Ответ 2
См. http://developer.android.com/guide/appendix/faq/framework.html#4:
Как проверить, запущено ли действие, прежде чем запускать его?
Общий механизм запуска нового действия, если он не работает, или для переноса стека активности на фронт, если он уже запущен в фоновом режиме, - это использовать флаг NEW_TASK_LAUNCH в вызове startActivity().
Ответ 3
Вы должны поместить свой таймер в Service, таким образом, вы уверены, что Android не будет убивать ваше приложение, пока оно задний план. (Вы можете поместить весь свой класс MyCount в службу).
Из сервиса вы всегда можете перенести свое приложение (Activity) на передний план:
Intent intent = new Intent(getBaseContext(), TimerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(intent);