错误日志:
12-30 19:08:57.524325 5973 5973 E AndroidRuntime: Process: com.blackshark.performancemaster, PID: 597312-30 19:08:57.524325 5973 5973 E AndroidRuntime: Process: com.blackshark.performancemaster, PID: 597312-30 19:08:57.524325 5973 5973 E AndroidRuntime: java.lang.RuntimeException: Unable to stop service com.blackshark.performancemaster.dock.performancecompass.FloatPluginService@efe9523: java.lang.IllegalArgumentException: Service not registered: com.blackshark.performancemaster.commom.service.DivingServiceImpl$Companion$serviceConnection$1@6244fd912-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ActivityThread.handleStopService(ActivityThread.java:4807)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ActivityThread.access$2200(ActivityThread.java:265)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2150)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:210)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.os.Looper.loop(Looper.java:299)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8122)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:565)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Service not registered: com.blackshark.performancemaster.commom.service.DivingServiceImpl$Companion$serviceConnection$1@6244fd912-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1899)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ContextImpl.unbindService(ContextImpl.java:2030)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.content.ContextWrapper.unbindService(ContextWrapper.java:856)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at com.blackshark.performancemaster.commom.service.DivingServiceImpl$Companion.unRegisterSnorkelingMode(DivingServiceImpl.kt:106)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at com.blackshark.performancemaster.dock.performancecompass.FloatPluginService.onDestroy(FloatPluginService.kt:128)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: at android.app.ActivityThread.handleStopService(ActivityThread.java:4787)12-30 19:08:57.524325 5973 5973 E AndroidRuntime: ... 9 more
1.先检查自己这个服务有没有注册,没有注册参考以下
清单文件要注册服务,这样才能用啊,不是写了类就能用的
服务需要注册,四大组件啊,<service android:name=".services.BackgroundService" android:exported="true" />
2.如果服务已经注册了,还报这样的错,往下看:
每次绑定服务时,标记一个全局变量布尔值记状态为true, 解除绑定服务时,检验布尔值是否为true,如果是true,就解除服务,并把布尔值设为false,这样即使多次点击解除服务,也不会报“service not registered”了。 示例代码如下:
private boolean mIsState ;
public void doBindService() {
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
mIsState = true;
}
public void doUnbindService() {
if (mIsState) {
unbindService(mConnection);
mIsState = false;
}
}
如果以上解决了你的问题,请点个小赞,还有疑问请留言