更改通知上的操作按钮

Jes*_*ess 15 notifications android android-notifications

我有一个通知,我试图通过重复使用相同的Notification Builder来更新,但是没有办法清除按钮,你只能打电话addAction.不使用相同的Builder会导致通知闪烁,这是不可取的.这有什么解决方案吗?我正在使用NotificationCompatv4支持库.

Pit*_*tel 5

notificationBuilder.mActions.clear();
Run Code Online (Sandbox Code Playgroud)

实际上是public ArrayList<Action>,因此您可以使用它来做任何想要的事情。

  • 它对我不起作用:( 我收到错误:“mActions 只能从同一个库组 (group=com.android.support) 内访问” (2认同)

pro*_*m85 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) {}
    
    Run Code Online (Sandbox Code Playgroud)