0

I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.

  • Is there a way to make sure that my Service does not get killed?

  • Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?

Thierry jegen
  • 263
  • 3
  • 10
  • So what you have tried yet with code – Ajay Pandya Oct 01 '19 at 11:56
  • if you look at some of the other fitness/step counter kind of apps, you will find that they show a persistent notification to the user when they are running service that captures the step count - this is an example of foreground service. Android documentation states that android system tries its best (but not 100% guarantee) to keep foreground service running. So I guess your best choice is to implement foreground service and show notification to user. – AADProgramming Oct 03 '19 at 07:26

2 Answers2

1

Foreground Service is the only way if you want to assure that the service will not be killed.

The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.

All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.

Daniel
  • 2,320
  • 1
  • 14
  • 27
0

Basically you need to keep service running in the background, Here is the workaround to achieve this

https://stackoverflow.com/a/58162451/7579041

above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc

I hope this will help you out

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53