I have an Activity
that usually needs some time to watch the screen without interacting with it.
The problem is that the screen turns off, just like with almos any other app. I want to prevent that automatic turn off for my Activity
, like Dolphin HD does (if you configure it from the settings menu).
I want to let the user to turn off the screen pressing the usual block button, but prevent the automatic block of the device while in my Activity
.
How can I achieve this?
Thanx.
All you need to do is click on the settings menu from the notification panel or the app drawer and go to the settings icon. Now click on the Display icon. Click on Screen Timeout and click on the Never option. After Clicking on Screen timeout your phone screen will stop turning off.
From the settings page navigate to > About Tablet > Software Information. Then tap the "Build Number" 7 times to enable developer mode. Developers mode is where you'll find the Stay Awake option, toggle to enable.
Add android:keepScreenOn="true"
to some widget in your layout XML resource for this activity. So long as that widget is visible on the screen, the screen will not turn off automatically.
EDIT:
A WakeLock
, as suggested by other answers, technically will work. But then you have to manually release the WakeLock
(if you mess that up, the screen will stay on a long time). And, since you could mess it up, you need the WAKE_LOCK
permission. Using keepScreenOn
avoids all of that.
To change it on-the-fly do this:
if (keepScreenOn) getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); else getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With