如果说我们在 Android OS API 19-25 上使用 NotificationCompat 的 setGroupAlertBehavior,是否需要执行 API 检查?
最佳答案
setGroupAlertBehavior用于更改通知分组行为。
通知组功能仅适用于 Android 7.0(API 级别 24)及更高版本,不向后兼容,因此您需要在使用前进行 API 级别检查
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
使用不同的方法为不同的平台构建通知,因此传递给 setGroupAltertBehavior
的 int
参数在为api 20
所以是的,在 20
查看何时调用 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/