屏幕亮度搜索栏无法正常工作

时间:2014-01-30 05:25:10

标签: android settings seekbar brightness screen-brightness

在我的应用中,我添加了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();
        }

seekbarclicklistener

  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);
      }
});

2 个答案:

答案 0 :(得分:4)

我开始知道设备卡在零值

所以我给onProgresschanged一个条件作为folows

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);

  }
}

它现在有效,就像魅力一样。

答案 1 :(得分: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);
        }