Android:在调用onStop()时出现java.lang.IllegalArgumentException: Service not registered。

4

我有一个应用程序,它绑定了一个长时间运行的Service

我需要确保当用户从Activity导航离开时,Service停止。

因此,我实现了onStop()方法来关闭服务:

以下是代码:

@Override    
protected void onStop() {
super.onStop();     
if(mService!=null)mService.stop();
stopService(new Intent(this, LocalService.class));
unbindService(mConnection);
stopService(intent);

}   

这是我的LogCat:

02-22 11:42:44.393: E/AndroidRuntime(1006): FATAL EXCEPTION: main
02-22 11:42:44.393: E/AndroidRuntime(1006): java.lang.RuntimeException: Unable to destroy activity {com.example.quotes/com.example.quotes.Quotes}: java.lang.IllegalArgumentException: Service not registered: com.example.quotes.Quotes$1@40cebd80
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3451)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.access$1200(ActivityThread.java:141)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.os.Looper.loop(Looper.java:137)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at dalvik.system.NativeStart.main(Native Method)
02-22 11:42:44.393: E/AndroidRuntime(1006): Caused by: java.lang.IllegalArgumentException: Service not registered: com.example.quotes.Quotes$1@40cebd80
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:921)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ContextImpl.unbindService(ContextImpl.java:1451)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.content.ContextWrapper.unbindService(ContextWrapper.java:484)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at com.example.quotes.Quotes.onDestroy(Quotes.java:420)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.Activity.performDestroy(Activity.java:5273)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
02-22 11:42:44.393: E/AndroidRuntime(1006):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3438)
02-22 11:42:44.393: E/AndroidRuntime(1006):     ... 11 more

感谢您的帮助!!!

1
onStop() 不是用来停止一个服务的,而是系统通知该服务正在被停止。 - S.D.
@User117 谢谢!但是代码中的 onStop() 是在 Activity 中的,当用户从该 Activity 导航时,它会被调用,在那个 onStop() 中,我需要停止和断开服务,调用 stopService(new Intent(this, LocalService.class));unbindService(mConnection); - Lisa Anne
2
如果您在任何地方调用了 startService(),则需要调用 stopService()。如果您只是绑定到服务,则当所有客户端从中解除绑定时,服务将自动停止。 - S.D.
如果我使用unbindService(serviceConn);,那么我就不需要使用stopService()方法了吗?@S.D. - gumuruh
1个回答

9
重点在于,在执行完onStop()之后,接下来会调用onDestroy()方法。

但是我的onDestroy()方法是:

@Override    
protected void onDestroy() {
    super.onDestroy(); 
    if(mService!=null)mService.stop();
    stopService(new Intent(this, LocalService.class));
    unbindService(mConnection);
    stopService(intent);        

 }

因此,我试图关闭服务两次。但是服务已经在onStop()断开连接了。
无论如何,谢谢。

我刚发现的一个有用的技巧是,在if语句中使用stopService(Intent)调用作为布尔值,以控制unbindService(ServiceConnection)的调用。如果服务已经不存在,stopService(Intent)会返回false。 - Tonithy
停止两次?如果我在onStop()方法中加入-> super.onStop(); 和 unbindService(myservice); 会怎么样?因为它仍然会抛出与@LisaAnne相同的错误。 - gumuruh
每次我都得到了null的mservice。 - Vasant

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接