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