如何在Android中检索当前系统的屏幕亮度值?

Ran*_*hts 3 android brightness

我正在尝试创建一个将以编程方式设置android设备亮度的应用程序。我设法使用以下代码更改设备的亮度:

BackLightValue = (float)arg1/100;
BackLightSetting.setText(String.valueOf(BackLightValue));

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
Run Code Online (Sandbox Code Playgroud)

我已使用以下代码检索当前的系统亮度值:

try {
    BackLightValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch(Exception e) {}
Run Code Online (Sandbox Code Playgroud)

上面的代码应该给我当前的亮度设置值,但是当我启动一个应用程序时,它会显示默认值50%。

怎么了?

Chi*_*rag 6

这是正确的行为,因为将screenBrightness设置为-1会将亮度设置为当前系统亮度级别。

int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS,-1);
Run Code Online (Sandbox Code Playgroud)

清单文件中的权限。

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Run Code Online (Sandbox Code Playgroud)

  • 嗯,您不需要权限即可调用`getInt()`。仅当您要调用`putInt()`时才需要它 (5认同)