代码之家  ›  专栏  ›  技术社区  ›  Nezir

安卓通知栏打开上次活动?

  •  5
  • Nezir  · 技术社区  · 14 年前

    我正在创建类似计时器的应用程序,当我启动计时器时,我可以选择去Android主页或启动任何其他活动。

    当我启动计时器时,我设置了一个通知栏图标,如果我使用其他应用程序(意味着从已启动的计时器活动开始),现在我需要通过单击通知图标返回到以前启动的计时器活动。???

    当我单击时,我正在启动一个新的实例计时器活动,而不是以前启动的计时器活动!,然后如果我单击后退按钮,它会显示以前的计时器活动。

    问题是:如何通过通知栏调用以前启动的活动,而不是启动该活动的新实例??

    以下是我的代码示例:

    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);
    
    }
    
    2 回复  |  直到 7 年前
        1
  •  14
  •   Community c0D3l0g1c    7 年前

    我找到了一个答案,是关于旗子的: 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
  •  2
  •   Emmanuel    14 年前

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

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