Stopping and starting a Service based on application state

后端 未结 8 1565
北海茫月
北海茫月 2021-01-05 16:45

I have a Service which tracks the location of the user. Currently, the Service boots when the application starts and stops when the application terminates. Unfortunately,

8条回答
  •  萌比男神i
    2021-01-05 17:23

    Try using the Bound Services technique to accomplish this.

    Bound Services | Android Developers

    You can use bound services in a way such that the service will stop when no activities are bound to it. This way, when the app is not in the foreground, the service will not be running. When the user brings the app back to the foreground, the Activity will bind to the service and the service will resume.

提交回复
热议问题