Keep android service alive using bindService
I start service using bindService method.
But when I click on back button on android emulator my service is destroyed.
How can I keep service live in whole application (I must use bindService for communitacion with activities)?
Thanks
Update Here is error log
09-10 18:28:18.352: ERROR/ActivityThread(905): Activity com.flax.trainer.RunOnly has leaked ServiceConnection com.flax.trainer.RunOnly$1@43e3fb40 that was originally bound here
09-10 18:28:18.352: ERROR/ActivityThread(905): android.app.ServiceConnectionLeaked: Activity com.flax.trainer.RunOnly has leaked ServiceConnection com.flax.trainer.RunOnly$1@43e3fb40 that was originally bound here
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.app.ActivityThread$PackageInfo$ServiceDispatcher.<init>(ActivityThread.java:1121)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:1016)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.app.ContextImpl.bindService(ContextImpl.java:863)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
09-10 18:28:18.352: ERROR/ActivityThread(905): 开发者_如何学C at com.flax.trainer.RunOnly.butonStartClickHandler(RunOnly.java:85)
09-10 18:28:18.352: ERROR/ActivityThread(905): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 18:28:18.352: ERROR/ActivityThread(905): at java.lang.reflect.Method.invoke(Method.java:521)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.view.View$1.onClick(View.java:2067)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.view.View.performClick(View.java:2408)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.view.View$PerformClick.run(View.java:8816)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.os.Handler.handleCallback(Handler.java:587)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.os.Looper.loop(Looper.java:123)
09-10 18:28:18.352: ERROR/ActivityThread(905): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-10 18:28:18.352: ERROR/ActivityThread(905): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 18:28:18.352: ERROR/ActivityThread(905): at java.lang.reflect.Method.invoke(Method.java:521)
09-10 18:28:18.352: ERROR/ActivityThread(905): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-10 18:28:18.352: ERROR/ActivityThread(905): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-10 18:28:18.352: ERROR/ActivityThread(905): at dalvik.system.NativeStart.main(Native Method)
If you use BIND_AUTO_CREATE
flag than your service will be destroyed when activity is closed.
You should use startService()
before bindService()
to prevent this
精彩评论