码迷,mamicode.com
首页 > 移动开发 > 详细

Android调节屏幕亮度工具类BrightnessUtils

时间:2015-05-16 18:23:42      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:android   屏幕亮度   brightnessutils   

如需转载请标明出处:http://blog.csdn.net/itas109

QQ技术交流群:129518033

 

 

项目需要做了一个调节屏幕的工具类

/*
 * Android调节屏幕亮度工具类
 * by itas109
 * http://blog.csdn.net/itas109
 * 
 * 注意:需要添加setting权限
 * <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 (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);
	}

}


运行效果图

技术分享

 

工程下载地址:

http://download.csdn.net/detail/itas109/8706979

 

如需转载请标明出处:http://blog.csdn.net/itas109

QQ技术交流群:129518033

 

Android调节屏幕亮度工具类BrightnessUtils

标签:android   屏幕亮度   brightnessutils   

原文地址:http://blog.csdn.net/itas109/article/details/45769489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!