设置屏幕亮度 和 音乐音量

代码如下:


layout.xml

<SeekBar
    android:id="@+id/volumeSeekBar"
    android:layout_width="353dp"
    android:layout_gravity="center_vertical"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:progressBackgroundTint="@color/textColorGray"
    android:progressTint="#00ff2a"
    android:thumb="@drawable/dark_point"
    android:thumbOffset="11dp"
    android:max="100" />

<SeekBar
    android:id="@+id/brightnessSeekBar"
    android:layout_width="353dp"
    android:layout_gravity="center_vertical"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:progressBackgroundTint="@color/textColorGray"
    android:progressTint="#00ff2a"
    android:thumb="@drawable/dark_point"
    android:thumbOffset="11dp"
    android:max="255" />
 
  
java代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting_layout);
    init();
}

@Override
protected void onResume() {
    super.onResume();
    updateSoundAndBrightValue();
}


public void init(){

    SeekBar brightnessSeekBar = (SeekBar) findViewById(R.id.brightnessSeekBar);
    brightnessSeekBar.setProgress((int) (android.provider.Settings.System.getInt(
            getContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) ));//亮度值范围为0-255
    brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            if (fromUser) {
                saveScreenBrightness(progress);
                setScreenBrightness(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });

    SeekBar volumeSeekBar = (SeekBar) findViewById(R.id.volumeSeekBar);
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前音乐音量
    volumeSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)); //设置为音量的最大阶数
    volumeSeekBar.setProgress(volume); //设置为当前音量进度
    volumeSeekBar.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) {
            if(fromUser){
                setMusicVolume(progress);
            }
        }
    });

}

//设置屏幕亮度
private void setScreenBrightness(int value){
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    float f = value / 255.0F;
    layoutParams.screenBrightness = f;
    getWindow().setAttributes(layoutParams);
}

//保存屏幕亮度
private void saveScreenBrightness(int value){
    try{
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);
    }
    catch (Exception exception){
        exception.printStackTrace();
    }
}

//设置音量
private void setMusicVolume(int value){
    ((AudioManager) getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_MUSIC, value, 0);
}

//更新屏幕和声音数值
private void updateSoundAndBrightValue() {

    SeekBar brightnessSeekBar = (SeekBar) findViewById(R.id.brightnessSeekBar);
    brightnessSeekBar.setProgress((int) (android.provider.Settings.System.getInt(
            getContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS, 255) ));

    SeekBar volumeSeekBar = (SeekBar) findViewById(R.id.volumeSeekBar);
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //获取当前音乐音量
    volumeSeekBar.setProgress(volume); //设置为当前音量进度
}
 
  
权限:
 
  
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

你可能感兴趣的:(Android)