【问题标题】:PendingIntents in Notifications通知中的 PendingIntents
【发布时间】:2010-10-22 05:54:10
【问题描述】:

我想显示一个通知,显示一个进度 正在进行的操作。这对我很有效。 但同时远程视图应包含取消按钮以停止正在进行的操作。通常的内容意图仍然应该做其他事情,即不取消正在进行的操作。看来我只能有一个意图。

我必须指定一个在点击时启动的 contentIntent 通知:如果我没有指定我会得到一些东西 行:

E/ActivityManager(   62): Activity Manager Crash
E/ActivityManager(   62): java.lang.IllegalArgumentException: contentIntent required ...

对于“取消”按钮,我设置了另一个意图:

Intent cancelSyncIntent = new Intent("com.xyz.CANCEL_SYNC");
contentView.setOnClickPendingIntent(R.id.cancel_sync,
                                    PendingIntent.getBroadcast(context, 0,
                                    cancelSyncIntent, 0));

但这永远不会奏效。当按钮时我总是得到内容意图 被点击。看起来我不能在远程视图中使用按钮 通知?!

我可能会显示一个文本:“>”,但这似乎有点笨拙。

更新:Afer J. 的建议:

    final Notification n = new Notification(R.drawable.gen_auto_notification_icon, context.getResources()
            .getString(
                    fastSyncOnly ? R.string.fast_synchronization_running_notification_title
                            : R.string.synchronization_running_notification_title), new Date().getTime());

    n.flags = Notification.FLAG_ONGOING_EVENT;

    final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.in_progress_notification);
    n.contentView = contentView;

    // Intent cancelSyncIntent = new Intent("com.newsrob.CANCEL_SYNC");
    Intent cancelSyncIntent = new Intent();
    cancelSyncIntent.setClass(context, FireReceiver.class);
    PendingIntent pendingCancelSyncIntent = PendingIntent.getActivity(context, 0, cancelSyncIntent, 0);
    contentView.setOnClickPendingIntent(R.id.cancel_sync, pendingCancelSyncIntent);

    Intent showDashboardIntent = new Intent(context, DashboardListActivity.class);
    PendingIntent showDashboardPendingIntent = PendingIntent.getActivity(context, 0, showDashboardIntent, 0);
    n.contentIntent = showDashboardPendingIntent;

【问题讨论】:

    标签: android


    【解决方案1】:

    简答:通知视图中不能有可点击的子视图。

    更长的答案...

    我花了一天时间试图让它也能正常工作,最终放弃并查看了 Android 源代码。看起来一切都发生在 StatusBarService.java 中的 makeNotificationView() 中。在那里,代码为通知下拉阴影中的每一行添加了一个名为“status_bar_latest_event”的布局。它从它刚刚膨胀的名为“内容”的视图中获取一个 ViewGroup,在其上调用 setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS),然后将您的通知视图添加为它的子视图。因此,假设我了解 FOCUS_BLOCK_DESCENDANTS 的工作原理,您的视图或其任何子视图似乎没有希望获得焦点或点击事件。

    我还遵循了与通知相关联的主要 PendingEvent 如何工作的逻辑。托管 Notification 视图的同一 ViewGroup 会在其上调用 setOnClickListener() 以注册名为“Launcher”的侦听器类。当该类获得 onClick() 事件时,它只需调用与通知关联的 PendingIntent 上的 send() 方法。我希望点击的坐标可能会传递,所以我至少可以尝试计算点击了哪个按钮,但没有这样的运气。

    无论如何,如果有人知道解决此问题的方法,我很想听听。现在,我只是拔出按钮继续前进。

    【讨论】:

      【解决方案2】:

      意图 cancelSyncIntent = new Intent(); cancelSyncIntent .setClassName("com.xyz","com.xyz.CANCEL_SYNC");

      PendingIntent pendingIntent = PendingIntent.getActivity(context,
          0 /* no requestCode */, cancelSyncIntent , 0 /* no flags */);
      contentView.setOnClickPendingIntent(R.id.cancel_sync, pendingIntent);
      

      【讨论】:

      • 谢谢 J。你确定这样可以吗?我尝试过,见上文,但仍然在点击“取消”时,我只得到 showDashboardIntent。
      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 2023-04-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多