Android通知重新启动应用程序,但希望继续

Android通知重新启动应用程序,但希望继续,android,background,notifications,foreground,android-pendingintent,Android,Background,Notifications,Foreground,Android Pendingintent,嗨,我已经能够得到一个通知显示我的活动,当用户 单击应用程序重新启动的通知。然而,我只是希望它重新出现,而不是重新启动。这是一个web应用程序,我希望它在用户选择通知时出现在最前面,但不刷新网页。我可以捕捉这个意图吗?还是我发送了错误的意图?通常,如果我按下home(主页)按钮并单击应用程序图标,应用程序就会出现,并且不会刷新/重新启动。这就是我想要的行为。有什么想法吗 String ns = Context.NOTIFICATION_SERVICE; NotificationManager

嗨,我已经能够得到一个通知显示我的活动,当用户 单击应用程序重新启动的通知。然而,我只是希望它重新出现,而不是重新启动。这是一个web应用程序,我希望它在用户选择通知时出现在最前面,但不刷新网页。我可以捕捉这个意图吗?还是我发送了错误的意图?通常,如果我按下home(主页)按钮并单击应用程序图标,应用程序就会出现,并且不会刷新/重新启动。这就是我想要的行为。有什么想法吗

 String ns = Context.NOTIFICATION_SERVICE;
 NotificationManager mNotificationManager = (NotificationManager)
                                             getSystemService(ns);
 //2.Instantiate the Notification
 int icon = R.drawable.notification_icon;
 CharSequence tickerText = "My App";  // temp msg on status line 
 long when = System.currentTimeMillis();
 Notification notification = new Notification(icon, tickerText, when);
 notification.flags |= Notification.FLAG_ONGOING_EVENT;
 //3.Define the Notification's expanded message and Intent: 
 Context context = getApplicationContext();
 CharSequence contentTitle = "Notification";
 CharSequence contentText = "My app!"; // message to user
 Intent notificationIntent = new Intent(this, HelloAndroid2.class);
 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
                                                          notificationIntent, 0);
 notification.setLatestEventInfo(context, contentTitle, contentText,
                                                          contentIntent);
 //4.Pass the Notification to the NotificationManager:  
 final int NOTIFICATION_ICON_ID = 1;
 mNotificationManager.notify(NOTIFICATION_ICON_ID, notification);

您可以尝试将此标志\u活动\u重新排序\u到\u前端(文档准确描述了您想要的内容)

我把这个放在舱单上了

android:launchMode="singleInstance"

这为我解决了问题。另外,我还没有尝试过提及其他感兴趣的内容。

如果您必须避免将活动设置为“singleInstance”,请按如下方式设置通知的意图:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

我建议在这种特殊情况下设置flag Intent.flag\u ACTIVITY\u SINGLE\u TOP,如果您使用android:launchMode=“singleInstance”,它会影响您在应用程序中调用其他Intent时的行为,例如使用facebook sdk、paypal sdk等

这里的解释很简单:

我有另一个解决方案:

    //Resume or restart the app (same as the launcher click)
    val resultIntent = Intent(context, MyLauncherActivity::class.java)
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER)
    resultIntent.setAction(Intent.ACTION_MAIN)
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    val pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
    builder.setContentIntent(pendingIntent)  //builder is the notificationBuilder

从.MainActivity活动部分的清单中删除此项(如果有):

android:noHistory="true"

这是致命的,因为无法在三星设备(以及其他设备)上用Android更新版7恢复应用程序

嗨,谢谢你的回答,我把这个问题放在清单中,修复了我的问题Android:launchMode=“singleInstance”这些回答也暗示了其他感兴趣的事情。。而且FLAG_ACTIVITY_SINGLE_TOP已经被列为一个答案-谢谢。
android:noHistory="true"