Android Crash:Bad notification for startForeground
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相关推荐
- 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 一.问 ...
- Android开发之自定义Notification(源代码分享)
Android的自定义notification选项我觉得限制还是挺多的,如图所示,官方API告诉我们它依然必须得设置icon,tittle,text三个选项,除此之外,还一定要设置pengdingin ...
- 【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )
文章目录 一.报错信息 二.解决方案 一.报错信息 使用如下代码启动前台服务 : public class ForegroundService extends Service {public Fore ...
- 结合源码深入理解Android Crash处理流程
应用程序crash在开发过程中还是很常见的,本文主要是从源码的角度去跟踪下Android对于crash的处理流程.App crash的全称:Application crash.而Crash又分为:na ...
- android的线程管理器,[Android开源]:一款安全、轻巧、简单的线程池管理器EasyThread...
EasyThread通过对原生的线程池进行封装,可让你更方便的进行线程任务操作. 特性 简单轻巧:方法数不过百,无额外次级依赖. 配置灵活:可方便.灵活的对每次所启动的任务,配置线程名.线程优先级等. ...
- 使用WebRTC开发Android Messenger:第2部分
这是一个由三部分组成的系列文章,内容涉及:利用WebRTC中的BUG和利用Messenger应用程序.本系列文章重点阐述了当应用程序不能应用于WebRTC补丁程序以及通信和安全问题通知中断时可能出问题 ...
- android crash没有日志_App测试之monkey(四)-调试参数及日志
由于monkey在测试app时,我们需要作长时间的稳定性测试,比如连续测试10小时(monkey不能指定时间,可以指定次数,时间可以在测试次数的日志基础上大概算出来),在测试过程中,app很可能测试时 ...
- android crash分析工具,Android Crash之Native Crash分析
前言 上一篇给大家介绍了Android Crash中的Java Crash分析,我们可以知道Java Crash一般会弹出提示框告诉我们程序崩溃了,通常使用Crash工具都能够捕获到:本篇博客来谈谈如 ...
- Android状态栏语言,Android实现3种Notification(状态栏通知)
Android实现3种Notification(状态栏通知) 点击标题下「安卓干货铺」可快速关注 Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示.当 APP 向系统发出通 ...
最新文章
- appium学习【二】:用try捕获异常后,用例的执行结果为pass
- 【杂谈】野生在左 科班在右——数据结构学习誓师贴
- MIT自然语言处理第五讲:最大熵和对数线性模型
- linux u 驱动程序,在uClinux中增加自己的设备驱动程序
- String对象的intern()
- Object类的toString方法
- Linux 终端仿真程序Putty
- 解决IE6下CSS兼容性的两把神器
- 提到强化学习只知道AlphaGo?其实,RL在工业界还有这些应用
- Asp.net 5种页面转向方法 转载
- macOS中安装secoclient
- 红米note10 pro刷机
- UE4 4.13 VR FPS 继续优化
- 网络安全求职该怎么写简历?
- RingBuffer
- 【vscode】MarkDown 插入视频标签
- 华为设备配置Smart Link负载分担
- tophat以及cufflinks的使用
- linux开机自启任务和定时任务
- pxc wsrep_sst_method均配置为xtrabackup-v2报错