首页 > 其他分享 >android5.0使用Notification报RemoteServiceException的解决办法

android5.0使用Notification报RemoteServiceException的解决办法

时间:2023-05-01 16:09:20浏览次数:47  
标签:android5.0 Notification RemoteServiceException FLAG Intent notify context Pendin


有时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);
	}

标签:android5.0,Notification,RemoteServiceException,FLAG,Intent,notify,context,Pendin
From: https://blog.51cto.com/u_5454003/6238918

相关文章

  • 一统天下 flutter - widget 滚动类: ScrollNotification - 滚动通知
    一统天下flutterhttps://github.com/webabcd/flutter_demo作者webabcd一统天下flutter-widget滚动类:ScrollNotification-滚动通知示例如下:lib\widget\scroll\scroll_notification.dart/**ScrollNotification-滚动通知*/import'package:flutter/mater......
  • Android学习笔记(五四):通知Notification(上)
    运行在后台的Service,需要某种方式来通知用户,例如通知用户来电,通知有新的消息。这类的通知显示在statusbar上,还可以带有硬件的提醒,例如振动、LED灯闪,播放声音等等。在Android中,可以通过NotificationManager来发起一个通知。我们先看一个简单的例子,如有图所示。界面很简单,两个大butt......
  • Dialog&Notification&OptionsMenu练习
    下面的menu没有意义,仅仅是个练习而已,看图先:布局:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"androi......
  • Notification的基本用法
    android4.0以前:privatestaticfinalintNOTIFY_ID=0;notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); privatevoidshowNotification(Storestore){ Notificationnotification=newNotification(); ......
  • mac notification
    displaynotification"message"withtitle"title"subtitle"subtitle"displaynotification"message"soundname"SoundName"Validsoundnamesarethenamesofsoundslocatedin…~/Library/Sounds......
  • Redis实践操作之—— keyspace notification(键空间通知)
    源码地址:https://github.com/Tinywan/PHP_Experience一、需求分析:设置了生存时间的Key,在过期时能不能有所提示?如果能对过期Key有个监听,如何对过期Key进行一个回调处理?如何使用Redis来实现定时任务?二、序言:    本文所说的定时任务或者说计划任务并不是很多人想象中的那样,比......
  • 关于Notification的使用
    Android8(包含8)以上需要创建channelid,否则无法弹出notification:publicclassNotificationTestextendsAppCompatActivity{Stringchannel_id="myChannelId";//每个n......
  • Notification(状态栏通知)详解
    Android中用于在状态栏显示通知信息的控件:Notification,相信大部分学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2.x的,而现在普遍的Android设备基本......
  • Android学习——控件Notification
    1.创建Notification和NotificationManager 2.NotificationChannel3.常用方法说明......
  • 本地通知UserNotifications的简单使用
    有三个概念要区分下:(1)通知中心:这个是语法中的设计模式,一对多的广播通知,代码中订阅了该通知的监听者可以接受此通知进行处理(2)远程通知:也可以说是APNs通知,极光推送等,一般指......