Android清除从通知发送的intent

3

我的应用程序通过传递信息(状态栏中的挂起意图)来调用,但当我按下主屏幕按钮并通过长按主屏幕按钮重新打开我的应用程序时,它再次调用意图,并且相同的额外信息仍然存在...

有没有办法清除意图?或检查是否以前使用过?

Intent intent = new Intent(context, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("reportID", reportID);
    intent.putExtra("message", message);
    intent.putExtra("toast_show", true);
    intent.putExtra("detail_open", true);
    intent.putExtra("SplashActivity", true);
    PendingIntent pendingIntent = PendingIntent
            .getActivity(context.getApplicationContext(), 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
                            | PendingIntent.FLAG_ONE_SHOT);

    // Intent.FLAG_ACTIVITY_NEW_TASK
    /* get the system service that manage notification NotificationManager */
    NotificationManager notificationManager = (NotificationManager) context
            .getApplicationContext().getSystemService(
                    Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
            context.getApplicationContext())
            .setWhen(when)
            .setContentText(message)
            .setContentTitle(title)
            .setSmallIcon(smalIcon)
            .setAutoCancel(true)
            .setTicker(message)
            .setLargeIcon(largeIcon)
            .setDefaults(
                    Notification.DEFAULT_LIGHTS
                            | Notification.DEFAULT_VIBRATE
                            | Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

    /* Create notification with builder */
    Notification notification = notificationBuilder.build();

    notificationManager.notify(0, notification);

当我收到通知后就会出现问题。如果我从通知中启动应用程序,则会启动正确的屏幕,我可以使用该应用程序=>好的。但是当我退出应用程序(使用主页或返回按钮)时,它不再出现在“最近的应用程序”列表中。更准确地说:有人知道如何在从通知中启动应用程序后将其保留在“最近的应用程序”列表中吗?

请检查此链接。 http://stackoverflow.com/questions/11743184/android-notification-how-to-bring-the-activity-to-front-without-recall-the-act 也许它会在你的情况下起作用。 - kalpana c
你的问题解决了吗?我遇到了同样的问题。 - Ponting
@Ponting 是的,我解决了它。 - kyogs
3
你是说“你是如何解决的”? - Ponting
2个回答

0

更新此行并尝试

PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);

我也尝试了你的解决方案,但当我通过长按主页按钮启动应用程序时,仍然会获取所有数据,并且它将使用旧的意图数据重新启动..:( - kyogs

-1
你需要的是 PendingIntent.FLAG_CANCEL_CURRENT
如果所描述的 PendingIntent 已经存在,则在生成新 PendingIntent 之前会取消当前 PendingIntent。您可以使用此功能在仅更改 Intent 中的额外数据时检索新的 PendingIntent;通过取消先前的挂起意图,这可以确保只有给定新数据的实体才能启动它。
PendingIntent pendingIntent = PendingIntent.getActivity(
               context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

这将确保您最新的数据将出现在您的Intent中。


我的问题是,当我从通知中心打开活动并从意图中获取所有值时,现在我按下返回按钮,应用程序就会结束,但现在再次通过长按主页按钮打开应用程序,然后再次在此意图中获取所有数据,但我希望这些数据为空。 - kyogs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接