سؤال

I there, I have a seekbar on my application to change the brightness and its working. Then problem is that, when I press the back button to navigate to another activity, the brightness goes back to default..

Here is my code to change brightness:

private void applyBrightnessListener() {
    SeekBar brightnessControl = (SeekBar) findViewById(R.id.brightnessControl);

    brightnessControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            // TODO Auto-generated method stub
            float BackLightValue = (float)arg1/100;
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.screenBrightness = BackLightValue;
            getWindow().setAttributes(layoutParams);
            android.provider.Settings.System.putInt(
                GeneralSettings.this.getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS,
                (int)BackLightValue);
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        }
    });
}

Thanks alot in advance ;)

هل كانت مفيدة؟

المحلول

Sadly you need to track that brightness, and set it for each activity. You can't set it once and have it stay.

So you would need to store the value in your preferences, or some other way, then when you start each activity, load the preference and set it in your activity.

One good way would be to subclass Activity, and put this code in the onCreate method.

                WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                layoutParams.screenBrightness = savedBackLightValue;
                getWindow().setAttributes(layoutParams);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top