Android调节屏幕亮度 Android开发实现调节屏幕亮度功能
GdinKing 人气:0想了解Android开发实现调节屏幕亮度功能的相关内容吗,GdinKing在本文为您仔细讲解Android调节屏幕亮度的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,屏幕亮度,下面大家一起来学习吧。
本文实例讲述了Android开发实现调节屏幕亮度功能。分享给大家供大家参考,具体如下:
在很多app中进入二维码显示界面时会自动调整屏幕亮度,那么如何实现调节app的屏幕亮度呢?下面我来为大家介绍:
注:调节屏幕亮度的核心思想就是对安卓系统提供的ContentProvider
进行操作
1、声明权限
需要允许用户修改系统配置
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
2、具体操作
/** * 判断是否开启了自动亮度调节 */ public static boolean isAutoBrightness(Context context) { ContentResolver resolver = context.getContentResolver(); boolean automicBrightness = false; try { automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return automicBrightness; } /** * 获取屏幕的亮度 */ 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; } /** * 设置当前Activity显示时的亮度 * 屏幕亮度最大数值一般为255,各款手机有所不同 * screenBrightness 的取值范围在[0,1]之间 */ public static void setBrightness(Activity activity, int brightness) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f); activity.getWindow().setAttributes(lp); } /** * 开启关闭自动亮度调节 */ public static boolean autoBrightness(Context activity, boolean flag) { int value = 0; if (flag) { value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; //开启 } else { value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;//关闭 } return Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value); } /** * 保存亮度设置状态,退出app也能保持设置状态 */ public static void saveBrightness(Context context, int brightness) { ContentResolver resolver = context.getContentResolver(); Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness); resolver.notifyChange(uri, null); }
好了,亮度调节差不多就这样了。
另外,更多关于Android权限控制的说明可点击此处查看Android权限操作说明
希望本文所述对大家Android程序设计有所帮助。
加载全部内容