Android 8.0 新特性 之通知、自适应图标

1,411 阅读1分钟

之前办公室的老大手机系统升级至8.0后,用我们的app就用不了了,一点就崩。于是乎,拿着报错日志来找我:android.app.RemoteServiceException:BadNotificationposted from package xxx :Couldn't create icon:StatusBarIcon ; 开始我还以为是应用图标的问题,于是做了8.0的自适应图标,后来仔细一看才发现是通知!!!! 这里附上我学习Android 8.0 和9.0新特性的两个链接 https://blog.csdn.net/lzllzllhl/article/details/76889957, https://blog.csdn.net/GenlanFeng/article/details/79496359

这里简单记录一下本人是这样解决上面那个异常的和自适应图标

1、通知 NotificationChannel 是Android 8.0 的新特性,如果targetSdkVersion提到26之后,就必须设置Chanel,不能为null,否则就不能弹出通知,但是上面我说我们的应用一点就崩,其实也不是,因为那是小米,每次点的时候就弹出错误日志上传的弹框,导致应用无法使用。

贴上我解决的代码:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ NotificationChannel channel = new NotificationChannel("chanelId","chanelName", android.app.NotificationManager.IMPORTANCE_DEFAULT); channel.enableLights(true); channel.setLightColor(Color.RED); channel.setShowBadge(true); notificationManager.createNotificationChannel(channel); notificationBuilder = new NotificationCompat.Builder(application,"finger"); }else { notificationBuilder = new NotificationCompat.Builder(application); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,"chanelId");

2、自适应图标 https://blog.csdn.net/lzllzllhl/article/details/76889957

如果要适配这个那就要让美工准备两张图了,一个是背景图ic_launcher_background,一个是前景图 ic_launcher_foreground;在资源目录下面创建一个一个文件夹mipmap-anydpi-v26存放ic_launcher.xml 和ic_launcher_round.xml,贴上代码


1)ic_launcher.xml

2)ic_launcher_round.xml

最后在AndroidManifest.xml的application节点里再加一个roundIcon的属性

android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round"

入行以来第一次写这样的文章,自知对原理没有阐述清除明白,所以文中附上了两个链接,学习的道路还很长很长呀!!!!!!!!!