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;
}