I have some problem with WindowManager.LayoutParams. I need to poweroff and poweron the screen using the proximity sensors. The proximity sensor is OK, but layoutParams doesn't work
public void setBright(float value) {
Window mywindow = getWindow();
WindowManager.LayoutParams lp = mywindow.getAttributes();
lp.screenBrightness = value;
lp.buttonBrightness = value;
if (value != WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF) {
lp.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
}
mywindow.setAttributes(lp);
}
The screen poweroff correctly. But if I use setBright(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) the screen remains black. I also tri开发者_Go百科ed using wakeLock
private void powerOnScreen() {
setBright(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
onResume();
if (!wakeLock.isHeld())
wakeLock.acquire();
}
where wakeLock is:
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE,
"MY_TAG");
but it still doesn't work. Any idea? Target platform is Android 2.2
Seems you should add the flag PowerManager.ACQUIRE_CAUSES_WAKEUP into newWakeLock call.
EDIT: That snippet worked for me:
boolean isScreenOff = !powerManager.isScreenOn();
if (isScreenOff) {
Window window = getWindow();
if (window != null) {
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
stopTimeDialogListener.wakeLock = powerManager
.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "TIMING_ALARM");
stopTimeDialogListener.wakeLock.acquire();
try {
long screenOffDelay = Settings.System.getLong(
getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT);
new Timer().schedule(
stopTimeDialogListener.releaseWakeLock,
screenOffDelay);
} catch (SettingNotFoundException e) {
}
}
精彩评论