在android P中清除android通知动作的正确方法?

问题描述 投票:0回答:2

我有一个“取消”操作的持续通知。它看起来像这样:

NotificationCompat.Builder progressNotif = new NotificationCompat.Builder(context, BackupService.BACKUP_RUNNING_NOTIFICATION);
...
...
// adding an action
progressNotif.addAction(cancelAction);

接下来,在作业结束后,我想删除“取消”操作。我在想的是这个:

progressNotif.mActions.clear();

但最近,Android Studio给了我这个错误:

Builder.mActions只能在同一个库组中访问(groupId = com.android.support)

这可能会导致Android Pie及以上版本出现问题。这个错误是什么意思?我该如何解决?

android android-notifications android-9.0-pie
2个回答
0
投票

如果您有通知IF,则可以使用通知管理器呼叫取消。

final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);

0
投票

如果你正在使用Kotlin,你可以像这样扩展NotificationCompat.Builder类:

@SuppressLint ("RestrictedApi")
fun NotificationCompat.Builder.clearActions () {
    mActions.clear()
}

然后你可以简单地打电话

progressNotif.clearActions()
© www.soinside.com 2019 - 2024. All rights reserved.