Android 通知重启应用但想恢复 [英] Android Notification restarts app but want to resume
问题描述
我已经能够为我的活动显示通知,以及当用户单击应用程序重新启动的通知.但是我只希望它重新出现而不是重新启动.例如.它是一个网络应用程序,我希望它在用户选择通知时出现在最前面......但不刷新网页.我可以捕获这个意图还是我发送了错误的意图?通常,如果我按下主页按钮并单击应用程序图标,应用程序就会出现并且不会刷新/重新启动.所以这就是我想要的行为.有什么想法吗?
Hi I have a been able to get a notification displayed for my activity, and when the user clicks the notification the app restarts. However I just want it to reappear not restart. eg. it is a web app and I want it to come to the front when the user selects the notification..but not refresh the web page. Can I trap this intent or am I sending the wrong intent? Normally if I press the home button and click on the app icon the app comes to the fore and doesn't refresh/restart. So this is the behaviour I want. Any ideas ?
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);
推荐答案
我把这个放在清单中
android:launchMode="singleInstance"
这为我解决了问题.还有 这个答案,我还没有尝试过,它暗示了其他有趣的事情.
This fixed the problem for me. Also this answer which I have not tried which allude to other things of interest.
这篇关于Android 通知重启应用但想恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!