Android 踩坑记2---------Android8.0系统通知创建坑

记上一篇文章写了下载逻辑后,产品要求下载后需要有通知栏显示下载进度条,其他手机能正常显示,8.0又继续在作怪,无法展示进度条,分析发现,原来8.0的通知栏不能像之前那样通过NotificationCompat.Builder 来创建,还需要设置NotificationChannel,

具体方式如下

1、首先一样是要判断版本号

mNotifyManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(mContext, null);
        if (Build.VERSION.SDK_INT >= 26) {
            String channelcontent = WalleChannelReader.getChannel(mContext);//获取渠道名称
            NotificationChannel channel = new NotificationChannel("channel_id","channel_name",  NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("下载");
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setVibrationPattern(new long[]{0});
//            channel.importance = NotificationManager.IMPORTANCE_LOW //设置为low, 通知栏不会有声音
            mNotifyManager.createNotificationChannel(channel);
            mBuilder.setChannelId("1");
        }

然后再按照正常逻辑走就可以了。

这里有两个坑需要说下,第一是NotificationManager.IMPORTANCE_HIGH  如果设置为NotificationManager.IMPORTANCE_LOW

会出现下载进度条不正常的问题。

第二,如果不加这个channel.setVibrationPattern(new long[]{0});会出现某些手机产生进度条的时候出现震动的问题,小米8不会出现,但是华为p20则会出现

android 开发不易,且行且珍惜!

猜你喜欢

转载自blog.csdn.net/wanzhuanit/article/details/81331363