dancer_69
dancer_69

Reputation: 331

How to keep the brightness setting after the activity finish?

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.

Upvotes: 1

Views: 5985

Answers (3)

PEPAN
PEPAN

Reputation: 141

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

Upvotes: 0

barto90
barto90

Reputation: 689

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

Upvotes: 3

Heskja
Heskja

Reputation: 785

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)

Upvotes: 1

Related Questions