1

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 tried 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

Sarbyn
  • 340
  • 1
  • 13

1 Answers1

0

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) {
            }
        }
slkorolev
  • 5,883
  • 1
  • 29
  • 32
  • I use a similar snippet in order to wake up the phone and it works. But if I use lp.screenBrightness = 0.0f the wakelock doesn't work. So it's not a problem of wakelock but a bug of LayoutParams? – Sarbyn Oct 11 '11 at 09:48
  • When first playing with power on/off I noticed that the screen goes off as soon as I release wakelock, that's why I added timer. – slkorolev Oct 11 '11 at 17:20
  • **Was hasty to press Enter when editing previous comment.** Now I feel I haven't understood your intention correctly. You want to switch off the screen? or you want to turn it on but not with full brightness? Just a note: when first playing with wakelock I noteced that screen is off the moment when I release the lock, that's why I added timer. – slkorolev Oct 11 '11 at 17:27
  • I want to power off the screen and power on the screen using the proximity sensor. So use setBright(0.0f) in order to power off the screen when the phone is near my face, and use the wakelock in order to poweron the screen. But the setBright(0.0f) doesn't allow the screen to power on – Sarbyn Oct 13 '11 at 15:28
  • Found another **[thread on stackoverflow](http://stackoverflow.com/questions/3018716/android-turn-off-screen-when-close-to-face)** on proximity. Did you read it? – slkorolev Oct 13 '11 at 16:22