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”了。
示例代碼如下:
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;
}
}
更詳細的解答見stack overflow:
http://stackoverflow.com/questions/22079909/android-java-lang-illegalargumentexception-service-not-registered
