问题:Bad notification posted from package com.notify: Couldn't create icon
09-19 21:10:05.725 1600-1600/? W/ResourceType: No known package when getting value for resource number 0x2714000e
09-19 21:10:05.725 1600-1600/? E/Icon: Unable to load resource 0x2714000e from pkg=com.notify
android.content.res.Resources$NotFoundException: Resource ID #0x2714000e
at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:288)
at android.content.res.Resources.getDrawableForDensity(Resources.java:1026)
at android.content.res.Resources.getDrawable(Resources.java:969)
at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:324)
at android.graphics.drawable.Icon.loadDrawable(Icon.java:277)
at android.graphics.drawable.Icon.loadDrawableAsUser(Icon.java:388)
at com.android.systemui.statusbar.StatusBarIconView.getIcon(StatusBarIconView.java:295)
at com.android.systemui.statusbar.StatusBarIconView.getIcon(StatusBarIconView.java:273)
at com.android.systemui.statusbar.StatusBarIconView.updateDrawable(StatusBarIconView.java:259)
at com.android.systemui.statusbar.StatusBarIconView.set(StatusBarIconView.java:221)
at com.android.systemui.statusbar.BaseStatusBar.createIcon(BaseStatusBar.java:2643)
at com.android.systemui.statusbar.BaseStatusBar.createNotificationViews(BaseStatusBar.java:2598)
at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1879)
at com.android.systemui.statusbar.BaseStatusBar$7$2.run(BaseStatusBar.java:798)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
09-19 21:10:05.725 1600-1600/? W/StatusBarIconView: No icon for slot com.notify/0x309
09-19 21:10:05.726 1600-1600/? W/StatusBar: removeNotification for unknown key: 0|com.notify|777|null|10532
09-19 21:10:05.728 1167-1469/? E/AwareLog: RMS.AwareAppAssociate: Unknown EventID: 21
09-19 21:10:05.728 1167-1469/? E/AwareLog: RMS.AwareAppAssociate: Unknown EventID: 21
09-19 21:10:05.729 17885-17885/com.notify W/System.err: android.app.RemoteServiceException: Bad notification posted from package com.notify: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.notify id=0x2714000e) visible user=0 )
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2101)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at android.taobao.atlas.runtime.ActivityThreadHook.handleMessage(ActivityThreadHook.java:290)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at android.os.Handler.dispatchMessage(Handler.java:104)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at android.os.Looper.loop(Looper.java:166)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7425)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
09-19 21:10:05.729 17885-17885/com.notify W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
解决:
1、思路
从这句话中No known package when getting value for resource number 0x2714000e 获取知道,系统运行无法找到0x2714000e对应的资源,通过搜索R.java文件,发现是0x2714000e对应notify.png图片,那么明确是在运行的时候没有找到图片资源。
2、原因
这个问题一般是由于setSmallIcon(R.drawable.notify)时候,在某些情况下(比如开启重启动系统就要发送通知),R.drawable.notify这个资源尚未准备好,导致了App异常。
3、解决
从ApplicationInfo中拿到应用icon当作SmallIcon图片;
setSmallIcon(context.getApplicationInfo().icon)
总结,要抢在系统重启动之前拿到icon,而不至于拿到一个空的resId。