更改通知中的操作按钮

19

我有一个通知,我想通过重复使用相同的通知构建器来更新它,但没有办法清除按钮,只能调用 addAction。不使用相同的构建器会导致通知闪烁,这是不可取的。是否有解决方案?我正在使用 v4 支持库中的 NotificationCompat


我也在寻找解决方案,你的问题有进展了吗? - Nesim Razon
你能提供更多的信息吗?你到底想做什么,你已经做了什么? - Paramone
3个回答

7
notificationBuilder.mActions.clear();

实际上是 public ArrayList<Action>,所以您可以随意使用它。


3
它对我不起作用:( 我收到一个错误消息: "mActions只能从同一库组(group=com.android.support)中访问" - user2146414

2
你有两个选项来实现这个目标:
  1. Use a custom layout (just copy the design of the native notification if you want to) and then use this in a RemoteView and just make views visible or hide them. With remoteView.setViewVisibility(...) for example... Or change the text of the buttons...
  2. Use reflection to clear the builders actions. Would work like following:

    try {
        //Use reflection to remove all old actions
        Field f = mNotificationBuilder.getClass().getDeclaredField("mActions");
        f.setAccessible(true);
        f.set(mNotificationBuilder, new ArrayList<>());
    } 
    catch (NoSuchFieldException e) {} 
    catch (IllegalAccessException e) {}
    

0

从API 24开始,您可以使用setActions()方法来更新图标、文本和挂起意图。

Notification.Action.Builder builder = new Notification.Action.Builder( Icon.createWithResource( this, R.drawable.ic_pause) , getString( R.string.pause ), PendingIntent.getBroadcast( this, 1, new Intent( TIMER_PAUSE ), 0 ) );
Notification.Action action = builder.build();
...
notification_builder.setActions( action );
Notification notification = notification_builder.build();
NotificationManager nm = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
nm.notify( 1, notification );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接