Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 620
Bennett Von Bennett Avatar asked Feb 18 '12 07:02

Bennett Von Bennett


People also ask

How do I stop my brightness from automatically raising?

Navigate to Settings, and then tap Display. Tap the switch next to Adaptive brightness to turn the setting off.

Does turning off auto brightness affect battery life?

If your phone is set to automatically adjust brightness levels, then the screen has probably become really bright, which drains battery life. The Purdue study found that switching from light mode to dark mode at 100% brightness saves an average of 39%-47% battery power.

Is it better to keep auto brightness on or off?

It is recommended to switch your phone from its default brightness setting so you can control how bright you want your phone to be. This is mainly for two reasons. The first is battery life. Bright light helps you see your screen better, especially if you are outside, but it will quickly kill your battery.


1 Answers

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;
    }
}
like image 154
Sergey Glotov Avatar answered Sep 23 '22 19:09

Sergey Glotov