我有一个通知,我想通过重复使用相同的通知构建器来更新它,但没有办法清除按钮,只能调用 addAction
。不使用相同的构建器会导致通知闪烁,这是不可取的。是否有解决方案?我正在使用 v4 支持库中的 NotificationCompat
。
我有一个通知,我想通过重复使用相同的通知构建器来更新它,但没有办法清除按钮,只能调用 addAction
。不使用相同的构建器会导致通知闪烁,这是不可取的。是否有解决方案?我正在使用 v4 支持库中的 NotificationCompat
。
notificationBuilder.mActions.clear();
实际上是 public ArrayList<Action>
,所以您可以随意使用它。
remoteView.setViewVisibility(...)
for example... Or change the text of the buttons...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) {}
从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 );