'how can I detect change in brightness in android?

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source