TaskStackBuilder的PendingIntent

时间:2013-10-09 04:23:24

标签: android notifications

我阅读了创建通知的文档。因为他们使用了TaskStackBuilder

  1. 为Activity创建单独的任务。
  2. 使用addParentStack()
  3. 添加活动的父级
  4. 添加意图
  5. 最终创建PendingIntent。
  6. 之后

    他们没有使用StackBuilder对象在NotificationCompat.Builder对象中设置。他们使用了PendingIntent对象。

    所有上述信息(创建单独的任务,识别父活动,识别意图)都在PendingInent中吗?

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ResultActivity.class);
    
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ResultActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
    

1 个答案:

答案 0 :(得分:0)

在您的应用中使用通知时会发生什么。您首先指定显式意图(正常意图)。然后创建 TaskStackBuilder 对象,以便在点击通知时访问您要启动的活动,然后使用 TaskStackBuilder PendingIntent 引用> getPendingIntent ()并将其传递给通知对象。

现在 PendingIntent 做的是使用 stackBuilder.getPendingIntent(int id,Flag) TaskStackBuilder 对象获取意图并传递挂起通过调用 notification.setContentIntent(PendingIntent object)

来通知对象

要恢复代码中的错误,请按以下步骤操作: -

  1. 首先,您应该创建明确的意图
  2. 然后创建 TaskStackBuilder 对象,然后添加 parentStack nextIntent
  3. 创建 PendingIntent 对象,然后使用 stackBuilder.getPendingIntent(int id,Flag)获取它的意图。
  4. 然后使用 notification.setContentIntent(PendingIntent)将其传递给Notification对象。
  5. 我相信你会完成这条指令......

    注意: - 通知对象不接受意图对象,它需要 PendingIntent对象