一、背光灯的实现目标
实现一个滑块,滑动滑块可以进行背光的控制
二、程序编写
1、实现滑块功能
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:max="100"
android:progress="50"
/>
2、实例化滑块
private SeekBar mBacklightSeekBar = null;
3、实现滑块的监听
/*监听方法*/
mBacklightSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener (){
@Override
/*在滑动的过程中响应函数*/
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
/*在开始滑动响应函数*/
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
/*在停止滑动响应函数*/
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
4、实现滑动过程的响应函数
/*在滑动的过程中响应函数*/
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
/*取出当前值*/
int brightness = mBacklightSeekBar.getProgress();
/*转换当前值*/
brightness = brightness * 255 / 100;
/*设置亮度*/
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
brightness);
}
5、设置滑块的显示变化
try {
/*关闭自动调光功能*/
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
/*取出数据库的亮度值*/
int brightness = android.provider.Settings.System.getInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
/*设置滑块的亮度值*/
mBacklightSeekBar.setProgress(brightness*100/255);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
6、添加写权限
在AndroidManifest.xml文件中添加
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>