我使用bindService方法启动服务。
但是当我点击android模拟器上的后退按钮时,我的服务就被破坏了。
如何在整个应用程序中保持服务(我必须使用bindService进行与活动的通信)?
由于
更新 这是错误日志
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): 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)
答案 0 :(得分:4)
如果您使用BIND_AUTO_CREATE
标志,那么在关闭活动时您的服务将被销毁。
您应该在startService()
之前使用bindService()
来阻止此