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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我把它放在清单中
这解决了我的问题。另外这个答案我没有尝试过,它暗示了其他有趣的事情。
I put this in manifest
This fixed the problem for me. Also this answer which I have not tried which allude to other things of interest.
如果您必须避免将 Activity 设置为“singleInstance”,请按如下方式设置通知的意图:
If you have to avoid setting your activity to "singleInstance", then set the notification's intent as follows:
您可以尝试这个 FLAG_ACTIVITY_REORDER_TO_FRONT (该文档准确描述了您想要的内容)
http: //developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
我这里有另一个适合我的解决方案:
I have another solution working for me here :
我建议在这种特殊情况下设置标志 Intent.FLAG_ACTIVITY_SINGLE_TOP ,如果您使用 android:launchMode="singleInstance" ,它会影响您在应用程序中调用其他意图时的行为,例如使用 facebook sdk、paypal sdk 等
。这里的解释很简单:
链接
I would recommend to set the flag Intent.FLAG_ACTIVITY_SINGLE_TOP for this particular situation, if you use android:launchMode="singleInstance" it would affect the behaviour when you call other intents in your application, example using facebook sdk, paypal sdk etc..
The explanation is simple right here:
link
如果您有的话,请从 .MainActivity 活动部分的清单中删除此内容:
这对于无法在 Android 版本高于 7 的三星设备(和其他设备)上恢复应用程序来说是致命的
Remove this from manifest from .MainActivity activity portion if you have it there:
This is fatal for not being able to resume app on Samsung devices (and others) with Android newer then 7
我尝试了上述方法,但没有成功(应用程序不断重新启动),直到我删除:
intent.setPackage(this.getPackageName());
我最近添加了它,看看是否可以修复通知问题Android 14。现在我正在使用:
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP)
返回到正在运行的计时器,并且它仍然按预期运行。
I had tried the above with no success (App kept restarting) until I removed:
intent.setPackage(this.getPackageName());
which I'd recently added to see if I could fix notification issues with Android 14. Now I'm using:
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP)
to return to a timer that's running, and it is happily still running as desired.