用于前台服务的Android 8通知通道

Har*_*ger 8 notifications android android-8.0-oreo

我想知道我的通知创建源的以下修改是否与Android 8一致. 之前:

Notification.Builder builder = new Notification.Builder(this);
// ... (some code omitted)
myNotification = builder.build();
// ... (some code omitted)
startForeground(MY_NOTIFICATION_ID, myNotification);
Run Code Online (Sandbox Code Playgroud)

后:

Notification.Builder builder;
if ( Build.VERSION.SDK_INT < 26 ) {
    builder = new Notification.Builder(this);
} else {
    final String ChannelId = "mypackage.NotificationChannelID";
    final CharSequence ChannelName = "My Notification";
    NotificationChannel channel = new NotificationChannel(ChannelId, ChannelName, NotificationManager.IMPORTANCE_LOW);
    ((NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE)).
        createNotificationChannel(channel);
    builder = new Notification.Builder(this, ChannelId);
}
// ... (some code omitted)
myNotification = builder.build();
// ... (some code omitted)
startForeground(MY_NOTIFICATION_ID, myNotification);
Run Code Online (Sandbox Code Playgroud)

特别是,至少还有两个问题:

(1)我想知道是否以及在哪里添加void deleteNotificationChannel(String channelId)不会产生任何内存泄漏?

(2)我应该添加builder.setCategory(Notification.CATEGORY_SERVICE),如果是,为什么?