Android调节手机亮度
Android 大约 3144 字- 程序退出之后亮度依旧有效 : setSystemBrightness
- 程序退出之后亮度失效 : setCurWindowBrightness
需添加权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
public class BrightnessUtils {
// 判断是否开启了自动亮度调节
public static boolean IsAutoBrightness(Context context) {
boolean IsAutoBrightness = false;
try {
IsAutoBrightness = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System
.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return IsAutoBrightness;
}
// 获取当前屏幕的亮度
public static int getScreenBrightness(Context context) {
int nowBrightnessValue = 0;
ContentResolver resolver = context.getContentResolver();
try {
nowBrightnessValue = android.provider.Settings.System.getInt(
resolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
e.printStackTrace();
}
return nowBrightnessValue;
}
// 设置亮度
// 程序退出之后亮度失效
public static void setCurWindowBrightness(Context context, int brightness) {
// 如果开启自动亮度,则关闭。否则,设置了亮度值也是无效的
if (IsAutoBrightness(context)) {
stopAutoBrightness(context);
}
// context转换为Activity
Activity activity = (Activity) context;
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
// 异常处理
if (brightness < 1) {
brightness = 1;
}
// 异常处理
if (brightness > 255) {
brightness = 255;
}
lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
activity.getWindow().setAttributes(lp);
}
// 设置系统亮度
// 程序退出之后亮度依旧有效
public static void setSystemBrightness(Context context, int brightness) {
// 异常处理
if (brightness < 1) {
brightness = 1;
}
// 异常处理
if (brightness > 255) {
brightness = 255;
}
saveBrightness(context, brightness);
}
// 停止自动亮度调节
public static void stopAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
// 开启亮度自动调节
public static void startAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
// 保存亮度设置状态
public static void saveBrightness(Context context, int brightness) {
Uri uri = android.provider.Settings.System
.getUriFor("screen_brightness");
android.provider.Settings.System.putInt(context.getContentResolver(),
"screen_brightness", brightness);
context.getContentResolver().notifyChange(uri, null);
}
}
阅读 1591 · 发布于 2019-04-14
————        END        ————
扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
名词解释:ETL阅读 708
-
走进Rust:配置IDEA开发环境阅读 1080
-
微信小程序渲染富文本HTML标签阅读 828
-
Java 进程间通讯的几种方式阅读 3
-
Java synchronized锁字符串注意点阅读 383
-
Linux之CentOS glibc版本过低安装不了软件阅读 2172
-
MySQL外键阅读 541
-
AndroidStudio module中依赖arr后,报failed to resolve错误的解决方法阅读 708
-
Spring Boot获取resource目录下的文件阅读 760
-
Notepad++保存Ctrl-S显示DC3,搜索Ctrl-F显示ACK解决办法阅读 3002