【问题标题】:Intent to resume a previously paused activity (Called from a Notification)意图恢复先前暂停的活动(从通知中调用)
【发布时间】:2011-07-11 23:58:28
【问题描述】:

我正在开发一个向用户显示通知的应用程序。通知的目的是让用户在用户处于另一个活动时更容易返回活动。我在我的应用程序中使用此代码来创建和显示通知。

                    notification = new Notification(R.drawable.icon,
                            "Notify",
                            System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "App name",
                            "App message",
                            PendingIntent.getActivity(
                                    this, 0,
                                    new Intent(this, Main.class),
                                    PendingIntent.FLAG_CANCEL_CURRENT));
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    nManager.notify(0, notification);

但是当用户点击通知时,会启动同一活动的新实例,而不是用户之前使用的那个。

我认为这与 PendingIntent 有关,但我找不到如何使该 Intent 恢复先前暂停的活动实例而不是创建新实例。

谢谢。

【问题讨论】:

  • 你找到解决这个问题的方法了吗?我发现的是一种解决方法:所有活动都扩展了一个基本活动来存储活动活动的数量(onCreate 为 +1,onDestroy 为 -1),并且通知启动了一个在新任务上运行的假活动,该活动检查正在运行的activity的数量,如果是>0,它只是关闭自己,如果是==0,它启动第一个activity。

标签: android android-activity notifications android-pendingintent


【解决方案1】:

我知道怎么做。我添加了以下代码:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

现在我的代码如下所示:

    notification = new Notification(R.drawable.icon,
            "Notify", System.currentTimeMillis());
    notification.setLatestEventInfo(this, "App name",
            "App message", PendingIntent.getActivity(this,
                    0, new Intent(this, Main.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

【讨论】:

  • Jimix,我尝试使用您的代码,但在我的情况下不起作用,这会打开 MainActivity 而不是最后一个集中的。可能我的错误在清单中,您如何设置 mainactivity ?
  • 这些代码应该可以工作。检查同一任务堆栈中的其他活动。它们可能涉及任务顺序。
【解决方案2】:

这对我有帮助android:launchMode="singleInstance"

<activity
        android:name="com.mosis.automatskidnevnik.MainActivity"
        android:launchMode="singleInstance"
        android:label="@string/app_name" >
        ...

【讨论】:

  • 如果您有多个活动,请避免使用此和单任务,这可以仅用于 1 个活动,但如果您需要恢复活动或有一些条件来启动它,请使用标志,问题在这里是如果您将来需要更多实例来发送额外内容或数据,这将阻止您这样做
【解决方案3】:

我通过设置解决了同样的问题

android:launchMode="singleTask"

【讨论】:

    【解决方案4】:

    在创建 PendingIntent 时,您使用:

    Main.this.getBaseContext()

    如果您想返回相同的活动,您应该使用默认活动上下文。在这里阅读更多:

    Android - what's the difference between the various methods to get a Context?

    【讨论】:

    • 我尝试在 PendingIntent 中使用带有“this”的默认上下文,但没有成功。
    【解决方案5】:

    除了@Jimix 的正确答案之外,我会将PendingIntent requestCode 从0 更改为非零 值,正如另一个answer 所评论的那样。另一方面,以前的 Android 版本有一个known bug,这让我花了几个小时的绝望。那里提出的解决方案对我在发送通知之前取消PendingIntent 效果很好:

    if (Build.VERSION.SDK_INT == 19) {
       getNotificationPendingIntent().cancel();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多