How to prevent automatic screen lock on android by code?

Adam Varhegyi picture Adam Varhegyi · Jul 5, 2012 · Viewed 20.2k times · Source

In my app there is a long loading process, and if the devices puts on the screen lock, my process stops for some reason.

How can i prevent the device from automatic screen lock?

Answer

barry picture barry · Jul 5, 2012

Another way to keep the screen lock on without having to request the permission in the manifiest is:

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

Are you doing your long loading process in the UI thread? Something doesn't seem right - if the process is so long that the screen lock timesout and your process ends, perhaps it needs to go in a background service.