不过有一个领域随着时间的推移发生了很大的变化,那就是 Android 的通知 API。曾经有一段时间,每一次 Google I/O 大会都会宣布一个新的 API 方面的变化。
好在我们有 NotificationManagerCompat
来拯救我们!!!
比如说当我们需要获取通知通道组(NotificationChannelGroup
)的时候:
val groups = notificationManagerCompat.notificationChannelGroups
我们不需要担心通道组是否被所有操作系统版本支持,因为它实际上会被在 Compat 类中处理掉:
public List getNotificationChannelGroups() {
if (Build.VERSION.SDK_INT >= 26) {
return mNotificationManager.getNotificationChannelGroups()
}
return Collections.emptyList()
}
如果我们是在 API 26 之前,我们只会得到一个空的列表,如果不是我们就会得到在 API 26 中引入的新通道组。
你可以在 NotificationManagerCompat 代码处找到更复杂的检查。
但如果你仔细观察,NotificationManagerCompat
会返回我们实际的 API 类。在上面的示例代码中列出了 NotificationChannelGroup
,这并不是一个复制的 Compat 版本,不过因为它检查了 API 的可用