Android Notification重新启动应用但想要恢复

ozm*_*ike 18 notifications android background foreground android-pendingintent

嗨,我已经能够显示我的活动的通知,当用户点击通知时,应用程序重新启动.但是我只是希望它重新出现而不是重启.例如.它是一个Web应用程序,我希望它在用户选择通知时出现在前面..但不刷新网页.我可以捕获这个意图还是我发错了意图?通常,如果我按下主页按钮并单击应用程序图标,应用程序就会出现并且不会刷新/重新启动.所以这就是我想要的行为.有任何想法吗 ?

 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);
Run Code Online (Sandbox Code Playgroud)

ozm*_*ike 27

我把它放在清单中

android:launchMode="singleInstance"
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题.同时这个答案,我还没有试过这暗示其他感兴趣的东西.


And*_*Dev 10

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

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Run Code Online (Sandbox Code Playgroud)


Waz*_*_Be 5

你可以试试这个 FLAG_ACTIVITY_REORDER_TO_FRONT (文档准确地描述了你想要的)

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT

Intent notificationIntent = new Intent(this, HelloAndroid2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)

  • 嗨,感谢您的回答我把它放在清单中,为我解决了这个问题 android:launchMode="singleInstance" 这些答案也暗示了其他感兴趣的事情.. http://stackoverflow.com/questions/4047683/android-how-to -resume-an-app-from-a-notification 和 http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification (2认同)