gpt4 book ai didi

android - 使用 bindService 使 android 服务保持 Activity 状态

转载 作者:行者123 更新时间:2023-11-29 16:25:54 25 4
gpt4 key购买 nike

我使用 bindService 方法启动服务。

但是当我点击 android 模拟器上的后退按钮时,我的服务被破坏了。

如何让服务在整个应用程序中保持 Activity 状态(我必须使用 bindService 与 Activity 进行通信)?

谢谢

更新这是错误日志

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)

最佳答案

如果您使用 BIND_AUTO_CREATE 标志,那么您的服务将在 Activity 关闭时被销毁。您应该在 bindService() 之前使用 startService() 来防止这种情况

关于android - 使用 bindService 使 android 服务保持 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3686048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com