How to toggle auto brightness on and off? (not a r

2019-02-14 07:33发布

问题:

I'm simply trying to toggle auto brightness on and off. I started with this code (inside the onCreate method)

final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.brightToggle);

    // display auto brightness state
    final ToggleButton autoBrightToggle = (ToggleButton) findViewById(R.id.autoToggle);
    autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));

    autoBrightToggle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (autoBrightToggle.isChecked()) {
                setAutoBright(true);
            } else {
                setAutoBright(false);
            }
        }
    }); // end anonymous OnClickListener function

    // toggle the brightness mode
    private void  setAutoBright(boolean mode) {
      if (mode) {
        Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
      } else {
        Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
        autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
      }
    }

Which doesn't seem to work. The setAutoBrightnessMode() method is also called again in onResume() but with the same non-results. Anyway, I'm sorry if someone feels this question is redundant but the other posts did not get me where I need to go!

(FWIW - I'm testing this on my old Droid X and my Galaxy Nexus, not the Emulator)

EDITED - UPDATE ON THIS: I'm 99% sure now that I am not seeing any changes to the Auto-Brightness mode reflected in the Settings panel and desktop widgets - even though I may actually be changing it's value. part of the problem is that I don't know how exactly to determine if Auto-Brightness is on or not!

For instance, does the screen quickly and visibly change? I've been expecting immediate visible changes in brightness according to environment - but perhaps the changes are subtle? and over a longer period? or perhaps it takes 30 seconds or more of environment change before brightness changes?

Can someone suggest how I can track this? I've tried querying the Settings.System.SCREEN_BRIGHTNESS_MODE constant - hooking this method up to a textfield:

   private int getAutoBrightnessMode() {
     try {
        int brightnessMode = Settings.System.getInt(cr, SCREEN_BRIGHTNESS_MODE);
     } catch (Settings.SettingNotFoundException e) {
        e.printStackTrace();
        int brightnessMode = -10000;
     }
    return brightnessMode;
   }

But it always reads 0, even after an onResume(). :-((

I know this is a simple procedure, but I'm trying to learn this stuff on my own, and have had almost no formal CS training... So all I can say is I'm very frustrated by this and feel like I've worked myself into a corner and at this point I'm so annoyed I can't think straight anymore.

So help would be great.

回答1:

I use following approach in my application. Tested on HTC Desire HD and pair of noname chinese tablets.
Add to manifest permission:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

And use below code to toggle auto brightness. There is one trick in the code: we need to "refresh" brightness of app manually, because it doesn't changes automatically. May be it is the problem in your case.

void setAutoBrightness(boolean value) {
    if (value) {
        Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    } else {
        Settings.System.putInt(getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    // After brightness change we need to "refresh" current app brightness
    if (isChecked) {
        refreshBrightness(-1);
    } else {
        refreshBrightness(getBrightnessLevel());
    }
}

private void refreshBrightness(float brightness) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    if (brightness < 0) {
        lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    } else {
        lp.screenBrightness = brightness;
    }
    getWindow().setAttributes(lp);
}

int getBrightnessLevel() {
    try {
        int value = Settings.System.getInt(getContentResolver(), SCREEN_BRIGHTNESS);
        // convert brightness level to range 0..1
        value = value / 255;
        return value;
    } catch (SettingNotFoundException e) {
        return 0;
    }
}