【问题标题】:Android: Notification to bring Activity to front with ViewpagerAndroid:使用 Viewpager 将 Activity 置于最前面的通知
【发布时间】:2014-05-14 00:09:44
【问题描述】:

我的 Android 应用由一个 Activity 组成,其中包含两个通过选项卡访问的片段(我使用的是 ViewPager)。

在 Fragment #2 中,通过按钮创建通知。例如,如果用户使用主页按钮将应用程序发送到后台,则此按钮启动需要继续的后台任务。

我希望用户能够在单击通知时将应用程序返回到前面,就像使用应用程序图标一样。这是我的代码:

Notification.Builder mBuilder =
    new Notification.Builder(getActivity())
    .setSmallIcon(R.drawable.ic_stat_av_play)
    .setContentTitle("MyApp")
    .setContentText("A background task is running");

Intent notificationIntent = new Intent(getActivity(), MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);


PendingIntent notificationPendingIntent = PendingIntent.getActivity(
    getActivity(), 0, notificationIntent, 0);

mBuilder.setContentIntent(notificationPendingIntent);
mNotificationManager = (NotificationManager) getActivity().getSystemService(
    Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());

很遗憾,单击通知会关闭当前正在运行的应用程序并重新启动它。

如您所见,我已经尝试过这里提出的解决方案:Resume application and stack from notification,但它不起作用。许多类似的问题也建议使用这些标志中的一个或多个:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_REORDER_TO_FRONT

我发现这些都对我的通知行为没有任何影响。我是否缺少一些东西来使这项工作按预期进行?

编辑:

也许这会帮助你帮助我。问题似乎确实来自应用程序结构。

如果我替换这一行:

Intent notificationIntent = new Intent(getActivity(), MainActivity.class);

这个:

Intent notificationIntent = new Intent(getActivity(), Fragment2.class);

如果在应用程序仍在前台时单击通知行为将是正确的(即通知抽屉将自行关闭)但是,如果我使用主页按钮返回主屏幕,然后单击通知,什么都没有发生。

如果我使用第一个意图定义(使用 MainActivity.class),则没有任何标志对意图有任何影响。这也适用于清单中定义的 launchMode。

编辑 2

已添加解决方案。我不太确定它为什么起作用,所以任何额外的输入都将不胜感激!

【问题讨论】:

    标签: android android-fragments notifications


    【解决方案1】:

    我用这个方法,问题就解决了。希望这对您也有帮助。

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setClass(this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    

    【讨论】:

    • 这是迄今为止唯一对我有用的东西。可能值得注意的是,我仍然需要Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP。
    【解决方案2】:

    你可以试试这个,

    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
    

    在你的清单中,

    android:launchMode="singleTask"
    

    用于您的 MainActivity。

    【讨论】:

    • 这不起作用。看起来我使用的标志由于某种原因无法识别。无论我尝试什么,我的通知行为都保持不变。
    • 一试删除,这两行代码,notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);并只放我定义的内容。
    • 可能是,我假设这个错误,将 android:launchMode="singleTask" 放在 MainActivity 的 标记中。不在您的 标记中。我以前犯过这个错误。
    • launchMode 位于正确的位置,在清单中的活动定义内!
    【解决方案3】:

    所以,我终于成功了!这是最终的代码,希望对遇到类似情况的人有所帮助。

    mBuilder =
        new Notification.Builder(getActivity())
        .setSmallIcon(R.drawable.ic_stat_av_play)
        .setContentTitle("MyApp")
        .setContentText("A background task is running.")
        .setOngoing(true);
    
    notificationIntent = new Intent(Intent.ACTION_MAIN);
    notificationIntent.setClass(getActivity().getApplicationContext(), MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    
    notificationPendingIntent = PendingIntent.getActivity(getActivity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    mBuilder.setContentIntent(notificationPendingIntent);
    
    mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    
    mNotificationManager.notify(1, mBuilder.build());
    

    我还必须在清单中设置这个启动模式:

    android:launchMode="singleTop"
    

    【讨论】:

      【解决方案4】:

      你可以试试,也许对你有帮助!

      Intent intent = new Intent(Intent.ACTION_MAIN);
      
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
      
      intent.setClass(this, Main.class);
      
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2012-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多