测试机:小米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

更多相关文章

  1. Android 通知之 Notification
  2. android 小米时钟的实现
  3. Android 判断通知栏权限的问题
  4. Android Context上下文理解
  5. Android上下文对象Context
  6. Android Notification通知栏、点击事件、悬浮通知的简单实现
  7. android通知栏Notification
  8. Android配置----小米手机通过wifi连接ADB调试Android应用
  9. Android中的通知Notification

随机推荐

  1. Android环境变量的设置
  2. ArcGIS API for Android(安卓)案例教程 4
  3. android 光标问题
  4. Notification(状态栏通知)详解
  5. Invalid project description
  6. 《Android开发艺术探索》读书笔记 (2) 第
  7. androidapk安装过程详解
  8. android通过chmod命令实现文件权限修改
  9. 多核软件设计:Android(安卓)& Linux multi
  10. android与HTML5相结合实现的省市县三级联