先说解决方案:https://blog.csdn.net/tempersitu/article/details/78784350
问题
问题出现在应用创建一条通知消息并显示后,提示系统UI崩溃,并进入锁屏无限闪屏.如图:
该问题只出现在了真机上而未出现在虚拟机上,而前者的安卓补丁比后者的还要更新.
复现
使用Android Studio 创建一个项目设置minSDK版本为26及以上,创建一条通知并显示.测试运行.代码基本如下:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default","default"
,NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);//安卓8.0后需要创建通知管道,用于通知分类等
Notification notification = new NotificationCompat.Builder(this,"default")
.setContentTitle("This is content title")
.setContentText("This is content text")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
//
.setWhen(System.currentTimeMillis())
.build();
manager.notify(1,notification);
*崩溃日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.systemui, PID: 4555
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:989)
at android.graphics.Bitmap.createBitmap(Bitmap.java:956)
at android.graphics.Bitmap.createBitmap(Bitmap.java:906)
at android.graphics.Bitmap.createBitmap(Bitmap.java:867)
at android.graphics.drawable.AdaptiveIconDrawable.updateMaskBoundsInternal(AdaptiveIconDrawable.java:333)
at android.graphics.drawable.AdaptiveIconDrawable.updateLayerBounds(AdaptiveIconDrawable.java:295)
at android.graphics.drawable.AdaptiveIconDrawable.onStateChange(AdaptiveIconDrawable.java:796)
at android.graphics.drawable.Drawable.setState(Drawable.java:760)
at android.widget.ImageView.drawableStateChanged(ImageView.java:1268)
at android.view.View.refreshDrawableState(View.java:19619)
at android.view.View.dispatchAttachedToWindow(View.java:17020)
at android.view.ViewGroup.addViewInner(ViewGroup.java:4924)
at android.view.ViewGroup.addView(ViewGroup.java:4716)
at com.android.systemui.statusbar.phone.NotificationIconAreaController.updateIconsForLayout(NotificationIconAreaController.java:204)
at com.android.systemui.statusbar.phone.NotificationIconAreaController.updateNotificationIcons(NotificationIconAreaController.java:152)
at com.android.systemui.statusbar.phone.StatusBar.updateNotificationShade(StatusBar.java:1900)
at com.android.systemui.statusbar.phone.StatusBar.updateNotifications(StatusBar.java:2080)
at com.android.systemui.statusbar.phone.StatusBar.addNotificationViews(StatusBar.java:6561)
at com.android.systemui.statusbar.phone.StatusBar.addNotification(StatusBar.java:1589)
at com.android.systemui.statusbar.phone.StatusBar$23$1.run(StatusBar.java:5534)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6535)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
原因
从日志中看到,当创建通知时,崩溃是由Oreo中引入的自适应图标引起的。具体可以拜读这篇文章https://blog.pusher.com/upgrade-app-android-oreo-avoid-factory-reset/
。
方案
知道问题是由自适应图标引起的后就可以有简单的解决方案,就是不使用自适应图标,具体方案最简单的是设置minSDK版本为26以下,或者直接删除res/mipmap-anydpi-v26
自适应图标文件夹。或者在Manifest中添加如下代码:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/your_non_adaptive_drawable" />
最后不要用自适应图标作为发送通知的图标。