Android: `java.lang.IllegalArgumentException: Service non inscrit " quand onStop() appelée

J'ai une application qui est liée à une longue course Service.
J'ai besoin de faire en sorte que lorsque l'utilisateur quitte la Activity la Service s'arrête.

Donc j'ai mis en place le onStop() pour fermer le service:

voici le code:

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

}   

Voici mon 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

Merci pour votre aide!!!

onStop() est pas pour vous arrêter un Service, son Système pour notifier que le service est arrêté.
Merci! MAIS le onStop() dans le code est dans l'Activité, elle est appelée lorsque l'utilisateur navigue de l'Activité, et en ce que onStop() j'ai besoin d'arrêter et débrancher le service d'appel stopService(new Intent(this, LocalService.class)); et unbindService(mConnection);
Si vous avez appelé startService() n'importe où, vous devez appeler stopService(). Si vous venez de lier de service, le service s'arrête automatiquement lorsque tous les clients de l'onu-lié.
ce qui signifie que si j'utilise unbindService(serviceConn); je n'ai pas besoin d'utiliser stopService() la méthode @S. D. ?

OriginalL'auteur Lisa Anne | 2013-02-22