项目场景:
- 在子线程中启动服务
- 在Activity中启动服务
问题描述:
软件在运行过程中,相关服务抛出异常:android.app.ServiceConnectionLeaked
详细日志:
Service com.xxx.xxx.alexaservice.MetroAlexaAudioProviderService has leaked ServiceConnection
com.xxx.xxx.connection.MetroConnection@32dc748 that was originally bound here
软件出现ANR,但并不会崩溃。
原因分析:
无论是在子线程,还是在Activity中,服务在启动时,与Context进行了绑定,在Context生命周期结束后,未及时解绑服务。
解决方案:
- 在Activity销毁时,解绑服务。
- 使用全局(Application)Context。
参考文章
Android应用开发——service连接泄露异常:android.app.ServiceConnectionLeaked: that was originally bound here
出现leaked ServiceConnection 的解决办法
Android ServiceConnectionLeaked异常的调查分析
往期文章推荐
成长路线 - Android移动开发架构师
面试宝典 - 你离升职加薪仅一步之遥
坤哥杂谈 - 梦想总是要有的,万一实现了呢