【发布时间】:2016-07-31 16:19:08
【问题描述】:
目前我有这个 WET 代码,因为 NotificationCompat 不支持图标而不是资源 ID 的 setSmallIcon:
val notification = if (Build.VERSION.SDK_INT < 23) {
NotificationCompat.Builder(this)
.setLargeIcon(bitmap)
.setSmallIcon(R.drawable.ic_launcher)
.setContentText(intentDescriber!!.userFacingIntentDescription)
.setContentTitle(label)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build()
} else {
Notification.Builder(this)
.setSmallIcon(Icon.createWithBitmap(bitmap))
.setLargeIcon(bitmap)
.setContentText(intentDescriber!!.userFacingIntentDescription)
.setContentTitle(label)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.build()
}
有没有办法让它变得更好(DRY?) - 问题是两个构建器类是不同的..
【问题讨论】:
-
SDK 23 引入 Icon 类,compat 无法支持。
-
但它可能需要一个位图,并且在 23 impl 中它可以使用 Icon 并为这个 SDK 级别及更高级别包装位图
-
您可以随时制作自己的构建器类...
标签: android notifications kotlin