简体   繁体   English

Android屏幕关闭/打开

[英]Android Screen OFF / ON

I need to get my screen turned on/off using SensorEventListener when 我需要使用SensorEventListener打开/关闭屏幕时,

    @Override
    public final void onSensorChanged(SensorEvent event) {
        if (event.values[0] == 0)
            turnScreenOFF();
        else if (event.values[0] == 5)
            turnScreenON();
    }

I have tried many sample code for it, but I can't get my screen turned ON again after it's turned OFF 我已经尝试了许多示例代码,但是在关闭屏幕后无法再次打开屏幕

There is the code to turn off the screen : 有关闭屏幕的代码:

        WindowManager.LayoutParams params = getWindow().getAttributes();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        params.screenBrightness = 0.0f;
        getWindow().setAttributes(params);

For screen on-off state, you can try with ACTION_SCREEN_ON and ACTION_SCREEN_OFF intents,which will come in nifty if you're making an application that might need to save state or respond to the user's screen going to sleep/waking up, etc. 对于屏幕开/关状态,您可以尝试使用ACTION_SCREEN_ONACTION_SCREEN_OFF意图,如果您要制作的应用程序可能需要保存状态或响应用户的屏幕进入睡眠/唤醒状态,那么这些意图会很漂亮。

Check out the Handling Screen ON/OFF. 签出处理屏幕的开/关。

EDITED: 编辑:

Try out below: 请尝试以下方法:

 private void unlockScreen() {
        Window window = this.getWindow();
        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    }

And call this method from onResume() . 并从onResume()调用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM