12

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

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); 

回答

22

我把这个清单

android:launchMode="singleInstance" 

这个固定的问题为了我。我还没有尝试过的其他this answer暗示其他感兴趣的事情。

+0

完美的答案! – 2016-11-01 10:57:34

4

你可以试试这个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); 
+2

嗨,谢谢你回答我把这个在清单中,解决了我的问题android:launchMode =“singleInstance”也这些答案暗指其他感兴趣的东西.. http://stackoverflow.com/questions/4047683/android-how-以恢复应用程序从通知和http://stackoverflow.com/questions/4047683/android-how-to-resume-an-app-from-a-notification – ozmike 2011-04-10 12:44:30

7

如果你必须避免你的设置活动“singleInstance”,然后设置通知的意图如下:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
+0

这适用于我,谢谢m8;) – pkdkk 2013-05-30 06:32:54

0

我会建议设置标志Intent.FLAG_ACTIVITY_SINGLE_TOP对于这种特定情况下,如果使用机器人:launchMode =“singleInstance”当你调用其他意图在应用程序中,使用Facebook的SDK例子,它会影响行为的PayPal SDK等。

原因很简单就在这里: link

+0

FLAG_ACTIVITY_SINGLE_TOP已被列为答案 - 谢谢你一样。 – ozmike 2017-02-27 04:21:51

相关问题