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

标签 android

我使用 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/

相关文章:

java - APK 64 位兼容性

javascript - KineticJS Android 舞台尺寸

android - 如何检测用户早上开始使用手机的时间

android - Cordova CLI - 不更新 config.xml

Android:选项卡 Activity 中的 ListView

android - 存储任意排序顺序的最有效方法?

android - 在操作栏中设置 fragment 内的自定义 View

android - 是否可以从 Firebase 同步加载数据?

android - 在创建与 SQLite 数据库交互的方法/Activity 时,我应该多注意线程安全?

android - 无法合并 Dex - Android Studio 3