【问题标题】:Android notification bar open last active activity?Android通知栏打开最后一个活动活动?
【发布时间】:2010-11-03 16:24:25
【问题描述】:

我正在创建类似计时器的应用程序,当我启动计时器时,我可以选择转到 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);

}

【问题讨论】:

    标签: android android-intent android-activity android-notification-bar


    【解决方案1】:

    我找到了一个关于标志的答案: 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);
    

    【讨论】:

      【解决方案2】:

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

      我想您可以在 Intent 中添加一个额外的内容,以指定确切用于调用您的应用的通知。这有帮助吗?

      【讨论】:

      • 嗨,曼努埃尔我有这个代码,它几乎可以正常工作。唯一的问题是点击通知栏不会打开/显示以前启动的活动?它打开该活动的新实例?现在清楚了吗?有活动计时器,计时器启动我创建通知图标,我做一些其他应用程序,然后当我点击通知栏时,我需要打开一个计时器活动,时间滴答作响,而不是新活动的新实例:)
      • 在这种情况下,请查看活动的 launchMode 属性:developer.android.com/guide/topics/manifest/… 我想您想使用 singleTop。这有帮助吗?
      猜你喜欢
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多