Android如何使设备进入睡眠模式? (关闭屏幕)

时间:2019-02-05 10:29:20

标签: android system sleep wakelock sleep-mode

我正在尝试向我的应用添加功能,该功能会使设备每隔一段时间进入睡眠状态。 目前,我正在使用android.permission.WRITE_SETTINGS,但是将来,我将使用android.permission.WRITE_SECURE_SETTINGS

代码:

startWakeLockTest(); -->


dimScreenWithInterval(DIM_SCREEN_MID_LEVEL, TIMER_DIM_SCREEN_MID_LEVEL);

    private void dimScreenWithInterval(final int screenBrightnessValue, final long handlerTime) {

        mDimHandler = new Handler();
        mDimRunnable = new Runnable() {
            @Override
            public void run() {
                dimScreen(screenBrightnessValue);
                if (handlerTime == TIMER_DIM_SCREEN_MID_LEVEL) {

                    dimScreenWithInterval(DIM_SCREEN_LOW_LEVEL, TIMER_DIM_SCREEN_LOW_LEVEL);
                } else if (handlerTime == TIMER_DIM_SCREEN_LOW_LEVEL) {

                    putDeviceIntoSleepingMode();
                }

            }
        };
        mDimHandler.postDelayed(mDimRunnable, handlerTime);

    }


    private void putDeviceIntoSleepingMode() {
        turnScreenOff();
    }

       private void turnScreenOff() {
        mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sensorsTest:tag");
        mWakeLock.acquire(WAKE_LOG_TIMEOUT);
        // because there is no easy way to put the device into sleep mode - just change the screen_off_timeout in settings and then chan=nge it back after on resume
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 100);
    }

基本上,我将系统screen_off_timer更改为0,然后在屏幕重新打开时将其设置回真实设备screen_off_time。

这样做似乎很奇怪也很糟糕……

有人有更好的解决方案吗? 预先感谢

0 个答案:

没有答案