如何将 android 通知转换为 NotificationCompat

How to convert an android Notification to a NotificationCompat

我正在尝试阅读 NotificationListenerService 中每个 StatusBarNotification 附带的 ActionRemoteInput。但是,我想使用兼容性库来获得支持,所以我试图将所有内容都设置为 NotificationCompat.Actionandroid.support.v4.app.RemoteInput

for (StatusBarNotification sbn : NLService.this.getActiveNotifications()) {
    Notification notif = sbn.getNotification();
    NotificationCompat notifCompat = //?????????????
}

getNotification only returns a Notification 而不是 NotificationCompat,我一直很沮丧地尝试将一个转换为另一个。我确信有一种非常简单的方法可以做到这一点,但是当我查看构建器 类 和扩展器 类 时,我找不到任何东西。

许多 appcompat-v4 类 提供静态方法来访问 API 4.

之后添加的方法

其中许多在 API 关卡上调用时将不执行操作或 return 默认值,而这些关卡尚不存在。有些人会尝试模仿新行为。它们通常被记录在案。

例如,

// Using latest APIs
String sortKey = myNotification.getSortKey();

// Using compat APIs
String sortKey = NotificationCompat.getSortKey(myNotification);

附带说明一下,这些 类 的 implementation details 值得研究,以防您需要自己编写。他们通过为当前 API 级别静态加载正确的实现 类 来避免代价高昂的反射。