【问题标题】:re-open background application via notification item通过通知项重新打开后台应用程序
【发布时间】:2010-06-02 18:32:07
【问题描述】:

我有一个带有标签和通知栏条目的应用, 当我将其发送到后台时(单击主页按钮) 并尝试通过单击重新打开应用程序 通知栏,应用程序重新启动(上次选择 选项卡丢失)。

当我在应用程序中按住主页按钮时 背景并从那里选择它或单击 应用程序在主屏幕上的图标,之前的状态是 默认恢复(选择了正确的选项卡)

IMO 通知的意图是错误的,但我是 不知道怎么解决。

简而言之:如何让后台应用程序返回 单击通知条目时的前台?

谢谢!

【问题讨论】:

  • 请注意,从通知中触发Intent 总是会创建Activity 的新实例;您不能直接返回到现有实例。在我的脑海中,我相信您可以使用 CLEAR_TOP 之类的标志之一(以及您的Activity 中的onNewIntent)来确保保留现有实例。

标签: android background notifications


【解决方案1】:

把这两行。这将恢复当前暂停的活动:

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

【讨论】:

  • 效果很好。但最好使用常量 notifyIntent.setAction(Intent.ACTION_MAIN); notifyIntent.addCategory(Intent.CATEGORY_LAUNCHER);
【解决方案2】:
Intent intent = new Intent(Application.getContext(), ActivityHome.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Application.getContext().startActivity(intent);

【讨论】:

    【解决方案3】:

    我也遇到过同样的问题,并努力寻找答案,但诀窍是:不要尝试使用通知意图以保存的状态重新启动应用程序,而是使用通知意图并在 onCreate( ) 活动的方法,只需完成()它。这将带您回到应用程序上上次查看的活动。

    【讨论】:

    • 天哪,我花了几个月的时间寻找解决方案,但这真是太棒了。这可能看起来像作弊,但很棒。
    【解决方案4】:
    public static boolean isApplicationRunningBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(am.getRunningAppProcesses().size());
        for (RunningTaskInfo runningTaskInfo : tasks) {
            if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                MyLog.i("UTIL", "packageName:" + runningTaskInfo.topActivity.getPackageName());
                MyLog.i("UTIL", "className" + runningTaskInfo.topActivity.getClassName());
                return true;
            }
        }
        return false;
    }
    
    Intent notificationIntent;
            if (Util.isApplicationRunningBackground(context)) {
                notificationIntent = new Intent(context, MainView.class);
            } else {
                notificationIntent = new Intent(context, Splash.class);
            }
    

    【讨论】:

      【解决方案5】:

      您是否按照lifecycle documentation? 中的建议实施 onSaveInstanceState 方法

      当您暂停应用程序并立即返回该应用程序时,该应用程序可能仍在后台的内存中挂起。但是,您不能依赖于此,因此您应该在每次进入后台时像当前打开的选项卡一样保存状态,并在重新激活时恢复它。

      【讨论】:

        【解决方案6】:

        在意图中使用两个标志

        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-22
          • 2018-01-13
          • 2012-02-24
          • 1970-01-01
          • 2017-11-21
          相关资源
          最近更新 更多