如何通过单击通知恢复之前的活动
当我的通知消失时,我想恢复放入后台的活动,而不是启动新活动。我已经看到了一些关于使用 FLAGS 的答案,但我不知道如何实现它
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);
我应该把它放在代码中的哪里?我尝试过,但没有成功。请帮忙!
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.icon;
tickerText = "Short Msg";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "MyApp";
contentText = "Reopen App";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationIntent = new Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
When my notification goes off I want to restore the activity that was put into the background, not start a new activity. I've seen some answers about using FLAGS but I don't know how to implement it
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | INTENT.FLAG_ACTIVITY_SINGLE_TOP);
Where do I put this in my code? I tried but it didn't work. Please help!
ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.icon;
tickerText = "Short Msg";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
context = getApplicationContext();
contentTitle = "MyApp";
contentText = "Reopen App";
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationIntent = new Intent(this, StartTimer.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了,在 Android Manifest 中将 Activity 设置为 SingleTop 或 SingleInstance,然后它不会创建新的 Activity,而是重新打开仍处于活动状态的 Activity。
Figured it out, set the Activity to SingleTop or SingleInstance in Android Manifest, then instead of creating a new activity it just reopen the one still active.
请注意,标记为正确的答案并不完全正确,因为“singleTop”仍然可能在某些条件下创建您的活动的多个实例。
真正保证在任何条件下创建活动的唯一实例的启动模式是“singleTask”和“singleInstance”。
这两个选项为您的活动创建一个且唯一的任务,作为任务的根,区别在于“singleInstance”不允许您的活动之上有其他活动,而“singleTask”则允许。
来源:
http://developer.android.com/guide/topics/manifest /activity-element.html#lmode
Note that answer marked as correct is not completely correct as "singleTop" still may create multiple instances of your activity under certain conditions.
The launch modes that really are garanteed to create a UNIQUE instance of your activity at any condition are "singleTask" and "singleInstance".
These two options creates one and only task for your activity being the root of the task, with the difference that "singleInstance" don't allow other activities on top of yours, while "singleTask" does.
Source:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode