Skip to content

Instantly share code, notes, and snippets.

@rakkang
Last active July 14, 2022 02:33
Show Gist options
  • Save rakkang/ed9764a5d73fd1d21c48 to your computer and use it in GitHub Desktop.
Save rakkang/ed9764a5d73fd1d21c48 to your computer and use it in GitHub Desktop.
【Android】修改系统亮度
/**
* 获取当前系统亮度
**/
public int getGlobalScrennBrightness(Context context) {
int nowBrightnessValue = -1;
ContentResolver resolver = context.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(resolver,
Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
// Ignore
}
return nowBrightnessValue;
}
/**
* 判断是否开启了自动亮度调节
*/
public static boolean isAutoBrightness(ContentResolver aContentResolver) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(aContentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) ==
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}
/**
* 停止自动亮度调节
*
* @param context
*/
public void stopAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
* 开启亮度自动调节
*
* @param context
*/
public void startAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/**
* 保存亮度设置状态
*
* @param resolver
* @param brightness
*/
public void saveBrightness(ContentResolver resolver, int brightness) {
Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness);
// resolver.registerContentObserver(uri, true, myContentObserver);
resolver.notifyChange(uri, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment