[TOC]
文章参考:https://blog.csdn.net/sinat_20059415/article/details/80584487
文章参考:https://blog.csdn.net/lylddinghffw/article/details/78219327
https://blog.csdn.net/lylddinghffw/article/details/80366791
文章参考:https://www.cnblogs.com/wangqiang9/p/10595417.html
源码分析:https://blog.csdn.net/lylddinghffw/article/details/80366791
前言:最近在处理Android O的应用crash和anr问题,其中遇到比较多的就是“Context.startForegroundService() did not then call Service.startForeground()”,将自己的处理心得总结回顾一下。
线上出现一个问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 10-28 15:48:22.038 E/ActivityManager( 2480): ANR in com.baidu.car.faceos 10-28 15:48:22.038 E/ActivityManager( 2480): PID: 3892 10-28 15:48:22.038 E/ActivityManager( 2480): Reason: Context.startForegroundService() did not then call Service.startForeground() 10-28 15:48:22.038 E/ActivityManager( 2480): Load: 7.91 / 1.87 / 0.62 10-28 15:48:22.038 E/ActivityManager( 2480): CPU usage from 3ms to 8055ms later (2020-10-28 15:48:13.836 to 2020-10-28 15:48:21.888): 10-28 15:48:22.038 E/ActivityManager( 2480): 105% 2480/system_server: 86% user + 19% kernel / faults: 3686 minor 2 major 10-28 15:48:22.038 E/ActivityManager( 2480): 77% 4186/com.baidu.xiaoduos.launcher: 67% user + 10% kernel / faults: 15014 minor 6 major 10-28 15:48:22.038 E/ActivityManager( 2480): 70% 2644/com.android.systemui: 62% user + 7.8% kernel / faults: 21468 minor 25 major 10-28 15:48:22.038 E/ActivityManager( 2480): 65% 1523//init: 65% user + 0.6% kernel 10-28 15:48:22.038 E/ActivityManager( 2480): 29% 2576/com.baidu.privacy: 18% user + 11% kernel / faults: 2645 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 16% 3612/com.baidu.iov.carid: 14% user + 2.1% kernel / faults: 2664 minor 1 major 10-28 15:48:22.038 E/ActivityManager( 2480): 15% 1170/mmcqd/0: 0% user + 15% kernel 10-28 15:48:22.038 E/ActivityManager( 2480): 14% 3892/com.baidu.car.faceos: 12% user + 2.1% kernel / faults: 2190 minor 5 major 10-28 15:48:22.038 E/ActivityManager( 2480): 9.5% 2144/surfaceflinger: 7% user + 2.4% kernel / faults: 439 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 9.4% 3299/com.baidu.iov.dueros.activate: 8.5% user + 0.9% kernel / faults: 1952 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 9.1% 2321/adbd: 1.9% user + 7.1% kernel / faults: 1555 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 7.9% 2141/logd: 2.5% user + 5.3% kernel / faults: 11 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 7.8% 3657/com.baidu.naviauto:remote: 6% user + 1.7% kernel / faults: 1896 minor 11 major 10-28 15:48:22.038 E/ActivityManager( 2480): 6.6% 3583/com.desay.kawa.fota: 5.7% user + 0.8% kernel / faults: 954 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 6.3% 2166/android.hardware.sensors@1.0-service: 2.5% user + 3.7% kernel 10-28 15:48:22.038 E/ActivityManager( 2480): 5.3% 1058/irq/175-asm330l: 0% user + 5.3% kernel 10-28 15:48:22.038 E/ActivityManager( 2480): 4.6% 2160/android.hardware.graphics.composer@2.1-service.renesas: 0.9% user + 3.7% kernel / faults: 9 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 4.3% 4021/com.baidu.che.codriver:remote: 3.3% user + 0.9% kernel / faults: 1543 minor 2 major 10-28 15:48:22.038 E/ActivityManager( 2480): 4.2% 3597/com.baidu.che.codriver: 3.4% user + 0.7% kernel / faults: 1179 minor 10-28 15:48:22.038 E/ActivityManager( 2480): 3.4% 1089/kworker/0:2: 0% user + 3.4% kernel
|
文章参考:https://blog.csdn.net/sinat_20059415/article/details/80584487
需要注意的是由于android8.0(API26)对后台运行做了限制,因此
如果处于后台时您的应用需要创建一个前台 Service,请使用 startForegroundService()。
如果 Service 容易引起用户注意,请将其设置为前台 Service。 例如,播放音频的 Service 始终应为前台 Service。 使用 startForegroundService()。
bindService启动服务的生命周期的执行顺序是onCreate–>onBind–>onUnBind–>onDestroy,与startService相比多次执行bindService启动Service并不会多次执行onBind函数。
已经设置了setForground之后,还是会出现崩溃
解决的方案:https://blog.csdn.net/KingofHacker/article/details/108093522