Android 调节屏幕亮度
//获取当前屏幕亮度模式
//手动调节:Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
//自动调节:Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
public static boolean isAutoBrightness(Activity activity) {
int mode = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
return true;
} else {
return false;
}
}
//关闭自动调节模式
public static void disableAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
//开启自动调节模式
public static void enableAutoBrightness(Activity activity) {
Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
//获取系统当前亮度,范围0~255
public static int getSystemBrightness(Activity activity) {
int brightness = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255/2);
return brightness;
}
//设定系统亮度
public static void setSystemBrightness(Activity activity, int brightness) {
//存储
Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
//使之生效
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
activity.getWindow().setAttributes(lp);
}
需要添加权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />