将应用程序带回到前面

11M*_*eys 13 android

我的应用程序是一个带报警类型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;
                    }
                }

          }
    }
Run Code Online (Sandbox Code Playgroud)

Kev*_*ker 5

对我来说是这样的:

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);
Run Code Online (Sandbox Code Playgroud)


pet*_*eri 0

请参阅http://developer.android.com/guide/appendix/faq/framework.html#4

\n\n

如何在启动之前检查活动是否已在运行?

\n\n

如果新活动未运行\xe2\x80\x94,则启动新活动,或者如果已在后台运行\xe2\x80\x94,则将活动堆栈置于前端,一般机制是在 startActivity 中使用 NEW_TASK_LAUNCH 标志() 称呼。

\n