1

I'm trying to make a slider which will change the screen brightness. I've managed to do it with the above code

Brightness code:

BackLightControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            float BackLightValue = (float)progress/100.0f;
            int brightnessMode;
            try {
                brightnessMode = Settings.System.getInt(getContentResolver(), 
                        Settings.System.SCREEN_BRIGHTNESS_MODE);

            if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 
                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            }
              BackLightSetting.setText(String.valueOf(BackLightValue));
              WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
              layoutParams.screenBrightness = BackLightValue;
              getWindow().setAttributes(layoutParams);
            } catch (SettingNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }           

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }
    });


}

}

The problem is that it' s working and I can see the brightness changing while slide the slider, but it returns to previous brightness level as soon as the activity finishes, which is pointless. The autobacklight setting, which disabled when use the slider, remain though. What I'm doing wrong? Also, how can I prevent the slider to be 0, because the screen goes off, and only by force close the app I can reset the change.

dancer_69
  • 331
  • 2
  • 6
  • 20

3 Answers3

3

To prevent the slider to be 0, just add 1 to your statement if the result is 0:

float BackLightValue = (float)progress/100.0f;
if(BackLightValue == 0){
   BackLightValue++
}

To save the brightness, use the following code and try to delay the finish statement in your activity by a few hundred milliseconds. For some reason this usually solves the problem.

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, brightness);

Just add this line where you want the brightness to be changed. The third parameter "brightness" should be replaced by the value from your seekbar. In your case BackLightValue. Remember that the brightness value ranges from 1 to 255.


You might need to refresh the screen by this hack:

  • Start an activity and finish it immediately

Start this activity after you've set the brightness:

context.startActivity(new Intent(context, Bright.class));

The dummy activity could be:

public class Bright extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

finish();
  }
}
barto90
  • 679
  • 1
  • 9
  • 27
  • 1
    Thanks, 0 problem solved. That was the easy one I guess. For saving brightness I don't really understand where to put this(i have very little experience on android programming). – dancer_69 Sep 30 '12 at 15:38
  • I cannot make it work. I suppose that the System.putInt.... save the value of slider position. But it – dancer_69 Sep 30 '12 at 20:55
1

To prevent the slider to have the value 0, you can use a version similar to this: Prevent SeekBar from moving past a dynamic point, but modify it to use static values for min, something like:

//We want a minimum om 20 percent screen brightness
private static int MY_MIN_VALUE = 20;

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (progress > MY_MIN_VALUE){
        seekBar.setProgress(progress);
        saveScreenBrightness(progreess);
    }

    else
        seekBar.setProgress(MY_MIN_VALUE);     
}

where the method saveScreenBrightness() is the method where you update the brightness.

To save the brightness to the System, try using the following code:

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, brightness);

I have not tested this method, but it should be working. You could also try reading in this post: Changing screen brightness programmatically (as with the power widget)

Community
  • 1
  • 1
Heskja
  • 787
  • 6
  • 19
  • Thanks for your quick and valuable answer too. I have read the thread from your link, and some other related I've found, but I didn't manage to solve it. For this I've started this thread. – dancer_69 Sep 30 '12 at 15:41
0

To prevent brightness to have the 0 value (you want brightness 1-255): seekBar.setMax(254); //0-254

//then instead od using progress (0-254), use allways ++progress (1-255)

//and when setting the seekBar progress use --brightness

PEPAN
  • 141
  • 1
  • 2