Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Turn off screen without going in StandBy Mode

Tags:

android

screen

I know that this question has been asked a lot of times but it has never been answered satisfactorily.

My problem is the following:

I have an activity which prevents the screen from turning off for a predefined amount of time.

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

When the predefined time is over I show a dialog with a countdown to inform the user that the display will turn off in 10 seconds if he doesnt press "cancel".

I managed to turn off the display but the phone always switches into StandBy-Mode.

For switching off I used:

Window mywindow = getWindow();

WindowManager.LayoutParams lp = mywindow.getAttributes();

lp.screenBrightness = 0.0f;

mywindow.setAttributes(lp);

Is there any possibility to completely darken the display without going to StandBy-Mode (which pauses the activity).

My goal is that the user should be able to just tap the display to brighten up the screen again. So the activity has to remain in an active state.

A similar question has been asked here.

Since this question is almost a year old I am hoping that maybe somebody managed to this in the mean time.

Lots of greetings

Siggy

like image 658
Siggy Avatar asked Jan 11 '13 08:01

Siggy


People also ask

Why is my Android phone screen not turning off?

First, open the Settings app on your phone. Second, click Display & Brightness. Then, select Screen timeout. And finally, adjust your screen timeout to 30 seconds (or 15 seconds.)


1 Answers

Seems like it isn't possible to turn off the screen AND reactivate just by touching the display.

My new approach now:

private WakeLock screenWakeLock;

PowerManager pm = PowerManager.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                "screenWakeLock");
screenWakeLock.acquire();

The PARTIAL_WAKE_LOCK keeps the CPU running but allows the display to shut down.

When power or home button is pressed the display turns on again and the activity becomes visible again (without having to "slide-to-unlock" or sth. else).

Don't forget to release the screenWakeLock.

In my case I did it in the onResume() of the activity:

if (screenWakeLock != null) {
   if(screenWakeLock.isHeld())
      screenWakeLock.release();
   screenWakeLock = null;
}

Maybe this helps someone with a similar problem in the future.

like image 123
Siggy Avatar answered Oct 09 '22 04:10

Siggy