6

I want to get the real brightness value from the background. I have tried several ways:

1.

    curBrightnessValue =android.provider.Settings.System.getInt(
                getContext().getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS);

But if the screen brightness is on Auto mode the value remain constant.

  1. Reading the sys/class/backlight/brightness/

    This is a good way but I want a way without reading a file.

Community
  • 1
  • 1
Simon Dzn
  • 150
  • 1
  • 1
  • 8

3 Answers3

4

Use the following code to get the brightness of the background (This will also allow you to change the value of brightness if you wish to):

Settings.System.putInt(
    cResolver,
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness =
    Settings.System.getInt(
        cResolver,
        Settings.System.SCREEN_BRIGHTNESS);  
System.out.println("Current Brightness level " + brightness);
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
2

To my knowledge, it cannot be done any other way in Auto mode. See this answer.

Community
  • 1
  • 1
solarbabies
  • 302
  • 1
  • 12
0

Method 1 as described using Settings.System.getInt() in auto mode doesn't work for older Android versions like 'N'. But it works for 'P' and it's the same value as that in the file /sys/class/backlight/panel0-backlight/brightness. Some sample code I tried in Processing

import android.provider.Settings;  // for global system settings
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

Activity act;
Context context;

void setup() {
    act = this.getActivity();
    context = act.getApplicationContext();
}

void draw() {
    text("brightness = " + getBrightness());
}

float getBrightness() {
    float brightness;
    if(!Settings.System.canWrite(context)) {
        // Enable write permission
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        context.startActivity(intent);
    } else {
        // Get system brightness
        Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); // enable auto brightness
        brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, -1);  // in the range [0, 255]
    }
    return brightness;
}
xiaoyund
  • 1
  • 2