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

举报
程思扬 发表于 2022/01/14 00:54:54 2022/01/14
【摘要】 Android 8.0中Notification的Progress每次更新进度,都会弹出提示,并且有提示音。原代码如下 public void notifyDownloading(long progress, long num, String file_name) { Notification.Builder mBuilder;...

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

文章来源: chengsy.blog.csdn.net,作者:程思扬,版权归原作者所有,如需转载,请联系作者。

原文链接:chengsy.blog.csdn.net/article/details/80448313

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。