有时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代码
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_SHOT
intent.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);
}
分享到:
相关推荐
用户使用android 5.0以上的系统在安装APP时,将消息通知的权限关闭掉了。实际上用户本意只是想关闭Notification,但是Toast的show方法中有调用INotificationManager这个类,而这个类在用户关闭消息通知权限的同时被...
Supports Android 4.0 - Android 5.0. Google Play link: https://play.google.com/store/apps/details?id=com.kodarkooperativet.notificationstopwatch Source code is licensed to Apache 2.0
Android中使用Notification提示消息示例,详细可以参见博客:http://www.cnblogs.com/plokmju/p/3235659.html
Android中使用Notification在通知栏中显示通知示例代码.rar Android中使用Notification在通知栏中显示通知示例代码.rar Android中使用Notification在通知栏中显示通知示例代码.rar
安卓Android源码——notification1.rar
Android实现Notification的通知栏常驻.zip
Android 通知(notification)简单实用Demo,包含点击功能简单例子,供大家下载学习
使用android push notification service 实现即时通知
Android NOtification 使用Android NOtification 使用
可以设置一条通知,当然可以更新一条通知,我们通过在调用NotificationManager.notify(ID, notification)时所使用的ID来更新它。为了更新你之前发布的通知,你需要更新或者创建一个NotificationCompat.Builder对象,...
Android最佳实践中的Notification的示例代码,很强大的参考价值。在Android5.0之后Notification有了很多的变化,请拥抱这个变化。
这个是android notification框架,如有需要请下载。发发发
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
HeadsUp-Notificationandroid 5.0 lollipop HeadsUp Notification with Eclipse edition from #2.0 使用库实现类似的方法(向下兼容2.3)#下面介绍一个自己写的,类似 Heads-up 组件的库.simple heads-up (no expand)...
Android自定义Notification,很实用的一个功能
android notification完全解析Demo
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
android 利用notification 通知栏 实现动态下载.
android 源码学习