0

I am trying to turn the android screen off and then on again, after a few seconds. The "turn off" part works, with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;
layoutParam.screenBrightness = 0; 
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

But then, when I try to turn the screen on again, it does not work with this code:

WindowManager.LayoutParams layoutParam = getWindow().getAttributes();
layoutParam.screenBrightness = oldBrightness;
layoutParam.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(layoutParam);

Any idea to solve that ?

thanks

2 Answers2

1

I think U can try Powermanager WakeLock maybe it will work. I m using this code in my application. and it works well. :)

Also u need to set permission in manifest.

<uses-permission android:name="android.permission.WAKE_LOCK"/> // Manifest Permission

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
                WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
                wakeLock.acquire();
0

First off, are you sure about the "/255f" in this line:

oldBrightness = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS)/255f;

What is the value in "oldBrightness" when you get/set it ?

Maybe you could try this:

PowerManager.WakeLock lck = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
lck.acquire();

The normal wake lock doesn't turn the screen on but causes it to stay on when a user causes it. But this flag forces the screen to turn on immediately. It requires the "android.permission.WAKE_LOCK".

More about it:

http://developer.android.com/reference/android/os/PowerManager.html#PARTIAL_WAKE_LOCK

And the screen properties (on, off, bright, dim, etc.):

http://developer.android.com/reference/android/os/PowerManager.html

HardCoder
  • 3,026
  • 6
  • 32
  • 52
  • android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN... return a value between 0 and 255, and layoutParam.screenBrightness wait for 0 and 1. With your code the screen is turned on only if I shut it down manually, not with my code. So I try to use a wake lock to turn off also, but it does not turn off like this: PowerManager pm = (PowerManager) SecureStopwatchExecActivity.this.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag"); wl.acquire(); wl.release(); –  May 06 '12 at 13:30