【问题标题】:PendingIntent for TaskStackBuilderTaskStackBuilder 的 PendingIntent
【发布时间】:2017-08-10 17:07:15
【问题描述】:

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

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

之后他们没有使用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());

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    当您在应用中使用通知时,会发生什么。您首先指定一个明确的意图(正常意图)。然后你创建 TaskStackBuilder 对象来访问你不想在点击通知时启动的活动,然后用 TaskStackBuilder getPendingIntent 初始化 PendingIntent 引用() 并将其传递给通知对象。

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

    对象到通知对象

    要恢复代码中的错误,请执行以下操作:-

    1. 首先你应该创建显式意图
    2. 然后创建 TaskStackBuilder 对象,然后添加 parentStacknextIntent
    3. 创建 PendingIntent 对象,然后使用 stackBuilder.getPendingIntent(int id, Flag) 获取它的意图。
    4. 然后使用 notification.setContentIntent(PendingIntent) 将其传递给 Notification 对象。

    我相信你会完成这个指令...

    注意:-Notification 对象不接受 Intent 对象,它需要 PendingIntent 对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多