Getting data from an Unbound Service in Android

你离开我真会死。 提交于 2019-12-22 08:55:21

问题


I currently I have an unbound service that is running continually grabbing my gps position that I start on boot. I then have an app that is suppose to plot where I've been by pulling data from the service.

I can't bind the the service to talk to it or it will be destroyed once I close the app.

Is there any good way to get data from an unbound service or keep a bound service from dying once I unbind it?

Cheers! :)


回答1:


There is a workaround for keeping your service alive. Call your service by calling startService and then bind to the service. This way your activity is maintaining the the lifecycle of the service.




回答2:


As the documentation states:

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections

So first, start the service with startService(), then bind to it in onResume(), and unbind in onPause(). The service will continue to run nevertheless because it is started. And when you want to stop the service either call stopSelf() from within the service or stopService() from an activity. It will stop right away or as soon as you unbind from it if there's any connection alive.



来源:https://stackoverflow.com/questions/5597298/getting-data-from-an-unbound-service-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!