如何在最新的 Android SDK 更新中使用 notificationBuilder.setSmallIcon(icon)?
How to use notificationBuilder.setSmallIcon(icon) in latest Android SDK update?
我想使用 setSmallIcon(Icon.createWithBitmap())
为我的通知创建小图标,但构造函数 NotificationCompat.Builder(context)
现在已被弃用,最新的构造函数 NotificationCompat.Builder(context, channelId)
不包含接受 icon
作为参数。我如何在最新的通知生成器中使用图标对象?
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{ createNotificationChannel("111", "Speed Monitor Service") } else { "" }
val remoteView = RemoteViews(packageName, R.layout.notification)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background) //this method only accepts int not Icon
.setCustomContentView(remoteView)
.build()
startForeground(1, notification)
Notification.Builder.setSmallIcon 是你需要使用的。如果您需要使用较旧的 API 级别,您可以使用 NotificationCompat.Builder
.
此方法有多个覆盖,具体取决于您要使用的资源,如果您需要使用运行时图像,而不是应用程序中捆绑的资源,您可以利用 Icon class 和方法 Notification.Builder.setSmallIcon(Icon icon)
.
你的问题有点含糊,让我为你澄清一下。 Notification.Builder(context)
(已弃用)和 Notification.Builder(context, channelId)
是相同 class Notification.Builder
的两个构造函数。 class 本身并未弃用。
Notification.Builder(context, channelId) doesn't contain this method
which accepts icon as parameter
以上说法是错误的(技术上的错误)。使用任一构造函数并不意味着您正在使用两个不同的 classes。这里只有一个 class Notification.Builder
并且它确实包含函数 setSmallIcon(Icon icon)
正如你在这里看到的 https://developer.android.com/reference/android/app/Notification.Builder.html#setSmallIcon(android.graphics.drawable.Icon)
换句话说 setSmallIcon(Icon icon)
必须在那里。您看不到此功能的原因可能不同。显示进一步许可的代码。
我想使用 setSmallIcon(Icon.createWithBitmap())
为我的通知创建小图标,但构造函数 NotificationCompat.Builder(context)
现在已被弃用,最新的构造函数 NotificationCompat.Builder(context, channelId)
不包含接受 icon
作为参数。我如何在最新的通知生成器中使用图标对象?
val channelId =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{ createNotificationChannel("111", "Speed Monitor Service") } else { "" }
val remoteView = RemoteViews(packageName, R.layout.notification)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background) //this method only accepts int not Icon
.setCustomContentView(remoteView)
.build()
startForeground(1, notification)
Notification.Builder.setSmallIcon 是你需要使用的。如果您需要使用较旧的 API 级别,您可以使用 NotificationCompat.Builder
.
此方法有多个覆盖,具体取决于您要使用的资源,如果您需要使用运行时图像,而不是应用程序中捆绑的资源,您可以利用 Icon class 和方法 Notification.Builder.setSmallIcon(Icon icon)
.
你的问题有点含糊,让我为你澄清一下。 Notification.Builder(context)
(已弃用)和 Notification.Builder(context, channelId)
是相同 class Notification.Builder
的两个构造函数。 class 本身并未弃用。
Notification.Builder(context, channelId) doesn't contain this method which accepts icon as parameter
以上说法是错误的(技术上的错误)。使用任一构造函数并不意味着您正在使用两个不同的 classes。这里只有一个 class Notification.Builder
并且它确实包含函数 setSmallIcon(Icon icon)
正如你在这里看到的 https://developer.android.com/reference/android/app/Notification.Builder.html#setSmallIcon(android.graphics.drawable.Icon)
换句话说 setSmallIcon(Icon icon)
必须在那里。您看不到此功能的原因可能不同。显示进一步许可的代码。