简体   繁体   中英

Changing programmatically brightness while using auto mode has no effect in Android

I've written code to create DialogPreference that should be working similar to default Android brightness setting dialog.

It works perfect as long as brightness mode is set to Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL . But when I change mode to Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC the phone brightness does not take the value of Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) into account.

Let me show you an example:

  1. I set brightness in Android settings to 255 and mode to automatic. The changes are reflected by the phone (as well as my app).
  2. I set brightness in Android settings to 100 and mode to manual. The changes are reflected by the phone (as well as my app).
  3. I set brightness in my app to 50 and mode is still manual. The changes are reflected by the phone (as well as my app).
  4. I set brightness in my app to 50 and mode to automatic. Although I programmatically change current screen brightness to 50, the rest of my app and the phone itself (for example phone settings and home) has brightness 255 and mode to automatic (which I can check in Android settings). At the same time, Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS) is still returning 50.

Here is my code. I as said, it's in a class that extends DialogPreference, thus the onCreateDialogView method.

private static int MAX_BRIGHTNESS_VALUE = 255;

@Override
protected View onCreateDialogView() {
    View dialogView = super.onCreateDialogView();
    mAutomatic = (CheckBox) dialogView.findViewById(R.id.automatic_brightness);
    mBrightnessBar = (SeekBar) dialogView.findViewById(R.id.brightness_bar);

    mBrightnessBar.setMax(MAX_BRIGHTNESS_VALUE);
    mAutomatic.setChecked(getBrightnessMode() == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    mBrightnessBar.setProgress(getBrightness());

    mAutomatic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                setAutomaticBrightness();
            } else {
                setManualBrightness();
            }
        }

    });
    mBrightnessBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (progress > 10) {
                setBrightness(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

    });
    return dialogView;
}

private void setBrightness(int value) {
    Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value);

    // here I set current screen brightness programmatically
    Window window = ((Activity) getContext()).getWindow();
    LayoutParams layoutpars = window.getAttributes();
    layoutpars.screenBrightness = value / (float) MAX_BRIGHTNESS_VALUE;
    window.setAttributes(layoutpars);
}

private int getBrightness() {
    try {
        return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
    } catch (SettingNotFoundException e) {
        e.printStackTrace();
        return MAX_BRIGHTNESS_VALUE;
    }
}

private void setAutomaticBrightness() {
    Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}

private void setManualBrightness() {
    Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}

private int getBrightnessMode() {
    try {
        return Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
    } catch (SettingNotFoundException e) {
        e.printStackTrace();
        return Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    }
}

I have <uses-permission android:name="android.permission.WRITE_SETTINGS" /> in my AndroidManifest.xml

Does anyone know what is going on? I have seen some Stack Overflow threads with similar problems, but none of them got any helpful answer.

If you set brightness mode to automatic. then you need to stop automatic brightness adjustment before sets brightness. this code worked for me:

      public static void stopAutoBrightness(Activity activity) {
          android.provider.Settings.System.putInt(activity.getContentResolver(),
                  android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
                  android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }

It seems that it just works that way. If you set brightness mode to automatic by yourself, you cannot manipulate brightness level.

An example is here: BrightnessPreference code on grepcode.com for 4.1.2 In this Preference brightness SeekBar is disabled when brightness mode is automatic.

Based on BrightnessPreference code on grepcode.com for 4.4.2 I came up with the following inflexible solution:

@Override
protected void onClick() {
    Intent intent = new Intent("com.android.settings.BrightnessPreferenceActivity");
    startActivity(intent);
}

It just starts the Activity used in Android settings. It was good enough in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM