Android-单击状态栏中的通知将意图与目标活动绑定

Android-单击状态栏中的通知将意图与目标活动绑定,android,Android,我创建了一个向状态栏发送大量通知的活动。每个通知都包含一个带有捆绑包的意图。代码如下: String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); int icon = R.drawable.icon; long when = System.currentTimeM

我创建了一个向状态栏发送大量通知的活动。每个通知都包含一个带有捆绑包的意图。代码如下:

 String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;     
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, "Test Notification", when);


    Context context = getApplicationContext();      

    Bundle bundle = new Bundle();
    bundle.putString("action", "view");
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtras(bundle);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);     
    mNotificationManager.notify(1, notification);
 Bundle bundle = this.getIntent().getExtras();

    if(bundle != null)
    {
        String action = bundle.getString("action");
            performAction(action)
    }
当用户单击此通知时,我读取捆绑字符串“action”并执行该操作。代码如下:

 String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.icon;     
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, "Test Notification", when);


    Context context = getApplicationContext();      

    Bundle bundle = new Bundle();
    bundle.putString("action", "view");
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtras(bundle);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);     
    mNotificationManager.notify(1, notification);
 Bundle bundle = this.getIntent().getExtras();

    if(bundle != null)
    {
        String action = bundle.getString("action");
            performAction(action)
    }
一切正常。但是,当我使用设备上的“箭头”按钮最小化应用程序,然后按住主页按钮并单击我的应用程序图标时,应用程序启动并执行与单击上次通知相同的上次操作。我发现,当我们单击应用程序图标时,应用程序将以通知触发的最后一个意图启动。有人能帮忙吗

首先,“向状态栏发送大量通知的活动”对于生产应用程序来说是一个糟糕的设计。
活动
在用户面前已经有了用户界面,因此不需要
通知
。即使您是通过
服务
执行此操作,绝大多数Android应用程序一次也不需要超过一个
通知

对于您的特定问题,您认为您正在创建多个
挂起内容,但实际上并非如此。默认情况下,只有当基础的
意图
与另一个未完成的
未决内容
使用的
意图
存在实质性差异时,才会创建不同的
未决内容
。你的只是额外的不同。如果要有几个未完成的
待处理内容
,则需要将它们放在不同的
意图
,其中这些
意图
因组件、操作、数据(Uri)或类别而异。

首先,“向状态栏发送大量通知的活动”对于生产应用程序来说,这是一个糟糕的设计。
活动
在用户面前已经有了用户界面,因此不需要
通知
。即使您是通过
服务
执行此操作,绝大多数Android应用程序一次也不需要超过一个
通知


对于您的特定问题,您认为您正在创建多个
挂起内容,但实际上并非如此。默认情况下,只有当基础的
意图
与另一个未完成的
未决内容
使用的
意图
存在实质性差异时,才会创建不同的
未决内容
。你的只是额外的不同。如果要有几个未完成的
待处理内容
,则需要将它们放在不同的
意图上,其中这些
意图
因组件、操作、数据(Uri)或类别而异。

我自己找到了解决方案:

Intent intent = getIntent();
int flags = intent.getFlags();
boolean launchedFromHistory = ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0);

通过这种方式,我们可以检查活动从何处开始。

我自己找到了解决方案:

Intent intent = getIntent();
int flags = intent.getFlags();
boolean launchedFromHistory = ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0);

这样我们可以检查活动从何处开始。

现在我修改了代码,现在只向状态栏发送一个通知。当我单击该通知时,相应的PendingEvent将启动。但是,如果我最小化应用程序并再次恢复它,它仍然会触发最后一个挂起的事件。我已将PendingEvent与通知绑定在一起,如果有人从状态栏单击该通知,并且您正在更新单个
通知
,并且仅在
意图
上更改附加内容,则它应该会触发,您需要在调用中使用
FLAG\u UPDATE\u CURRENT
来创建
PendingEvent
。我想知道为什么在未单击通知的情况下启动Intent?我只是最小化了应用程序并恢复了它,它触发了上一个挂起的意图。现在我修改了代码,现在我只向状态栏发送一个通知。当我单击该通知时,相应的PendingEvent将启动。但是,如果我最小化应用程序并再次恢复它,它仍然会触发最后一个挂起的事件。我已将PendingEvent与通知绑定在一起,如果有人从状态栏单击该通知,并且您正在更新单个
通知
,并且仅在
意图
上更改附加内容,则它应该会触发,您需要在调用中使用
FLAG\u UPDATE\u CURRENT
来创建
PendingEvent
。我想知道为什么在未单击通知的情况下启动Intent?我只是最小化了应用程序并恢复了它,它触发了最后一个挂起的意图。