Service启动泄露异常:android.app.ServiceConnectionLeaked

项目场景:

  • 在子线程中启动服务
  • 在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移动开发架构师
面试宝典 - 你离升职加薪仅一步之遥
坤哥杂谈 - 梦想总是要有的,万一实现了呢

Android常用开源库
Android开发知识梳理、分享

APP架构搭建 - MVP基础版
博客知识体系 - 软件开发基础

个人独立博客 https://karenchia.gitee.io
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值