Search code examples
androidandroid-screen

Disable screen turning off while sensor measuring is on


I have an activity with a button "start measuring": pressing it will register this activity as sensor lister. However, I discovered that if user does not interact with it for a while screen turns off and android disables sensor listenig (in order to save battery I guess).

I discovered that it's a bit tricky to have a background thread listen for sensor events too and, as it's not a problem, I simply want to disable turning off while listener is on, and enable it again when user presses "stop measuring".


Solution

  • I had an activity that used the sensor only and the screen would turn off, i used

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    

    and to turn off

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    

    In the resume and pause of my activity