当按下主页按钮、查看不同的活动时,服务仍然绑定到主活动
好的,所以我编写的应用程序有一个服务(跟踪 GPS 数据),该服务有一个主要活动,该活动通过其 onStart()
方法中的 bindService 绑定到该服务,并从它的 onStart() 方法中取消与该服务的绑定。使用 unbindService(ServiceConnection)
的 onStop()
方法。
我还有一个活动,它是一个选项屏幕,通过按主要活动上的按钮启动。在此选项屏幕上,我有一个复选框,上面写着“在后台运行”,如果设置为 true,则意味着当用户使用“主页”或“后退”按钮退出应用程序时,服务将继续运行,而不是打开 GPS离开。
我通过调用 this.startForeground onUnbind
和 this.stopForeground onRebind
(如果设置设置为 true)来执行此操作,然后停止并启动我的位置读取器 onUnbind<如果设置为 false,则分别为 /code> 和
onRebind
。
如果该服务在后台运行,它还会显示一条通知,以确保用户了解 GPS 仍在运行并耗尽电量。此通知的显示和停止依赖于 startForeground
和 stopForeground
方法。
因此,问题在于,无论此设置是否正确,如果我正在查看选项屏幕,我希望服务继续运行,并且我不希望出现该通知。因此,我设置了一个标志,如果我点击按钮进入选项屏幕,则该标志设置为 true,并且如果该标志为 true,则不调用 unbindService。
除了一个小问题之外,这是可行的。如果用户在查看选项屏幕时点击主页按钮,则该服务不会停止(因为它仍然绑定到主屏幕),并且如果它应该继续运行,则不会显示警告通知(当然,这意味着该服务仍然被认为处于后台)。
我应该如何让这个应用程序按照我想要的方式运行?
Ok, so the application I've written has a service (that tracks GPS data) with a single main activity that binds to it with bindService in it's onStart()
method, and unbinds from the service in it's onStop()
method using unbindService(ServiceConnection)
.
I also have an activity which is an options screen, that is launched by pressing a button on the main activity. On this options screen, I have a checkbox that says "Run in background", which, if set to true, means that when the user exits the application with either the Home or Back buttons, the service will continue running, not turning the GPS off.
I do this by calling this.startForeground onUnbind
, and this.stopForeground onRebind
, if the setting is set to true, and stopping and starting my location reader onUnbind
and onRebind
respectively if the setting is false.
If the service is running in the background, it also displays a notification to ensure the user understands that the GPS is still running and draining their power. This notification is displayed and stopped by relying on the startForeground
and stopForeground
methods.
So, the problem lies in the fact that whether or not this setting is true, if I'm viewing the options screen, I want the service to keep going, and I do not want that notification to appear. So I set a flag that is set to true if I've hit the button to go to the options screen, and do not call unbindService if the flag is true.
This works, except for one slight problem. If the user hits the home button while viewing the options screen, the service is not stopped if it is supposed to be (because it's still bound to the main screen), and if it is supposed to keep running, it does not display the warning notification (and of course, this means that the service is still considered to be in the background).
How should I go about getting this application to behave the way I want it to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我刚刚找到了一个似乎有效的解决方案。我在活动的 onStart() 方法中将选项屏幕绑定到服务,并在 onStop() 中再次取消绑定。前一个活动的 onStop() 方法直到新活动的 onResume() 方法之后才会被调用,因此它可以工作。
但有人有更好的方法吗?将活动绑定到它实际上不需要访问的服务似乎很愚蠢。
Ok, so I just found a solution that seems to work. I bind the options screen to the service in the activity's onStart() method, and unbind it again in onStop(). The previous activity's onStop() method isn't called until after the new one's onResume() method, so it works.
Does anyone have a better way though? It seems silly to bind an activity to a service that it doesn't actually need access to.