How do you detect if user has auto-brightness enabled?

Viewed 458

It should be an easily searchable question yet I can't find a single result. I can find how to retrieve screen brightness but not how to retrieve the status of auto brightness.

And a second question, Android Studio forces me to surround this statement with a try/catch block, which I guess means it won't work all the time. Is there a more reliable way to retrieve screen brightness?

int currentBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);

When I hover the mouse over getInt I get this message: Unhandled exception: android.provider.Settings.SettingNotFoundException

I have to turn it into this:

try {

     int currentBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);

} catch (Settings.SettingNotFoundException e) {

                e.printStackTrace();
}
3 Answers

how to retrieve the status of auto brightness

If you Ctrl+click SCREEN_BRIGHTNESS in Android Studio you can see the source code. Right below where SCREEN_BRIGHTNESS is there's also SCREEN_BRIGHTNESS_MODE and constants for the two modes:

/** Control whether to enable automatic brightness mode. */
public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode"; 

/** SCREEN_BRIGHTNESS_MODE value for manual mode. */
public static final int SCREEN_BRIGHTNESS_MODE_MANUAL = 0;

/** SCREEN_BRIGHTNESS_MODE value for automatic mode. */
public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;

See also Settings.java in Android Code Search.

Android Studio forces me to surround this statement with a try/catch block, which I guess means it won't work all the time.

Android Studio doesn't force you to do anything, it's trying to tell you something, probably, that you should do something differently. At the minimum, log the exception so you can share it in your SO question, so people can help you figure out what's going on.

Update your question with the exact exception stack trace and more code.

android.provider.Settings.SettingNotFoundException

SettingNotFoundException happens in getInt when

  • the setting is unset
  • the setting set but is not a number

If you don't want to deal with it use the overload that takes 3 parameters and doesn't throw:

ContentResolver contentResolver = getContext().getContentResolver();
int currentBrightness = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, /* default value */ 0);

Per documentation:

The default value will be returned if the setting is not defined or not an integer.

If you want to know if the user has the auto brightness mode enabled in the device, you can try a function like that:

    int getBrightnessMode() {
    try {
        int brightnessmode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
        return brightnessmode;
    } catch (Exception e) {
        return 0;
    }
}

This function will return a number, 1 for auto brightness enabled and 0 for auto brightness disabled.

If you want to change the brightness manually, your app needs a special permission, because the screen brightness is a device parameter.

To give permission to the app and change the brightness, use this code:

    if (!Settings.System.canWrite(getApplicationContext())) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        startActivity(intent);
    }

if (Settings.System.canWrite(getApplicationContext())) {
Settings.System.putInt(getApplicationContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, myBrightness);
}

You will need to add the Write Settings permission to the AndroidManifest file:

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

Tested on Nokia 6.2 & Samsung Tab Running Android 10 and Android 5.1

private int getMobileBrightnessMode() {
        // works for adaptive brightness option as well as auto for old models
        // adaptive brighness Android Pie onwards
        // Auto Brightness for old versions
        ContentResolver contentResolver = getContentResolver();
        int brightnessMode = 0;
        try {
             brightnessMode = Settings.System.getInt(contentResolver,Settings.System.SCREEN_BRIGHTNESS_MODE);

        } catch (Exception e) {
            Log.d("tag", e.toString());
        }
        return brightnessMode;
        // if 0 auto mode is off
        // if 1 auto mode is on
    }
Related