I am trying to develop a simple app that can monitor the value of current screen brightness and update the value when the brightness is changed by a user.
Here is my code to get the current brightness.
try {
float curBrightnessValue=android.provider.Settings.System.getInt(
getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
System.out.println(curBrightnessValue);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
but I am struggling with updating the value. How can I update the value when there is a change in brightness?
This can help you:
ContentObserver contentObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
int a = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0);
Log.d("MainActivity", "Brightness value: "+ a);
}
};
getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
false, contentObserver);
Callback is attached to receive the changes when content are changed