Android 通知兼容性和API检查

Android 通知兼容性和API检查,android,android-notifications,android-support-library,Android,Android Notifications,Android Support Library,如果我们在Android OS API 19-25上使用NotificationCompat的setGroupAlertBehavior,是否需要执行API检查?用于更改通知分组行为 通知组功能仅在Android 7.0(API级别24)及更高版本上可用,该功能不向后兼容,因此您需要在使用前进行API级别检查 在Android 7.0(API级别24)及更高版本上,系统会自动 使用每个组中的文本片段为您的组生成摘要 通知。用户可以展开此通知以查看每个 单独的通知,如图1所示以支持旧版本 无法显

如果我们在Android OS API 19-25上使用NotificationCompat的setGroupAlertBehavior,是否需要执行API检查?

用于更改通知分组行为

通知组功能仅在Android 7.0(API级别24)及更高版本上可用,该功能不向后兼容,因此您需要在使用前进行API级别检查

在Android 7.0(API级别24)及更高版本上,系统会自动
使用每个组中的文本片段为您的组生成摘要
通知
。用户可以展开此通知以查看每个 单独的通知,如图1所示<代码>以支持旧版本 无法显示嵌套通知组的版本,必须 创建作为摘要的额外通知。这似乎是 唯一的通知,系统将隐藏所有其他通知。那么这个 摘要应包括来自所有其他通知的片段, 用户可以点击以打开您的应用程序

因此,为了在旧设备上几乎不模仿组通知的行为,您需要有单独的编码行为,因此需要进行API级别检查,因为对于旧设备


我们的测试表明,在操作系统上运行时,IDE没有发出警告,也没有崩溃 4.4.x、6.x和7.x。奇怪

NotificationCompat
使用不同的方法为不同的平台构建通知,因此在为下面的api
20
创建通知时,传递给
setGroupAltertBehavior
int
参数将永远不会被使用,因此在下面的api
20
上使用它是安全的

请参见何时调用
setGroupAltertBehavior

 public Builder setGroupAlertBehavior(int groupAlertBehavior) {
        mGroupAlertBehavior = groupAlertBehavior;
        return this;
    }
但是,
mGroupAlertBehavior
将永远不会作为

 @RequiresApi(19)
    static class NotificationCompatApi19Impl extends NotificationCompatApi16Impl {
        @Override
        public Notification build(Builder b, BuilderExtender extender) {
            NotificationCompatKitKat.Builder builder = new NotificationCompatKitKat.Builder(
                    b.mContext, b.mNotification, b.resolveTitle(), b.resolveText(), b.mContentInfo,
                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate, b.mShowWhen,
                    b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly,
                    b.mPeople, b.mExtras, b.mGroupKey, b.mGroupSummary, b.mSortKey,
                    b.mContentView, b.mBigContentView);
            addActionsToBuilder(builder, b.mActions);
            addStyleToBuilderJellybean(builder, b.mStyle);
            return extender.build(b, builder);
        }


    @RequiresApi(20)
    static class NotificationCompatApi20Impl extends NotificationCompatApi19Impl {
        @Override
        public Notification build(Builder b, BuilderExtender extender) {
            NotificationCompatApi20.Builder builder = new NotificationCompatApi20.Builder(
                    b.mContext, b.mNotification, b.resolveTitle(), b.resolveText(), b.mContentInfo,
                    b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon,
                    b.mProgressMax, b.mProgress, b.mProgressIndeterminate, b.mShowWhen,
                    b.mUseChronometer, b.mPriority, b.mSubText, b.mLocalOnly, b.mPeople, b.mExtras,
                    b.mGroupKey, b.mGroupSummary, b.mSortKey, b.mContentView, b.mBigContentView,
                    b.mGroupAlertBehavior);
 // used on API 20+. ^^^^^^^^^^^^^^^^^^^^^ but not below
            addActionsToBuilder(builder, b.mActions);
            addStyleToBuilderJellybean(builder, b.mStyle);
            Notification notification = extender.build(b, builder);
            if (b.mStyle != null) {
                b.mStyle.addCompatExtras(getExtras(notification));
            }
            return notification;
        }

您的IDE将向您发出警告我们的测试表明,在OS 4.4.x、6.x和7.x上运行时,IDE不会发出警告,也不会发生崩溃。@Android您仍然应该这样做,因为这也取决于制造商。我们的测试表明,在OS 4.4.x、6.x和7.x上运行时,IDE不会发出警告,也不会发生崩溃。奇怪。这可能是因为制造商或一些优化(虽然你可能在logcat中找到一些东西),但请尝试更新的答案。我不买制造商的角度。请记住,这是NotifcationCompat,它是支持库的一部分。我也不出售它,我记得,但您是否看过更新的答案,旧版本上不支持通知组,因此两个平台的方法都不同。旧版本上不支持通知组,但调用不会崩溃。因此支持库是否能够正常处理此问题?