【问题标题】:Bring application back to front将应用程序带回前面
【发布时间】: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


【解决方案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);

【讨论】:

  • 它重新创建了活动!销毁当前实例并创建一个新实例。
  • 您可以尝试删除 Intent.FLAG_ACTIVITY_NEW_TASK。
【解决方案2】:

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

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

如果一个新的 Activity 没有运行,或者如果它已经在后台运行,则将它放到最前面启动一个新的 Activity 的一般机制是在 startActivity() 调用中使用 NEW_TASK_LAUNCH 标志。

【讨论】:

  • 它应该如上所述发生,但它仍然无法正常工作。我启动我的应用程序,然后点击主页按钮,然后应用程序进入后台但仍在运行,2-3 分钟后我用 NEW_TASK_LAUNCH 标志触发了一个意图,但它从主入口点再次启动应用程序,而不是从它离开的地方
【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多