2

在我的应用程序中,我添加了一个SeekBar控制屏幕亮度的。它工作得很好。我获取设备的屏幕亮度Oncreate()并设置SeekBar.

但是当我更改搜索栏进度时,当我将其设置为最小值(即设置为零位置)时,设备会卡住,我必须重新启动设备才能使其再次工作。

我的代码出了什么问题。请帮我

oncreate()我有以下代码,

         try {
            BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);

            brightnessscrollbar.setProgress(BackLightValue);

            System.out.println("Baclightvalue is "+BackLightValue);


        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

搜索栏点击监听器

  brightnessscrollbar.setOnSeekBarChangeListener(

          new OnSeekBarChangeListener() {

  @Override

  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
      // TODO Auto-generated method stub

     BackLightValue = (int) ((float)arg1/100);         
      WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
      layoutParams.screenBrightness = BackLightValue;
      getWindow().setAttributes(layoutParams);
  }
  @Override
  public void onStartTrackingTouch(SeekBar arg0) {
  }
  @Override
  public void onStopTrackingTouch(SeekBar arg0) {

      int SysBackLightValue = (int)(BackLightValue * 255);
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS,
                SysBackLightValue);
      }
});
4

2 回答 2

4

我开始知道 Device 卡在零值

所以我给了 onProgresschanged 一个条件如下

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
  // TODO Auto-generated method stub

 BackLightValue = (int) ((float)arg1/100);       

  if(arg1>5){  
  WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  layoutParams.screenBrightness = BackLightValue;
  getWindow().setAttributes(layoutParams);

  }
}

它现在起作用了,就像一个魅力。

于 2014-01-31T06:08:10.517 回答
0
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            progress = progressValue;
            if(progress <1 ) {

                progress = 1;

            }
            android.provider.Settings.System.putInt(getActivity().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

            android.provider.Settings.System.putInt(getActivity().getContentResolver(),
                    android.provider.Settings.System.SCREEN_BRIGHTNESS,
                    progress);


            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.screenBrightness = progress / (float)255;
            window.setAttributes(layoutParams);
        }
于 2014-10-17T03:27:04.283 回答