سؤال

In my app I added a SeekBar which controls the screen brightness. It works some what fine . I get the device's screen brightness in Oncreate() and set the progress of the SeekBar.

But when I change the seekbar progress , when I set it to the minimum(ie. to the zero position) the device gets stuck and I had to restart my device to get it working again.

What is wring with my code. Please help me

in oncreate() I have the following code,

         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);
      }
});
هل كانت مفيدة؟

المحلول

I came to know that Device gets stuck at the value zero

So I gave a condition on onProgresschanged as 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);

  }
}

And it works now, like a charm.

نصائح أخرى

        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);
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top