简体   繁体   中英

Turning screen on/off - android

I have written code to turn screen on/off based on the brightness. I am able to turn screen off but when screen is turned on the brightness is updated to 1 but screen doesn't wakes up and shows itself. I have to use the manual lock/unlock button on the device.

Is there something missing from my code? I am using Android 2.3.4

            if (command.equals("ON")) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        onResume();
                        WindowManager.LayoutParams screenBrightness = getWindow()
                                .getAttributes();
                        screenBrightness.screenBrightness = 1;
                        screenBrightness.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
                        screenBrightness.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
                        screenBrightness.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
                        getWindow().setAttributes(screenBrightness);
                    }
                });
                WMLP = getWindow().getAttributes();
                System.out.println("Screen Brightness ON: "
                        + WMLP.screenBrightness);
            } else if (command.equals("OFF")) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        WindowManager.LayoutParams screenBrightness = getWindow()
                                .getAttributes();
                        screenBrightness.screenBrightness = 0;
                        screenBrightness.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                        getWindow().setAttributes(screenBrightness);
                    }
                });
                WMLP = getWindow().getAttributes();
                System.out.println("Screen Brightness OFF: "
                        + WMLP.screenBrightness);
            }

To programmatically turn off the screen and turn on the screen you can use Device Policy Manager.

To turn on the screen you can use PowerManager WakeLock (Wakelock is deprecated but currently working fine).

Use DevicePolicyManager.locknow(); to lock the screen. For that you need to register DevicePolicyManager in you app. A tutorial can be found here .

and use the code below to unlock the device.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
                WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                                                 | PowerManager.ACQUIRE_CAUSES_WAKEUP
                                                 | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
                wakeLock.acquire();

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