Stopping and starting a Service based on application state

后端 未结 8 1579
北海茫月
北海茫月 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条回答
  •  误落风尘
    2021-01-05 16:58

    I have not tried this approach but I think you can override the home key of android device by using KeyEvent.KEYCODE_HOME and you can use stopService(Intent) to stop your service and when again application resumes, you can write startService(Intent) in the onResume() method of your Activity.

    This way I think your service will only stop when user explicitly presses home button to take application in the background and not when he switches from one activity to another.

提交回复
热议问题