android5.0使用Notification报RemoteServiceException的解决办法
来源:互联网 发布:union软件安卓 编辑:程序博客网 时间:2024/04/26 10:15
有时android5.0下使用Notification会报如下错误信息(比如开启重启动系统就要发送通知)
android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon
这个问题多数集中在setSmallIcon(R.drawable.scanner_small)这句代码上,在某些情况下(比如开启重启动系统就要发送通知),R.drawable.scanner_small这个资源尚未准备好,导致了App异常。那怎么办呢?
这是android5.0的bug,在android4.4和6.0中都正常,一般情况下,这没办法解决,如老外说的那样
http://stackoverflow.com/questions/25317659/how-to-fix-android-app-remoteserviceexception-bad-notification-posted-from-pac
不过,如果你不介意图标大小,可以这样写:
setSmallIcon(context.getApplicationInfo().icon)
从ApplicationInfo中拿到应用icon当作SmallIcon。
总之,要抢在系统重启动之前拿到icon,而不至于拿到一个空的resId。
解决思路:
http://stackoverflow.com/questions/24968556/how-to-fix-this-bug-android-app-remoteserviceexception-bad-notification-post
附上我完整的Notification代码
android.app.RemoteServiceException: Bad notification posted from package *: Couldn't create icon: StatusBarIcon
这个问题多数集中在setSmallIcon(R.drawable.scanner_small)这句代码上,在某些情况下(比如开启重启动系统就要发送通知),R.drawable.scanner_small这个资源尚未准备好,导致了App异常。那怎么办呢?
这是android5.0的bug,在android4.4和6.0中都正常,一般情况下,这没办法解决,如老外说的那样
http://stackoverflow.com/questions/25317659/how-to-fix-android-app-remoteserviceexception-bad-notification-posted-from-pac
不过,如果你不介意图标大小,可以这样写:
setSmallIcon(context.getApplicationInfo().icon)
从ApplicationInfo中拿到应用icon当作SmallIcon。
总之,要抢在系统重启动之前拿到icon,而不至于拿到一个空的resId。
解决思路:
http://stackoverflow.com/questions/24968556/how-to-fix-this-bug-android-app-remoteserviceexception-bad-notification-post
附上我完整的Notification代码
private static final int NOTIFY_ID = 0;public static void showCustomNotification(Context context) {NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(context, MainActivity.class);//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// FLAG_ONE_SHOTintent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);PendingIntent contentIntent = PendingIntent.getActivity(context, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);NotificationCompat.Builder mBuilder = new Builder(context); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_notify); boolean isScan=(Boolean)SPUtils.get(context, App.KEY_SCAN, true); remoteViews.setTextViewText(R.id.btn_scan,isScan?"隐藏扫描键": "显示扫描键"); //点击事件处理 Intent actionIntent = new Intent(App.ACTION_NOTIFICATION); actionIntent.putExtra(App.KEY_NOTIFICATION_CLICK, isScan); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.btn_scan, pendingIntent); mBuilder.setContent(remoteViews) .setContentIntent(contentIntent) .setTicker("扫描精灵") .setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setSmallIcon(context.getApplicationInfo().icon)//采用quick fallback image.setDefaults(Notification.DEFAULT_ALL); Notification notify = mBuilder.build(); notify.flags = Notification.FLAG_NO_CLEAR;//|Notification.FLAG_ONGOING_EVENT; notificationManager.notify(NOTIFY_ID, notify);}
0 0
- android5.0使用Notification报RemoteServiceException的解决办法
- android.app.RemoteServiceException: Bad notification posted from package com.xxx 报错
- android5.0无法使用HttpClient解决办法
- android5.0无法使用HttpClient解决办法
- Android5.0 SharedElement的使用
- android5.0使用requestWindowFeature(Window.FEATURE_NO_TITLE)隐藏标题栏的报错的解决方法
- 关于错误:android.app.RemoteServiceException: Bad notification posted from package
- 自定义notification布局出现异常:android.app.RemoteServiceException
- android.app.RemoteServiceException: Bad notification posted from package
- 关于错误:android.app.RemoteServiceException: Bad notification posted from package
- android.app.RemoteServiceException: Bad notification posted from package 异常解决
- Android5.0 Toolbar和Theme的使用
- <android5.0>之dialog的使用
- Android5.0之Toobar的使用
- Android5.0之NavigationView的使用
- Android5.0之NavigationView的使用
- Android5.0之CoordinatorLayout的使用
- Android5.0之CardView的使用
- 仿微信页面切换图标颜色渐变效果
- canvas实现简单的Amaziograph效果画对称图
- sqlite3:not found 解决方法
- RecyclerView 中的 item 如何居中问题
- C++字符串数字的比较
- android5.0使用Notification报RemoteServiceException的解决办法
- java-基本数据类型 循环
- 可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
- 左中右结构的行布局
- which命令
- PhotoView点击放大图片效果
- MVC、MVP、MVVM的区别
- 让任意view具有滑动效果的SlideUp
- windows虚拟域名设置