简体   繁体   中英

Android Screen OFF / ON

I need to get my screen turned on/off using SensorEventListener when

    @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.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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