Android sets the lock screen time, the screen is always on

Add permissions.

 <uses-permission
        android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />

static constant.

private static final String SCREEN_OFF_TIMEOUT = "screen_off_timeout";

Call the system setup method

/**
     * 默认无操作后15s屏幕进入休眠
     *
     * @param isChecked 常亮开关
     */
     private void setScreenSaverEnable(boolean isChecked) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                        Uri.parse("package:" + BaseApp.getInstance().getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(intent, 1000);
            } else {
                PrfUtils.setScreenState(isChecked);
                if (isChecked) {
                    Settings.System.putInt(this.getContentResolver(), SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);//永不休眠
                } else {
                    Settings.System.putInt(this.getContentResolver(), SCREEN_OFF_TIMEOUT, 1000 * 15);
                }
            }
        }

    }

Notice:

Settings.System.putInt(this.getContentResolver(), SCREEN_OFF_TIMEOUT, 1000 * 15);
The value value will only be set according to the system. For example, the system sets the minimum lock screen time to 15s, but the code is set to 10s, and the lock screen is still 15s

Guess you like

Origin blog.csdn.net/x158454996/article/details/127362162