【Android开发经验】与屏幕亮度调节相关的各种方法整理

public class SystemManager {

	private Context mContext;
	private static SystemManager sInstance;

	private SystemManager(final Context context) {
		mContext = context;
	}

	public static SystemManager init(final Context context) {
		if (null == sInstance) {
			sInstance = new SystemManager(context);
		}
		return sInstance;
	}

	public static SystemManager getInstance() {
		return sInstance;
	}

	// 判断是否是自动调光模式
	public boolean isAutoBrightness() {
		boolean automicBrightness = false;
		try {
			ContentResolver resolver = mContext.getContentResolver();
			automicBrightness = Settings.System.getInt(resolver,
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return automicBrightness;
	}

	// 设置屏幕亮度
	public void setBrightness(Activity activity, int brightness) {
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
		activity.getWindow().setAttributes(lp);
	}

	// 保存屏幕亮度
	public void saveBrightness(int brightness) {
		ContentResolver resolver = mContext.getContentResolver();
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		android.provider.Settings.System.putInt(resolver, "screen_brightness",
				brightness);
		resolver.notifyChange(uri, null);
	}

	// 开启自动调光模式
	public void startAutoBrightness() {
		ContentResolver resolver = mContext.getContentResolver();
		Settings.System.putInt(resolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		resolver.notifyChange(uri, null);
	}

	// 关闭自动调光模式
	public void stopAutoBrightness() {
		ContentResolver resolver = mContext.getContentResolver();
		Settings.System.putInt(resolver,
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		Uri uri = android.provider.Settings.System
				.getUriFor("screen_brightness");
		resolver.notifyChange(uri, null);
	}

	// 获得当前屏幕亮度
	public int getScreenBrightness() {
		int nowBrightnessValue = 0;
		try {
			ContentResolver resolver = mContext.getContentResolver();
			nowBrightnessValue = android.provider.Settings.System.getInt(
					resolver, Settings.System.SCREEN_BRIGHTNESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nowBrightnessValue;
	}

	// 设置光亮模式
	public void setBrightnessMode(int mode) {
		Settings.System.putInt(mContext.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
	}

	// 获得亮度模式
	public int getBrightnessMode() {
		try {
			return Settings.System.getInt(mContext.getContentResolver(),
					Settings.System.SCREEN_BRIGHTNESS_MODE);
		} catch (SettingNotFoundException e) {
			return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		}
	}

}


权限

 <uses-permission android:name="android.permission.WRITE_SETTINGS" />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值