user1029309
user1029309

Reputation:

Turn off/on android screen

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

Upvotes: 0

Views: 10057

Answers (2)

Kishan B Manavadariya
Kishan B Manavadariya

Reputation: 447

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();

Upvotes: 1

HardCoder
HardCoder

Reputation: 3036

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

Upvotes: 0

Related Questions