解决android8.0在通知中更新进度条导致连续响铃振动的bug

1,206 阅读1分钟

关键点在于设置通知渠道的优先级:NotificationManager.IMPORTANCE_LOW

private void createNotifcaton() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel); }

    builder = new NotificationCompat.Builder(context,channelID)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(false)
            .setChannelId(channelID)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
            .setContentTitle("更新中...")
            .setProgress(100,0, false)
            .setContentText(("进度:0%"));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_PROGRESS);
    }
}

private void updateNotifacation() {
    builder.setProgress(100, (int) currentProgress, false)
            .setContentText(("进度:" + (int) ((currentProgress) / ((double) 100) * 100) + "%"));

    showNotifacation();
}

private void showNotifacation() {
    notificationManager.notify(notificationId, builder.build());
}