gpt4 book ai didi

java - 您如何检测用户是否启用了自动亮度?

转载 作者:行者123 更新时间:2023-12-05 00:19:30 25 4
gpt4 key购买 nike

这应该是一个易于搜索的问题,但我找不到一个结果。我可以找到如何检索屏幕亮度,但不知道如何检索自动亮度的状态。
第二个问题,Android Studio 强制我用 try/catch block 包围这个语句,我猜这意味着它不会一直工作。有没有更可靠的方法来检索屏幕亮度?

int currentBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
当我将鼠标悬停在 getInt 上时,我收到以下消息:未处理的异常:android.provider.Settings.SettingNotFoundException
我必须把它变成这样:
try {

int currentBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);

} catch (Settings.SettingNotFoundException e) {

e.printStackTrace();
}

最佳答案

how to retrieve the status of auto brightness


如果您按住 Ctrl 并单击 SCREEN_BRIGHTNESS在 Android Studio 中你可以看到源代码。正下方 SCREEN_BRIGHTNESS还有 SCREEN_BRIGHTNESS_MODE和两种模式的常量:
/** Control whether to enable automatic brightness mode. */
public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";

/** SCREEN_BRIGHTNESS_MODE value for manual mode. */
public static final int SCREEN_BRIGHTNESS_MODE_MANUAL = 0;

/** SCREEN_BRIGHTNESS_MODE value for automatic mode. */
public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
另见 Settings.java in Android Code Search .

Android Studio forces me to surround this statement with a try/catch block, which I guess means it won't work all the time.


Android Studio 不会强制你做任何事情,它会试图告诉你一些事情,很可能,你应该做一些不同的事情。至少,记录异常,以便您可以在您的 SO 问题中分享它,以便人们可以帮助您弄清楚发生了什么。
使用确切的异常堆栈跟踪和更多代码更新您的问题。

android.provider.Settings.SettingNotFoundException

SettingNotFoundException发生在 getInt什么时候
  • 设置未设置
  • 设置但不是数字

  • 如果您不想处理它,请使用带有 3 个参数且不抛出的重载:
    ContentResolver contentResolver = getContext().getContentResolver();
    int currentBrightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, /* default value */ 0);
    根据 documentation :

    The default value will be returned if the setting is not defined or not an integer.

    关于java - 您如何检测用户是否启用了自动亮度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62744414/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com