Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent the screen of an android device to turn off during the execution of an Activity?

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.

like image 272
Alex Avatar asked Feb 17 '12 21:02

Alex


People also ask

How do I stop the screen from turning off?

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.

How do I keep my Android awake?

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.


2 Answers

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.

like image 115
CommonsWare Avatar answered Nov 07 '22 14:11

CommonsWare


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); 
like image 29
noelicus Avatar answered Nov 07 '22 14:11

noelicus