打开/关闭屏幕 - android

时间:2014-06-04 07:49:42

标签: android android-windowmanager

我已编写代码根据亮度打开/关闭屏幕。我可以关闭屏幕,但是当屏幕打开时,亮度更新为1,但屏幕不会唤醒并显示自己。我必须使用设备上的手动锁定/解锁按钮。

我的代码中是否缺少某些内容?我使用的是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);
            }

1 个答案:

答案 0 :(得分:1)

要以编程方式关闭屏幕并打开屏幕,您可以使用设备策略管理器。

要打开屏幕,您可以使用PowerManager WakeLock(Wakelock已被弃用,但目前正常工作)。

使用DevicePolicyManager.locknow();锁定屏幕。为此,您需要在您的应用中注册DevicePolicyManager。一个tutorial can be found here

并使用以下代码解锁设备。

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