android - NotificationCompat 和 API 检查

标签 android android-notifications android-support-library

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

最佳答案

setGroupAlertBehavior用于更改通知分组行为。

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

From Docs

On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification. The user can expand this notification to see each separate notification, as shown in figure 1. To support older versions, which cannot show a nested group of notifications, you must create an extra notification that acts as the summary. This appears as the only notification and the system hides all the others. So this summary should include a snippet from all the other notifications, which the user can tap to open your app.

因此,要勉强模仿旧设备上的组通知行为,您需要有单独的编码行为,因此您需要 API 级别检查,因为对于旧设备

Our tests indicate no warning from IDE and no crash when running on OS 4.4.x, 6.x and 7.x. Odd.

NotificationCompat 使用不同的方法为不同的平台构建通知,因此传递给 setGroupAltertBehaviorint 参数在为api 20 所以是的,在 20

以下的 API 上使用它是安全的

查看何时调用 setGroupAltertBehavior

 public Builder setGroupAlertBehavior(int groupAlertBehavior) {
        mGroupAlertBehavior = groupAlertBehavior;
        return this;
    }

但是 mGroupAlertBehavior 将永远不会在 API 19 及以下版本中使用

 @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;
        }

关于android - NotificationCompat 和 API 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49903233/

相关文章:

java - 如何在android中的gridview中添加图像按钮

android - java.io.filenotfoundexception :/home/ljh/. android/analytics.settings(权限被拒绝)

安卓 MultiDex : an all time salvation is imperative

java - 同一项目中的多个 android-support-v4 版本

java - 在 Android Studio 中是否可以使用热键通过分配来替换字段的所有出现?

Android:在应用程序的设备中管理多个推送通知

android - 本地通知问题

android - 通知标志的值为 -3

c# - Xamarin - 找不到类(android 支持库)

安卓位置监听器