3

I'm creating an application that needs to detect the biggest acceleration that the phone detects. Currently it works, but it does not continue the task when the screen turns off. To achieve what I have now, I wrote in onCreate:

mSensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
mAccelerometer = mSensorManager!!.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION)

I have initialized these variables globally in the class. I then have implemented onSensorChanged(), onResume(), onPause() and left onAccuracyChanged empty.

From what I have understood implementing functions like this is different than more simply creating an asynchronous task. How would I go about changing this so to make it work in the background as well? Thank you!

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31
  • At best, you would need to implement a foreground service (to help keep your process running) with a partial `WakeLock` (to try to keep the phone CPU awake). Most likely you will also need the phone to be plugged in all the time, as otherwise Doze mode will interrupt your `WakeLock` and allow the CPU to stop. And, even with all of that, it will depend on whether the device manufacturer supports your desired sensors with the screen off -- that is not guaranteed. – CommonsWare Oct 16 '21 at 19:33
  • What would a rough example of that be? The app should work without having to be plugged all the time too. Thank you. – Enrico Cortinovis Oct 16 '21 at 19:45
  • "The app should work without having to be plugged all the time too" -- then I suspect that what you want is not possible across a wide range of Android devices. Some devices have the ability to react to sensor input even when the phone is asleep, but that capability is on a model-by-model basis (and even there, it might be only some sensors, not all) and is not necessarily exposed to app developers. – CommonsWare Oct 16 '21 at 19:48
  • That's strange, thank you. I will search more to hopefully find a way to make this work at least on the models where it's possible. – Enrico Cortinovis Oct 16 '21 at 19:51
  • A notification is a foreground job. Make it persist though. And using a WakeLock too might help, I think. – Darkman Oct 19 '21 at 15:20
  • Thank you for the suggestion @Darkman. I will look into that. – Enrico Cortinovis Oct 25 '21 at 04:46

1 Answers1

1

You won't be able to use the Sensors API when the app went to the background even with WakeLock - the official documentation is clear about that.

You can easily proceed with using Sensors API even with the phone screen disabled inside a foreground service, though. In order to do that use this documentation as a start - Foreground Service. This doesn't guarantee the eternal live of the Service but it will most definitely live longer than an ordinary Service as well as you will have the access to the Sensors API. I am not sure about WakeLock in this case - you will have to try(but I think you won't need it).

Here is a span of answers. Some of them contain sample code and even links to sample projects

Here and here there are neet examples in form of medium articles.

Pavlo Ostasha
  • 14,527
  • 11
  • 35
  • Thank you for the suggestions. I will try it with a foreground task. I’d like to reward the 50 rep to this answer, could you show a quick example of starting a foreground task and running an activity there? – Enrico Cortinovis Oct 25 '21 at 08:49
  • @EnricoCortinovis not foreground task, but Foreground Service and you cannot run an Activity there - you can run some code there. I will add a few links to some sample code. – Pavlo Ostasha Oct 25 '21 at 15:37
  • Thanks, I'll look into that. – Enrico Cortinovis Oct 26 '21 at 04:42