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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章