Android 中Notification进度条一直弹出提示及提示音

简介: Android 中Notification进度条一直弹出提示及提示音

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());

}

目录
相关文章
|
1月前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
57 1
|
9月前
|
Android开发 UED
Android 实现通知栏和进度条效果(适用于Android8.0以上)
Android 实现通知栏和进度条效果(适用于Android8.0以上)
111 0
|
Android开发 开发者
Android 13 NotificationChannels与Notification的加载流程
Android 13 NotificationChannels与Notification的加载流程
Android 13 NotificationChannels与Notification的加载流程
|
5月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
5月前
|
XML Java Android开发
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
Android Studio App开发之通知推送Notification的讲解及实战(给用户推送信息实战)
209 0
|
1月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
9月前
|
Android开发
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
Android 中ProgressDialog进度条对话框的使用(使用子线程模拟更新进度)
104 0
|
5月前
|
XML API Android开发
Android 自定义View 之 饼状进度条
Android 自定义View 之 饼状进度条
|
5月前
|
Android开发 UED
[Android]ProgressBar进度条
[Android]ProgressBar进度条
41 0
|
5月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
64 0