【问题标题】:Android notification click resume activityAndroid 通知点击恢复活动
【发布时间】:2014-10-09 08:38:58
【问题描述】:

我的应用程序的状态栏中有一个通知。问题在于,当您从应用程序按下主页按钮(将其推到后台)时,然后按下从状态栏访问的列表中的通知,它开始一个新的活动副本。我想要做的就是恢复应用程序(比如当你长按主页按钮并按下应用程序的图标时)。有没有办法创建一个 Intent 来做到这一点?

【问题讨论】:

标签: android android-activity notifications


【解决方案1】:

在 AndroidManifest 中为您的活动声明 launchMode="singleInstance" 属性。 http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

相比之下,“singleTask”和“singleInstance”活动只能 开始一项任务。它们始终位于活动堆栈的根部。 此外,该设备在同一时间只能保存一个活动实例 时间——只有一项这样的任务。

【讨论】:

  • 这有点正确。但这会在用户单击主页按钮并单击应用图标以恢复应用时产生问题。
【解决方案2】:
private void startNotificationOnStatusBar() {
    try {
        Intent notificationIntent = new Intent(this, RadioPlayerActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Title")
            .setContentIntent(intent)
            .setPriority(2)
            .setContentText("Content text")
            .setAutoCancel(true);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    } catch (Exception e) {
    }
}

这是我没有修改 AndroidManifest 的代码。这是完美的工作。当用户点击通知时,它会继续而不执行 onCreate() 方法。

【讨论】:

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