Android中屏幕亮度相关设置
jopen
10年前
测试Activity:
import android.app.Activity; import android.os.Bundle; import android.provider.Settings; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class TestScreenBrightnessActivity extends Activity { private int brightness; private SeekBar seekbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); seekbar = (SeekBar) findViewById(R.id.main_sb); initBrightness(); seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { brightness = seekBar.getProgress(); seekBar.setProgress(brightness); SetAndGetDataUtil.SetData(getApplicationContext(), "test", "light", brightness + ""); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int curProgress = seekBar.getProgress();// 得到当前进度值 // 当进度小于70时,设置成70,防止太黑看不见的情况。 if (curProgress < 70) { curProgress = 70; } // 根据当前进度改变屏幕亮度 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, curProgress); curProgress = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1); BrightnessUtil.setBrightness(TestScreenBrightnessActivity.this, curProgress); // BrightnessUtil.saveBrightness(TestScreenBrightnessActivity.this, // curProgress); } }); } /** * 初始化屏幕亮度值 */ private void initBrightness() { // 如果开启了自动亮度调节,则关闭之 if (BrightnessUtil.isAutoBrightness(this)) { BrightnessUtil.stopAutoBrightness(this); } // 读取文件中设置的亮度值 String light = SetAndGetDataUtil.GetData(this, "test", "light"); if (!"".equals(light)) { brightness = Integer.valueOf(light); } else { brightness = BrightnessUtil.getScreenBrightness(this); } BrightnessUtil.setBrightness(this, brightness); seekbar.setProgress(brightness); } }
存取数据工具类:SetAndGetDataUtil