我今天解决了这个问题。
首先,您需要将权限添加到AndroidManifest.xml文件中:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
它应该放在文件的确切位置是什么?
<manifest>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application>
<activity />
</application>
</manifest>
该权限允许您更改影响其他应用程序的设置。
现在,您可以打开或关闭自动亮度模式。
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); //this will set the automatic mode on
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); //this will set the manual mode (set the automatic mode off)
自动模式现在是否已打开或关闭?您可以获取此信息。
int mode = -1;
try {
mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1)
} catch (Exception e) {}
如果您想手动更改亮度,您需要首先设置手动模式,然后才能更改亮度。
注:SCREEN_BRIGHTNESS_MODE_AUTOMATIC为1
注:SCREEN_BRIGHTNESS_MODE_MANUAL为0
您应该使用这个。
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
} else {
}
不要使用这个
if (mode == 1) {
} else {
}
现在你可以手动调节亮度了
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); //brightness is an integer variable (0-255), but dont use 0
并且阅读亮度
try {
int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); //returns integer value 0-255
} catch (Exception e) {}
现在一切都设置好了,但是...你仍然看不到变化。
你需要做一件事来看到变化!
刷新屏幕...这样做:
try {
int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) br / 255;
getWindow().setAttributes(lp);
} catch (Exception e) {}
不要忘记权限...
<uses-permission android:name="android.permission.WRITE_SETTINGS" />