I'm making an app where a background service does some stuff on some specific motion. At the moment I use a simple Service with a SensorEventListener attached to register the movement of the phone and to do the action I wanted it to do. Im also using an (optional) wakelock without any time limitations (but it gets unatached when the service is terminated) to ensure that the service is always accessable. I used the START_REDELIVER_INTENT to ensure a restart when the activity stops it when the user stops its (regularly via the ome button). Now to my problem: When I stop the activity, the service gets terminated but only sometimes restarted (I dont know whether its not restarted or just doesnt get its sensorchanged called. I suspect last but I dont know for sure due to I only place a simple Sysout in the Sensorchanged method for testing).
Is there better and more efficient way to achive my goal (eg startForeground) and how can I ensure a continous aquiring of sensordata after an activity stop?
Services which need to run continuously have to be started with startForeground()
.
A positive sideeffect of this is that the user will be aware of your app using a sensor and can stop it if he runs low on battery.