我有一个正在进行的通知,其中包含“取消”操作。看起来像这样:
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及更高版本中引起问题。 这个错误是什么意思?我该如何解决?
答案 0 :(得分:0)
如果您有通知ID,则可以使用通知管理器致电取消。
final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
答案 1 :(得分:0)
如果您使用的是Kotlin,则可以像这样扩展NotificationCompat.Builder
类:
@SuppressLint ("RestrictedApi")
fun NotificationCompat.Builder.clearActions () {
mActions.clear()
}
然后您可以简单地致电
progressNotif.clearActions()