Android 通知栏打开最后一个活动活动?

发布于 2024-09-30 15:00:46 字数 1424 浏览 2 评论 0原文

我正在创建类似计时器应用程序,当我启动计时器时,我可以选择转到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-10-07 15:00:47

我找到了一个关于标志的答案:
Android:new Intent() 使用 android:launchMode="singleTop 启动新实例”

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

I found an answer it's about flags :
Android: new Intent() starts new instance with android:launchMode="singleTop"

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
好久不见√ 2024-10-07 15:00:47

我不确定我是否明白你的意思。

我想您可以在意图中添加一个额外的内容来指定究竟使用哪个通知来调用您的应用程序。这有帮助吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文