Android 通知栏打开最后一个活动活动?
我正在创建类似计时器应用程序,当我启动计时器时,我可以选择转到 android Home 或启动任何其他活动。
当我启动计时器时,我设置了一个通知栏图标,如果我使用其他应用程序(意味着从启动的计时器活动开始),现在我需要通过单击通知图标返回到之前启动的计时器活动???
当我单击时,我将启动一个新的实例计时器活动,而不是之前启动的计时器活动! ,如果我单击后退按钮,它会显示以前的计时器活动。
问题是:如何通过通知栏调用以前启动的活动,而不是启动该活动的新实例?
这是我的代码示例如下:
private void notificationBar()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ico;
CharSequence tickerText = "some title...";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "some app title";
CharSequence contentText = "...some info !";
Intent notificationIntent = new Intent(this, main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIF_ID, notification);
}
private void notificationClose(int notifID)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(notifID);
}
I am creating like timer application and when I start timer I have option to go to android Home or start any other activity .
When I start timer I set a notification bar icon and if i use some other application (mean go from started timer activity) and now I need to go to back to my previously started timer activity by clicking on notification icon ???
When I click I am starting a new instance timer activity , not the previously started timer activity ! , and if I then click back button it show me a previously timer activity ..
Question is: How to call previously started activity trough notification bar , not to start new instance of that activity ??
This is sample of my code below :
private void notificationBar()
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ico;
CharSequence tickerText = "some title...";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "some app title";
CharSequence contentText = "...some info !";
Intent notificationIntent = new Intent(this, main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIF_ID, notification);
}
private void notificationClose(int notifID)
{
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.cancel(notifID);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个关于标志的答案:
Android:new Intent() 使用 android:launchMode="singleTop 启动新实例”
I found an answer it's about flags :
Android: new Intent() starts new instance with android:launchMode="singleTop"
我不确定我是否明白你的意思。
我想您可以在意图中添加一个额外的内容来指定究竟使用哪个通知来调用您的应用程序。这有帮助吗?
I'm not certain I understand what you mean.
I suppose you could add an extra in the intent to specify which notification exactly was used to invoke your app. Does this help at all?