WindowManager.LayoutParams和screenBrightness

时间:2011-10-06 15:05:21

标签: android

我对WindowManager.LayoutParams有一些问题。我需要使用接近传感器关闭和启动屏幕。接近传感器没问题,但layoutParams不起作用

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

屏幕正常关机。但是如果我使用setBright(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE),屏幕仍然是黑色的。我也尝试过使用wakeLock

private void powerOnScreen() {
    setBright(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE);

    onResume();
    if (!wakeLock.isHeld())
        wakeLock.acquire();
}

wakeLock是:

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(
            PowerManager.SCREEN_DIM_WAKE_LOCK
            | PowerManager.ON_AFTER_RELEASE,
            "MY_TAG");

但它仍然无效。任何的想法?目标平台是Android 2.2

1 个答案:

答案 0 :(得分:0)

似乎你应该将标志PowerManager.ACQUIRE_CAUSES_WAKEUP添加到newWakeLock调用中。

编辑: 该片段对我有用:

        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) {
            }
        }