13

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

Community
  • 1
  • 1
Siggy
  • 752
  • 1
  • 5
  • 26
  • Have you tried [`PowerManager`](http://developer.android.com/reference/android/os/PowerManager.html) ? That might help you turn screen on and off. Though I fear it might disable your activity receiving touch events too. – S.D. Jan 11 '13 at 08:52
  • 1
    Apart from being possible or not, I'm not sure if this is a good idea to implement. When the screen goes off users expect that it is on stand by. Meaning that no other processes are being run that could potentially drain battery life. But also that the device is locked and a pin code or something has to be filled in to unlock. You shouldn't be afraid of "pausing" an application. Actually it will be "stopped" considering the Android Activity Lifecycle. Paused is when a dialog is opened for example, not when the device goes on stand by. – Stephan Celis Jan 11 '13 at 08:54
  • @Singularity I am currently playing around with `PowerManager` and `WakeLocks` a bit but I couln't achieve the exact handling I'd like to have yet. – Siggy Jan 11 '13 at 09:01
  • @StephanCelis I know that this kind of implementation isn't the correct handling but it is a feature requested by the customer I am working for. If it isn't possible I have to find another solution to satisfy the needs. – Siggy Jan 11 '13 at 09:03
  • You might want to check out [this solution][1] [1]: http://stackoverflow.com/a/16034100/1195076 – skubo May 24 '13 at 12:05

2 Answers2

8

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.

badp
  • 11,409
  • 3
  • 61
  • 89
Siggy
  • 752
  • 1
  • 5
  • 26
  • 1
    +1 but note that the wake lock is deprecated as of API 13 (you can still use it though in higher version if you specify minSDK lesser than 13). – ripopenid Nov 01 '13 at 02:02
  • In newer versions of the API, you have to use it more like this: `PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE);` – forresthopkinsa May 10 '17 at 19:58
2

Note : i wasn't able to work with WAKELOCK

I have figured a workaround which involves changing SCREEN_OFF_TIMEOUT. Use the below code to achieve it.

Settings.System.putInt(getContentResolver(), 
                           Settings.System.SCREEN_OFF_TIMEOUT, 10);

this sets 10 milliseconds as timeout for screen_timeout SYSTEM-WIDE .

Now you might be troubled by SYSTEM-WIDE changes brought upon by this. To work around that you can get the default screen_timeout_time and save it to a variable then set back the System's SCREEN_OFF_TIMEOUT at finish() of your activity.

Before setting 10ms as our screen_timeout get SCREEN_OFF_TIMEOUT,

int defaultScreenTimeout= android.provider.Settings.
      System.getInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,-1);

Now when you have finished with your changes or when your activity ends you may set the SCREEN_OFF_TIMEOUT back .

@Override
public void finish(){
    Settings.System.putInt(getContentResolver(), 
                      Settings.System.SCREEN_OFF_TIMEOUT, defaultScreenTimeout);
super.finish();
}
Deepak Negi
  • 893
  • 10
  • 19