Android 在从通知面板重新启动之前关闭打开的应用程序?

Android 在从通知面板重新启动之前关闭打开的应用程序?,android,android-intent,android-notifications,Android,Android Intent,Android Notifications,我正在使用Intent从通知面板重新启动应用程序,但在启动新实例之前,无论应用程序处于何种状态,如何关闭现有的相同打开的应用程序 任何想法/建议。使用必须使用一些标志,这可能会对你有所帮助 Intent notificationIntent = new Intent(context, MainActivity.class); // set intent so it does not start a new activity notificationIntent.setFlags

我正在使用Intent从通知面板重新启动应用程序,但在启动新实例之前,无论应用程序处于何种状态,如何关闭现有的相同打开的应用程序


任何想法/建议。

使用必须使用一些标志,这可能会对你有所帮助

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

    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;

    notificationManager.notify(0, notification);
同时检查您的舱单以了解

android:launchMode="singleTop"

属性。创建启动应用程序的
意图时,只需添加
意图。标记\u活动\u清除\u顶部|意图。标记\u活动\u新任务


注意:但是,只有当您的根
活动
仍然存在于任务中时(即:您没有调用
finish()
在您的任务中启动任何其他
活动时,请在其上。

我使用了Intent.FLAG_Activity_CLEAR_TOP | Intent.FLAG_Activity_SINGLE_TOP..但它是一样的…打开的活动保持原样…n新的Intent Activity get lauchThanks…将尝试一下..但有了它,应用程序正在玩多个场景:)你说的“多个场景”是什么意思?通知可以在应用程序的任何屏幕上发出,因此很难保持状态…比如说,如果控制在某个屏幕上,那么它应该在那里,如果它在屏幕B..上,那么它应该像这样重定向到B..的父级。我回答中的代码将从任务中删除所有活动,并基本上从头开始重新启动应用程序。无论应用程序的当前状态如何。这不是你想要的吗?Heyy David…正如你提到的“只有当你的根活动仍然存在于任务中时,这才有效”…因此,使用此应用程序时,还有一些其他情况。