在通知PendingIntents通知、PendingIntents

2023-09-06 06:28:44 作者:堕落ai上颓废

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

我必须指定点击时启动一个contentIntent通知:如果我不指定我沿着那些得到的东西行:

  E / ActivityManager(62):活动管理器崩溃E / ActivityManager(62):java.lang.IllegalArgumentException异常:需要contentIntent ... 

有关取消按钮,我设置的另一个意图:

 意图cancelSyncIntent =新意图(com.xyz.CANCEL_SYNC);contentView.setOnClickPendingIntent(R.id.cancel_sync,                                    PendingIntent.getBroadcast(上下文,0,                                    cancelSyncIntent,0)); 

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

我大概可以显示文本:<< preSS取消操作>>,但似乎相当重手

更新:AFER J.建议:

 最后通知N =新的通知(R.drawable.gen_auto_notification_icon,context.getResources()            .getString(                    fastSyncOnly? R.string.fast_synchronization_running_notification_title                            :R.string.synchronization_running_notification_title),新的Date()的getTime());    n.flags = Notification.FLAG_ONGOING_EVENT;    最后RemoteViews内容查看=新的RemoteViews(context.getPackageName(),R.layout.in_progress_notification);    n.contentView =内容查看;    //意图cancelSyncIntent =新意图(com.newsrob.CANCEL_SYNC);    意图cancelSyncIntent =新的Intent();    cancelSyncIntent.setClass(背景下,FireReceiver.class);    的PendingIntent pendingCancelSyncIntent = PendingIntent.getActivity(上下文,0,cancelSyncIntent,0);    contentView.setOnClickPendingIntent(R.id.cancel_sync,pendingCancelSyncIntent);    意图showDashboardIntent =新意图(背景下,DashboardListActivity.class);    的PendingIntent showDashboardPendingIntent = PendingIntent.getActivity(上下文,0,showDashboardIntent,0);    n.contentIntent = showDashboardPendingIntent; 
刚刚 这些中级考生成绩被取消

解决方案

简短的回答:你不能在通知视图点击子视图

再回应...

我花了一天时间试图让这还有工作,并最终放弃了,看着Android源。看起来一切都在makeNotificationView()内发生StatusBarService.java。在那里,code膨胀名为status_bar_latest_event在通知下拉遮阳每一行布局。它抓住从该视图它只是夸大命名为内容一ViewGroup中,它调用setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS),然后将您的通知视图的一个孩子。因此,假设我了解FOCUS_BLOCK_DESCENDANTS作品,似乎也没有你的看法的希望或任何其子女获得焦点或单击事件。的

我也跟着你与关联通知的主要PendingEvent如何运作的逻辑。承载您的通知查看相同的ViewGroup沾到它setOnClickListener()调用注册名为启动器的侦听器类。当该类获得一个onClick()事件,它只是呼吁的PendingIntent你send()方法与通知相关联。我希望,也许点击的坐标可能相处,所以我至少可以尝试计算点击的是哪个按钮,但没有这样的运气。

传递

无论如何,如果任何人的方式知道要解决这个问题,我很乐意听到这种说法。现在,我只是拉我的按钮并继续前进。

I would like to show a notification that displays the progress of an ongoing operation. That works well for me. But at the same time the remote view should contain cancel button to stop the ongoing operation. The usual content intent should still do something else, i.e. not cancel the ongoing operation. It seems though that I can only have one intent.

I have to specify a contentIntent that is launched when clicking on the notification: If I don't specify that I get something along those lines:

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

For the "cancel" button I set another intent:

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

But this never works. I always get the content intent when the button is clicked. It looks like I cannot use buttons in remote views of notifications?!

I could probably display a text: "<< Press to cancel operation >>", but that seems rather heavy handed.

Update: Afer J.'s recommendations:

    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;

解决方案

Short answer: You cannot have clickable child views in a notification view.

Longer answer...

I spent a day trying to get this to work as well and eventually gave up and looked at the Android source. It looks like everything happens in makeNotificationView() within StatusBarService.java. In there, the code inflates a layout named "status_bar_latest_event" for each row in the notification pull down shade. It grabs a ViewGroup from that view it just inflated named "content", calls setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS) on it, and then adds your notification view as a child of that. So, assuming I understand how FOCUS_BLOCK_DESCENDANTS works, it appears there is no hope of your view or any of its children getting focus or click events.

I also followed the logic of how the main PendingEvent that you associate with your Notification works. The same ViewGroup that hosts your Notification view gets a setOnClickListener() call on it to register a listener class named "Launcher". When that class gets an onClick() event, it simply calls the send() method on the PendingIntent you associated with the Notification. I was hoping maybe the coordinate of the click might get passed along so I could at least attempt to compute which button was clicked on, but no such luck.

Anyway, if anyone knows of a way to work around this, I'd love to hear it. For now, I'm just pulling my buttons out and moving on.