Android java.lang.IllegalArgumentException: Service not registered

java.lang.IllegalArgumentException: Service not registered

首先检查一下,Service是否在AndroidManifest文件中注册。格式如下:

  <service   android:name=".MyService"  ></service>

如果Service已经注册了,还是会报这个错误的话,可能是

  1. bindService没有成功,就直接unbindService;
  2. 也可能是已经unbindService成功了,还多次进行unbindService。

解决方法:

每次绑定服务时,用一个布尔值记状态为true,
解除绑定服务时,检验布尔值是否为true,如果是true,就解除服务,并把布尔值设为false,

这样即使多次解除服务,也不会报“service not registered”了。

示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

private boolean mIsBound=false ;

public void doBindService() {

  Intent bindIntent = new Intent(this, MyService.class);

   bindService(bindIntent,connection,BIND_AUTO_CREATE);

    mIsBound = true;

}

  

public void doUnbindService() {

    if (mIsBound) {

        unbindService(mConnection);

        mIsBound = false;

    }

}

转载于:https://www.cnblogs.com/expiator/p/5719774.html 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值