我正在尝试创建代码,以便当用户移动SeekBar时,它会更改手机亮度(移动时)并将其设置为手机 MAIN 设置。 我已经剪断了我认为是问题的代码部分..
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress);
}
任何帮助或建议将不胜感激,谢谢:)
答案 0 :(得分:2)
您需要WindowManager来帮助您更改亮度。我的英语不好,所以我写了一个这样的示例代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScreenBrightnessSeekBar = (SeekBar)findViewById(R.id.seekBrightness);
//Brightness is a float-point number between 0 and 1,But getProess() returns Int, so, we can setMax(100), and then we div 100 later.
ScreenBrightnessSeekBar.setMax(100);
ScreenBrightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setScreenBrightness((float)seekBar.getProgress()/100);
}
});
}
private void setScreenBrightness(float b){
//get window attributes and save in layoutParams
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = b;
//display the new brightness
getWindow().setAttributes(layoutParams);
}
//新代码:
SeekBar screenBrightnessSeekBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenBrightnessSeekBar = (SeekBar)findViewById(R.id.seekBrightness);
screenBrightnessSeekBar.setMax(255);
screenBrightnessSeekBar.setProgress((int) (android.provider.Settings.System.getInt(
getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 255)));
screenBrightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, progress);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = (float)progress / 255;
getWindow().setAttributes(layoutParams);
}
}
});
}
您将需要AndroidManifest.xml中的此权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>