Search code examples
androidasynchronousaccelerometeronpause

How to keep my accelerometer working on lock screen (onPause)?


I'm making an app that reads movement with Android's accelerometer, however I want it to continuously read the data even when phone is locked. Would I just have to put the same code I used in onSensorChanged() listener in the onPause() method? I read something about Async, but I'm not sure if it works with hardware applications. Which would be the better option?


Solution

  • Use Service , setup accelerometer and implement SensorEventListener and then everytime this event fires, acquire wake lock. and then send broadcast to activities.

    public SensorService extends Service implements SensorEventListener{
    

    }

    hope this helps :)