Reputation: 2333
I have written code to turn screen on/off based on the brightness. I am able to turn screen off but when screen is turned on the brightness is updated to 1 but screen doesn't wakes up and shows itself. I have to use the manual lock/unlock button on the device.
Is there something missing from my code? I am using Android 2.3.4
if (command.equals("ON")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
onResume();
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 1;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness ON: "
+ WMLP.screenBrightness);
} else if (command.equals("OFF")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
WindowManager.LayoutParams screenBrightness = getWindow()
.getAttributes();
screenBrightness.screenBrightness = 0;
screenBrightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(screenBrightness);
}
});
WMLP = getWindow().getAttributes();
System.out.println("Screen Brightness OFF: "
+ WMLP.screenBrightness);
}
Upvotes: 0
Views: 1872
Reputation: 447
To programmatically turn off the screen and turn on the screen you can use Device Policy Manager.
To turn on the screen you can use PowerManager WakeLock (Wakelock is deprecated but currently working fine).
Use DevicePolicyManager.locknow();
to lock the screen. For that you need to register DevicePolicyManager
in you app. A tutorial can be found here.
and use the code below to unlock the device.
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