我今天已经解决了这个问题。
首先,您必须将权限放入您的 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) {
//Automatic mode
} else {
//Manual mode
}
而不是这个
if (mode == 1) {
//Automatic mode
} else {
//Manual mode
}
现在您可以手动更改亮度
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); //this will get the information you have just set...
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) br / 255; //...and put it here
getWindow().setAttributes(lp);
} catch (Exception e) {}
不要忘记许可...
<uses-permission android:name="android.permission.WRITE_SETTINGS" />