阅读 671

Android 8.0 发Notification通知后系统崩溃

发生的8.0的机器上, 以下是报错信息

2019-10-26 18:00:51.099 4825-4825/? D/AndroidRuntime: Shutting down VM
2019-10-26 18:00:51.100 4825-4825/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.android.systemui, PID: 4825
    java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1001)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:968)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:918)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:879)
        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:782)
        at android.graphics.drawable.Drawable.setState(Drawable.java:775)
        at android.widget.ImageView.drawableStateChanged(ImageView.java:1305)
        at android.view.View.refreshDrawableState(View.java:20012)
        at android.view.View.dispatchAttachedToWindow(View.java:17409)
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4964)
        at android.view.ViewGroup.addView(ViewGroup.java:4755)
        at com.android.systemui.statusbar.phone.NotificationIconAreaController.updateIconsForLayout(NotificationIconAreaController.java:247)
        at com.android.systemui.statusbar.phone.NotificationIconAreaController.updateNotificationIcons(NotificationIconAreaController.java:155)
        at com.android.systemui.statusbar.phone.StatusBar.updateNotificationShade(StatusBar.java:1962)
        at com.android.systemui.statusbar.phone.StatusBar.updateNotifications(StatusBar.java:2142)
        at com.android.systemui.statusbar.phone.StatusBar.addNotificationViews(StatusBar.java:6804)
        at com.android.systemui.statusbar.phone.StatusBar.addEntry(StatusBar.java:1633)
        at com.android.systemui.statusbar.phone.StatusBar.onAsyncInflationFinished(StatusBar.java:1650)
        at com.android.systemui.statusbar.notification.NotificationInflater$AsyncInflationTask.onAsyncInflationFinished(NotificationInflater.java:641)
        at com.android.systemui.statusbar.notification.NotificationInflater.finishIfDone(NotificationInflater.java:460)
        at com.android.systemui.statusbar.notification.NotificationInflater.-wrap0(Unknown Source:0)
        at com.android.systemui.statusbar.notification.NotificationInflater$6.onViewApplied(NotificationInflater.java:343)
        at android.widget.RemoteViews$AsyncApplyTask.onPostExecute(RemoteViews.java:3421)
        at android.widget.RemoteViews$AsyncApplyTask.onPostExecute(RemoteViews.java:3401)
        at android.os.AsyncTask.finish(AsyncTask.java:695)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        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:775)
2019-10-26 18:00:51.107 2389-4630/system_process W/RescueParty: Noticed 3 events for UID 10015 in last 4 sec
2019-10-26 18:00:51.108 2389-4630/system_process W/ActivityManager: Process com.android.systemui has crashed too many times: killing!
2019-10-26 18:00:51.108 4825-4825/? I/Process: Sending signal. PID: 4825 SIG: 9
2019-10-26 18:00:51.177 2389-2577/system_process W/InputDispatcher: channel 'ce0fd40 com.android.systemui (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.177 2389-2577/system_process E/InputDispatcher: channel 'ce0fd40 com.android.systemui (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.177 2389-2577/system_process W/InputDispatcher: channel '14e8472 com.android.systemui (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.177 2389-2577/system_process E/InputDispatcher: channel '14e8472 com.android.systemui (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.177 2389-2577/system_process W/InputDispatcher: channel '2ee37d4 com.android.systemui (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.177 2389-2577/system_process E/InputDispatcher: channel '2ee37d4 com.android.systemui (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.177 2389-2577/system_process W/InputDispatcher: channel '7becac5 com.android.systemui.ImageWallpaper (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.177 2389-2577/system_process E/InputDispatcher: channel '7becac5 com.android.systemui.ImageWallpaper (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.177 2389-2577/system_process W/InputDispatcher: channel '666dc68 StatusBar (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.177 2389-2577/system_process E/InputDispatcher: channel '666dc68 StatusBar (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.190 2389-2577/system_process W/InputDispatcher: channel '70763c0 AssistPreviewPanel (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.190 2389-2577/system_process E/InputDispatcher: channel '70763c0 AssistPreviewPanel (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.190 2389-2577/system_process W/InputDispatcher: channel '8767c24 DockedStackDivider (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
2019-10-26 18:00:51.190 2389-2577/system_process E/InputDispatcher: channel '8767c24 DockedStackDivider (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-10-26 18:00:51.191 2389-2409/system_process D/ConnectivityService: ConnectivityService NetworkRequestInfo binderDied(NetworkRequest [ LISTEN id=14, [] ], android.os.BinderProxy@61e49b1)
2019-10-26 18:00:51.191 2389-2409/system_process W/AudioService: Current remote volume controller died, unregistering
2019-10-26 18:00:51.192 2389-2409/system_process W/VolumeController: Error calling dismiss
    android.os.DeadObjectException
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:748)
        at android.media.IVolumeController$Stub$Proxy.dismiss(IVolumeController.java:170)
        at com.android.server.audio.AudioService$VolumeController.postDismiss(AudioService.java:6493)
        at com.android.server.audio.AudioService.setVolumeController(AudioService.java:6341)
        at com.android.server.audio.AudioService$4.binderDied(AudioService.java:6350)
        at android.os.BinderProxy.sendDeathNotice(Binder.java:828)
复制代码

最开始有意识到是Notification导致的崩溃,但是并没有想是setIcon的时候导致的,因为我开发的是大屏只是怀疑是小厂对系统封装的不好,不断检查自己的代码

发送notification方式是按照SDK的写法,所以语法和逻辑不会有问题

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(NotificationChannelId, NotificationChannelName, NotificationManager.IMPORTANCE_DEFAULT);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}
Notification.Builder builder = new Notification.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setTicker(this.getString(R.string.service_foreground_notification_ticker))
        .setContentTitle(this.getString(R.string.service_foreground_notification_title))
        .setContentText(this.getString(R.string.service_foreground_notification_text))
        .setOngoing(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    builder.setChannelId(NotificationChannelId);
}
Notification notification = builder.build();
this.startForeground(1, notification);
复制代码

后面突然开窍,开始怀疑是Notification设置读取通知图片的时候导致的绘制的bitmap读取除了问题,导致width and height < 0, 注掉

.setSmallIcon(R.mipmap.ic_launcher)
复制代码

果然成功

想到设置这一行的时候调用的是

这里的资源文件,里面的文件不是png

<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background" />
    <foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>
复制代码

解决方式:

删除mipmap-anydpi-v26文件夹,或者删除替换文件夹里的资源文件,就解决了这个问题,notification导致的问题,在推送上应该也会有该问题出现

所以怀疑是底层的调用的时候出现问题,导致bitmap没有读成功,就找了一些资料看底层的调用流程

参考调用流程 https://blog.csdn.net/u013989732/article/details/85090535