Android 8.0中Notification的Progress每次更新进度,都会弹出提示,并且有提示音。原代码如下
public void notifyDownloading(long progress, long num, String file_name) { Notification.Builder mBuilder; mBuilder = new Notification.Builder(MainActivity.this, TAG); NotificationChannel channel; channel = new NotificationChannel(TAG , file_name, NotificationManager.IMPORTANCE_MAX); mNotifyManager.createNotificationChannel(channel); mBuilder.setSmallIcon(R.drawable.notification_download_icon); mBuilder.setProgress((int) num, (int) progress, false); mBuilder.setContentInfo(getPercent((int) progress, (int) num)); mBuilder.setOngoing(true); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setContentTitle(file_name); mBuilder.setContentText("download"); PendingIntent pendIntent = PendingIntent.getActivity( MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendIntent); mNotifyManager.notify(NOTIFY_ID, mBuilder.build()); }
这里需要修改NotificationChannel的importance属性:
/** * Min notification importance: only shows in the shade, below the fold. */ public static final int IMPORTANCE_MIN = 1; /** * Low notification importance: shows everywhere, but is not intrusive. */ public static final int IMPORTANCE_LOW = 2; /** * Default notification importance: shows everywhere, makes noise, but does not visually * intrude. */ public static final int IMPORTANCE_DEFAULT = 3; /** * Higher notification importance: shows everywhere, makes noise and peeks. May use full screen * intents. */ public static final int IMPORTANCE_HIGH = 4;
/** * Unused. */ public static final int IMPORTANCE_MAX = 5;
这里的IMPORTANCE_MAX应该和IMPORTANCE_HIGH属性类似,表示显示时有声音,且会出现弹框提示。在Android 8.0中,这样设置后,Progress每次更新都会有声音和弹框。
把IMPORTANCE_MAX修改为IMPORTANCE_LOW,则不会出现该现象。
修改后代码如下:
public void notifyDownloading(long progress, long num, String file_name) { Notification.Builder mBuilder; mBuilder = new Notification.Builder(MainActivity.this, TAG ); NotificationChannel channel; channel = new NotificationChannel(TAG, file_name, NotificationManager.IMPORTANCE_LOW); mNotifyManager.createNotificationChannel(channel); mBuilder.setSmallIcon(R.drawable.notification_download_icon); mBuilder.setProgress((int) num, (int) progress, false); mBuilder.setContentInfo(getPercent((int) progress, (int) num)); mBuilder.setOngoing(true); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setContentTitle(file_name); mBuilder.setContentText("download"); PendingIntent pendIntent = PendingIntent.getActivity( MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendIntent); mNotifyManager.notify(NOTIFY_ID, mBuilder.build()); }
但是虽然修改了IMPORTANCE_LOW属性,通过Android Studio直接安装,发现并不生效,这里请参考我的另一个博文:
https://blog.csdn.net/u012551029/article/details/79917099