Android:如何在(亮度)设置更改后刷新屏幕

时间:2012-10-17 23:38:27

标签: java android refresh settings screen

我正在尝试创建代码,以便当用户移动SeekBar时,它会更改手机亮度(移动时)并将其设置为手机 MAIN 设置。 我已经剪断了我认为是问题的代码部分..

public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
    android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress);
}

如果您需要更多信息,请告诉我。它确实改变了亮度,但你必须刷新屏幕 - 按下电源按钮并再次解锁 - 让它工作,我想不出以编程方式做这个的方法

任何帮助或建议将不胜感激,谢谢:)

1 个答案:

答案 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>