android 设置系统屏幕亮度

关于亮度设置,网上有2种方式:设置系统亮度和设置当前Activity亮度。

1)设置Activity亮度
这个网上代码可以使用,本文就不说了。

2)设置系统亮度
试了网上的各种方案,都没有达到预期的效果。实际调试时,发现调节系统亮度时,亮度没有渐变效果,只是修改了系统的亮度值(就是设置后,去手机设置界面查看亮度,发现值已修改)。
后来参考了github上一个例子,它把1)和2)结合起来使用,达到成功设置系统亮度的效果。

即:滑动亮度滑块时,通过实时改变当前Activity亮度来实现亮度渐变效果,最后保存时再改变系统的亮度值。参考例子网址:[url]https://github.com/malinkang/BrightnessDemo[/url]

本来以为一切都OK了,后来测试时发现:如果改变了Activity的亮度,再去手机设置里修改亮度,则对Activity无效。即:如果Activity设置了自己的亮度,则优先使用自己的亮度。未设置才会使用系统的亮度。

最后实现方式如下:滑动亮度滑块时,通过修改Activity亮度来达到亮度实时渐变的效果,点击确定后,则保存系统亮度,同时恢复Activity的亮度设置(值重置为-1)。

代码如下(项目用到了androidannotations library):

/**
* 系统亮度设置
*
* @author qiw
* @version 1.0, 2013-9-13
*/
@EBean(scope = Scope.Singleton)
public class BrightnessSettings {
private static final String TAG = BrightnessSettings.class.getSimpleName();

@RootContext
Context context;

/**
* 获得当前系统的亮度模式
* SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
* SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
*/
public int getBrightnessMode() {
int brightnessMode = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
try {
brightnessMode = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
} catch (Exception e) {
Log.e(TAG, "获得当前屏幕的亮度模式失败:", e);
}
return brightnessMode;
}

/**
* 设置当前系统的亮度模式
* SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
* SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
*/
public void setBrightnessMode(int brightnessMode) {
try {
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightnessMode);
} catch (Exception e) {
Log.e(TAG, "设置当前屏幕的亮度模式失败:", e);
}
}

/**
* 获得当前系统的亮度值: 0~255
*/
public int getSysScreenBrightness() {
int screenBrightness = MAX_BRIGHTNESS;
try {
screenBrightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Exception e) {
Log.e(TAG, "获得当前系统的亮度值失败:", e);
}
return screenBrightness;
}

/**
* 设置当前系统的亮度值:0~255
*/
public void setSysScreenBrightness(int brightness) {
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
resolver.notifyChange(uri, null); // 实时通知改变
} catch (Exception e) {
Log.e(TAG, "设置当前系统的亮度值失败:", e);
}
}

/**
* 设置屏幕亮度,这会反映到真实屏幕上
*
* @param activity
* @param brightness
*/
public void setActScreenBrightness(final Activity activity, final int brightness) {
final WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.screenBrightness = brightness / (float) MAX_BRIGHTNESS;
activity.getWindow().setAttributes(lp);
}

/**
* 显示调节亮度的设置对话框
*
* @param act
*/
public void showBrightnessSettingsDialog(final Activity act) {
if (act == null) {
return;
}
final int oldBrightness = getSysScreenBrightness(); // 当前系统的亮度
final int oldBrightnessMode = getBrightnessMode(); // 当前系统的亮度模式
boolean autoMode = (oldBrightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); // 是否自动调节亮度模式
LayoutInflater inflater = act.getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.seekbar_dialog, null);
CheckBox cbAutoMode = (CheckBox) dialogLayout.findViewById(R.id.cb_auto_mode); // 自动调节亮度
cbAutoMode.setChecked(autoMode);
final SeekBar seekbar = (SeekBar) dialogLayout.findViewById(R.id.seekbar); // 手动调节亮度
int progress = oldBrightness - MIN_BRIGHTNESS; // SeekBar的值范围:0~225,代表的亮度值是30~255。
seekbar.setProgress(progress < 0 ? 0 : progress);
seekbar.setMax(MAX_BRIGHTNESS - MIN_BRIGHTNESS); // 最大值:225
// 自动调节亮度选项改变时
cbAutoMode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
seekbar.setVisibility(View.GONE);
setBrightnessMode(Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
seekbar.setVisibility(View.VISIBLE);
setBrightnessMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
}
});
// 手动调节亮度滑块滑动时
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int brightness = progress + MIN_BRIGHTNESS; // 实际亮度
// 亮度滑块滑动时,实时改变屏幕亮度
setActScreenBrightness(act, brightness); // 改变当前屏幕亮度
setSysScreenBrightness(brightness); // 改变系统屏幕亮度
}
});
// 是否显示手动调节亮度滑块
if (autoMode) {
seekbar.setVisibility(View.GONE);
}
// 显示调节亮度的dialog
AlertDialog.Builder builder = new AlertDialog.Builder(act);
builder.setTitle(R.string.brightness_setting);
builder.setView(dialogLayout);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// 点击确定按钮,恢复activity亮度设置
setActScreenBrightness(act, -MAX_BRIGHTNESS);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// 点击取消按钮,则需要还原亮度模式和亮度值的设置
recoverBrightnessSetting(act, oldBrightnessMode, oldBrightness);
}
});
builder.setOnCancelListener(new OnCancelListener() { // 点击返回键/dialog外部等让dialog消失的事件
@Override
public void onCancel(DialogInterface dialog) {
recoverBrightnessSetting(act, oldBrightnessMode, oldBrightness);
}
});
builder.show();
}

/**
* 还原亮度模式和亮度值的设置
*
* @param act
* @param brightnessMode
* @param brightness
*/
private void recoverBrightnessSetting(final Activity act, final int brightnessMode, final int brightness) {
setBrightnessMode(brightnessMode);
setSysScreenBrightness(brightness);
setActScreenBrightness(act, -MAX_BRIGHTNESS);
}

/** 可调节的最小亮度值 */
public static final int MIN_BRIGHTNESS = 30;
/** 可调节的最大亮度值 */
public static final int MAX_BRIGHTNESS = 255;

}

调节亮度对话框布局文件(seekbar_dialog.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<CheckBox
android:id="@+id/cb_auto_mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/auto_brightness_setting"
android:textSize="14sp" />

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cb_auto_mode"
android:max="225"
android:paddingBottom="10dp" />

</RelativeLayout>

实际效果截图:
[img]http://dl2.iteye.com/upload/attachment/0090/5229/70aa9bf1-ea4e-38e5-a23b-ba216117ada1.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值