Android8.0自定义通知栏如何实现,给出代码示范
时间: 2023-04-09 09:02:51 浏览: 158
以下是一个简单的 Android 8.0 自定义通知栏的示例代码:
```
// 创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("channel_description");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// 显示通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
```
请注意,这只是一个简单的示例代码,您需要根据您的需求进行修改和扩展。
阅读全文