Android 8.0开始Notification 需要指定一个channel,当target大于26时,这个channel需要进行系统注册,否则会crash,crash信息如下:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=default pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:164)at android.app.ActivityThread.main(ActivityThread.java:6494)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

修改方案

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {val channelId = "default"val channel = NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT)val nm = service.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManagernm?.let {if (it.getNotificationChannel(channelId) == null) {//没有创建it.createNotificationChannel(channel)//则先创建}}val notification: Notificationval builder = Notification.Builder(service, channelId).setContentTitle("").setContentText("")notification = builder.build()service.startForeground(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
}private void createNotificationChannel(NotificationManager manager) {if (manager.getNotificationChannel("default") == null) {NotificationChannel notificationChannel =new NotificationChannel("default","notification_channel",NotificationManager.IMPORTANCE_LOW);notificationChannel.setDescription("notification_channel_description");manager.createNotificationChannel(notificationChannel);}}

Android Crash:Bad notification for startForeground相关推荐

  1. Android WebView 优化、WebV离线包开发出现Crash: Fatal signal 5 , code 1, fault addr 0x6e1f33c798 in tid 32217

    Android WebView 优化/离线包开发出现Crash: A Fatal signal 5 , code 1, fault addr 0x6e1f33c798 in tid 32217 一.问 ...

  2. Android开发之自定义Notification(源代码分享)

    Android的自定义notification选项我觉得限制还是挺多的,如图所示,官方API告诉我们它依然必须得设置icon,tittle,text三个选项,除此之外,还一定要设置pengdingin ...

  3. 【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )

    文章目录 一.报错信息 二.解决方案 一.报错信息 使用如下代码启动前台服务 : public class ForegroundService extends Service {public Fore ...

  4. 结合源码深入理解Android Crash处理流程

    应用程序crash在开发过程中还是很常见的,本文主要是从源码的角度去跟踪下Android对于crash的处理流程.App crash的全称:Application crash.而Crash又分为:na ...

  5. android的线程管理器,[Android开源]:一款安全、轻巧、简单的线程池管理器EasyThread...

    EasyThread通过对原生的线程池进行封装,可让你更方便的进行线程任务操作. 特性 简单轻巧:方法数不过百,无额外次级依赖. 配置灵活:可方便.灵活的对每次所启动的任务,配置线程名.线程优先级等. ...

  6. 使用WebRTC开发Android Messenger:第2部分

    这是一个由三部分组成的系列文章,内容涉及:利用WebRTC中的BUG和利用Messenger应用程序.本系列文章重点阐述了当应用程序不能应用于WebRTC补丁程序以及通信和安全问题通知中断时可能出问题 ...

  7. android crash没有日志_App测试之monkey(四)-调试参数及日志

    由于monkey在测试app时,我们需要作长时间的稳定性测试,比如连续测试10小时(monkey不能指定时间,可以指定次数,时间可以在测试次数的日志基础上大概算出来),在测试过程中,app很可能测试时 ...

  8. android crash分析工具,Android Crash之Native Crash分析

    前言 上一篇给大家介绍了Android Crash中的Java Crash分析,我们可以知道Java Crash一般会弹出提示框告诉我们程序崩溃了,通常使用Crash工具都能够捕获到:本篇博客来谈谈如 ...

  9. Android状态栏语言,Android实现3种Notification(状态栏通知)

    Android实现3种Notification(状态栏通知) 点击标题下「安卓干货铺」可快速关注 Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示.当 APP 向系统发出通 ...

最新文章

  1. appium学习【二】:用try捕获异常后,用例的执行结果为pass
  2. 【杂谈】野生在左 科班在右——数据结构学习誓师贴
  3. MIT自然语言处理第五讲:最大熵和对数线性模型
  4. linux u 驱动程序,在uClinux中增加自己的设备驱动程序
  5. String对象的intern()
  6. Object类的toString方法
  7. Linux 终端仿真程序Putty
  8. 解决IE6下CSS兼容性的两把神器
  9. 提到强化学习只知道AlphaGo?其实,RL在工业界还有这些应用
  10. Asp.net 5种页面转向方法 转载
  11. macOS中安装secoclient
  12. 红米note10 pro刷机
  13. UE4 4.13 VR FPS 继续优化
  14. 网络安全求职该怎么写简历?
  15. RingBuffer
  16. 【vscode】MarkDown 插入视频标签
  17. 华为设备配置Smart Link负载分担
  18. tophat以及cufflinks的使用
  19. linux开机自启任务和定时任务
  20. pxc wsrep_sst_method均配置为xtrabackup-v2报错

热门文章

  1. 如何使用码匠连接 DynamoDB
  2. 机械振动系统的matlab仿真分析-郭
  3. 码农必备,一款超好用Json编辑工具
  4. html鉴赏古诗词代码,教你如何鉴赏古诗词
  5. 安卓开发之EventBus使用
  6. Matlab二值图像进行轮廓提取
  7. 港科夜闻|香港科技大学(广州)与广东联通签署多方位战略合作协议
  8. 十年SEO风云巨变,还有多少站长在坚持
  9. 第六章 样本与抽样分布
  10. uIP中文协议文档:Ch01