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

更多相关文章

  1. Android字符串格式化
  2. 设置Listview item 的分隔线
  3. TextView设置android:ellipsize="marquee"属性,无法实现跑马灯效
  4. Android(安卓)Snippet
  5. android:layout_below位置到底below到哪儿了?
  6. AndroidX之CoordinatorLayout+AppBarLayout顶部折叠栏
  7. 【LatinIME】默认开启键盘按键声
  8. android View的三个构造方法 简单总结
  9. Android的Audio系统详解

随机推荐

  1. Android 笔记
  2. Settings.System 和 SystemProperties
  3. Android初级教程_onKeyDown监听返回键无
  4. android中的handler
  5. Android AIDL使用
  6. Android 如何获取keyboard和TP消息 分享
  7. 升级Android(安卓)Sdk Tools时遇到Failed
  8. Android常用屏幕适配方式
  9. Android(安卓)SDK版本号和名称对应表
  10. Android中RelativeLayout布局各个xml相对