Android 如何以编程方式更改屏幕超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10748861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:50:22  来源:igfitidea点击:

How to change screen timeout programmatically?

android

提问by kakopappa

just wondering whether it's possible to change the screen timeout using code in Android

只是想知道是否可以使用 Android 中的代码更改屏幕超时

enter image description here

在此处输入图片说明

回答by Klaudo

It is simple to do.. You should learn to solve your problem from Android source code.

做起来很简单..你应该学会从Android源代码中解决你的问题。

  /**
   * set screen off timeout
   * @param screenOffTimeout int 0~6
   */
private void setTimeout(int screenOffTimeout) {
    int time;
    switch (screenOffTimeout) {
    case 0:
        time = 15000;
        break;
    case 1:
        time = 30000;
        break;
    case 2:
        time = 60000;
        break;
    case 3:
        time = 120000;
        break;
    case 4:
        time = 600000;
        break;
    case 5:
        time = 1800000;
        break;
    default:
        time = -1;
    }
    android.provider.Settings.System.putInt(getContentResolver(),
            Settings.System.SCREEN_OFF_TIMEOUT, time);
}

回答by Thira

A better solution is to do one of the following (depending on whether you want it to be dynamic or static):

更好的解决方案是执行以下操作之一(取决于您希望它是动态的还是静态的):

  1. Specify attribute android:keepScreenOnin layout (xml) (i.e. indefinitely prevent screen timeout at all times),
  2. Add the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ONflag when you create your Activity, or
  3. Use a WakeLockto control how long the screen should be up (dynamic)
  1. android:keepScreenOn在布局(xml)中指定属性(即无限期地始终防止屏幕超时),
  2. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON创建活动时添加标志,或
  3. 使用 aWakeLock来控制屏幕应该显示多长时间(动态)

Check out this blog postfor more detailed examples of these two approaches.

查看此博客文章,了解有关这两种方法的更详细示例。

回答by TheBandi

Setting the screen timeout to -1 does not seem to do an accurate job of what is required.

将屏幕超时设置为 -1 似乎不能准确完成所需的工作。

I've found that setting the value to Integer.MAX_VALUE works better.

我发现将值设置为 Integer.MAX_VALUE 效果更好。

For example:

例如:

android.provider.Settings.System.putInt(content, Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);

android.provider.Settings.System.putInt(content, Settings.System.SCREEN_OFF_TIMEOUT, Integer.MAX_VALUE);

This seems to set the max timeout to the maximum allow by the device.

这似乎将最大超时设置为设备允许的最大值。

For example, if when navigating to the display settings on your phone only allows you to have a maximum screen timeout of 30 minutes, doing the above code will set the screen timeout to 30 minutes.

例如,如果导航到手机上的显示设置时只允许最大屏幕超时为 30 分钟,则执行上述代码会将屏幕超时设置为 30 分钟。

回答by kakopappa

If anybody needs to set it to never, here is the code

如果有人需要将其设置为从不,这里是代码

Settings.System.putString(cr, Settings.System.SCREEN_OFF_TIMEOUT, "-1");