Context.startForegroundService() did not then call Service.startForeground的解决方案

Your app will crash if you call Context.startForegroundService(…) and then call Context.stopService(…) before Service.startForeground(…) is called.

Some suggestions:
在这里插入图片描述

//千年难解的老bug,搜索了一下如下文章分析的不错

stackoverflow: https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground

注重报错堆栈,和解决方案:https://blog.csdn.net/sinat_20059415/article/details/80584487

ActiveServices.java

bringDownServiceLocked
在这里插入图片描述

走到这里面继而会由ams发出一个service_foreground_crash_msg的消息,导致crash。

至于为嘛会走到这里呢,都是id = 0 的过,既没有走前台服务的流程也没有将r.fgRequired设为false,anr的msg也没有移除掉。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

anr的时限为嘛是5s呢?

在这里插入图片描述

看来已经宽限到10s了.

在这里插入图片描述

Android O 后台应用想启动服务就老老实实的加个notification给用户看,表示你自己在后台占着资源,杀不杀由用户决定,偷偷地在后台跑没有framework帮忙想都别想,一个anr+crash套餐了解一下。

1)activity: Context.startForegroundService()

2)Service:startForeground(int id, Notification notification)(id must not be 0)
————————————————
版权声明:本文为CSDN博主「i加加」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_20059415/article/details/80584487

Another 分析和总结:

源码分析:https://blog.csdn.net/lylddinghffw/article/details/80366791

startForegroundService中主要是通知创建service 并发送一个延迟5s的msg,5s(SERVICE_START_FORGROUND_TIMEOUT)后就会抛出异常。

而在service创建之后会调用oncCreate中手动添加的startForeground,它的作用是添加notification避免 “null notification”,

同时取消startForegroundService中发出的msg 避免异常“Context.startForegroundService() did not then call Service.startForeground()”。
————————————————
版权声明:本文为CSDN博主「lyldding-HFFW」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lylddinghffw/article/details/80366791

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值