【问题标题】:Changing the action buttons on a notification更改通知上的操作按钮
【发布时间】:2013-09-04 01:55:36
【问题描述】:

我有一个通知,我正在尝试通过重复使用相同的通知生成器来更新,但无法清除按钮,您只能致电addAction。不使用相同的 Builder 会导致通知闪烁,这是不可取的。有什么解决办法吗?我正在使用来自 v4 支持库的NotificationCompat

【问题讨论】:

  • 我也在寻找解决方案,您的问题有进展吗?
  • 您能提供更多信息吗?你到底想做什么,你做了什么?

标签: android notifications android-notifications


【解决方案1】:
notificationBuilder.mActions.clear();

实际上是public ArrayList<Action>,所以你可以随心所欲地使用它。

【讨论】:

  • 它对我不起作用:(我收到一个错误:“mActions 只能从同一个库组中访问 (group=com.android.support)”
【解决方案2】:

您有两种选择来实现这一目标:

  1. 使用自定义布局(如果需要,只需复制本机通知的设计),然后在 RemoteView 中使用它,让视图可见或隐藏它们。以remoteView.setViewVisibility(...) 为例...或者更改按钮的文本...
  2. 使用反射来清除构建器的操作。会像下面这样工作:

    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) {}
    

【讨论】:

    【解决方案3】:

    从 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多