Android Bad notification for startForeground: java.lang.RuntimeException: invalid channel for servic
16lz
2021-01-23
测试机:小米8SE,MIUI 11.0.2
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0x00000000 vis=PRIVATE)
查阅了诸多资料,发现这是Android8.0对于服务通知的变更
在8.0以后不能直接调用startservice了
改成
Intent intet1 = new Intent(MainActivity.this, LocationUtils.class); intet1.setAction("android.intent.action.RESPOND_VIA_MESSAGE"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { //android8.0以上通过startForegroundService启动service startForegroundService(intet1); } else { startService(intet1); }
在开启通知的时候
Intent nfIntent = new Intent(this, MainActivity.class); Notification.Builder builder = new Notification.Builder(this.getApplicationContext()) .setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标 .setContentTitle(getResources().getString(R.string.app_name)) .setContentText("正在上传...") // 设置上下文内容 .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间 String CHANNEL_ONE_ID = "com.primedu.cn"; String CHANNEL_ONE_NAME = "Channel One"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { //修改安卓8.1以上系统报错 NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN); notificationChannel.enableLights(false);//如果使用中的设备支持通知灯,则说明此通知通道是否应显示灯 notificationChannel.setShowBadge(false);//是否显示角标 notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.createNotificationChannel(notificationChannel); builder.setChannelId(CHANNEL_ONE_ID); } Notification notification = builder.build(); // 获取构建好的Notification notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音 startForeground(1, notification);
在mainfest中添加
参考:https://www.jianshu.com/p/7691957536a2
https://blog.csdn.net/qq_15527709/article/details/78853048
更多相关文章
- Android 通知之 Notification
- android 小米时钟的实现
- Android 判断通知栏权限的问题
- Android Context上下文理解
- Android上下文对象Context
- Android Notification通知栏、点击事件、悬浮通知的简单实现
- android通知栏Notification
- Android配置----小米手机通过wifi连接ADB调试Android应用
- Android中的通知Notification