Android O中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());
}

但是虽然修改了IMPORTANCE_LOW属性,通过Android Studio直接安装,发现并不生效,这里请参考我的另一个博文:

https://blog.csdn.net/u012551029/article/details/79917099

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值