How to control the lighting (brightness) of Android via code for all activities?

Asked

Viewed 252 times

0

I’m using the following code:

    //brilho
    try {
            //sets manual mode and brightnes 255
            Settings.System.putInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)
            Settings.System.putInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 85);  //maximo 255 -> diminui 1/3
            int br = Settings.System.getInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            WindowManager.LayoutParams lp = act.getWindow().getAttributes();
            lp.screenBrightness = (float) br / 85;
            act.getWindow().setAttributes(lp);


    } catch (Exception e) {
        e.getMessage();
    }

However, I can only change the brightness of the Activity I call the method. I needed to change the whole tablet. Is there any way to do this without user intervention? Thanks in advance.

2 answers

3


You can create an "Activitybase":

public abstract class ActivityBase extends Activity {
@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //brilho
    try {
        //sets manual mode and brightnes 255
        Settings.System.putInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  //this will set the manual mode (set the automatic mode off)
        Settings.System.putInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 85);  //maximo 255 -> diminui 1/3
        int br = Settings.System.getInt(act.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        WindowManager.LayoutParams lp = act.getWindow().getAttributes();
        lp.screenBrightness = (float) br / 85;
        act.getWindow().setAttributes(lp);


    } catch (Exception e) {
        e.getMessage();
    }
}
}

And "Extend" your Activities using this as a basis:

public class ActivityMain extends ActivityBase {

I hope I can help you!

1

First you need permission

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

Arrow the variables

private int brightness;
private ContentResolver cResolver;
private Window window;

And inside your onCreate

cResolver = getContentResolver();

window = getWindow();

try
   {
        Settings.System.putInt(cResolver,
        Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

        brightness = System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);

        System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);

        LayoutParams layoutpars = window.getAttributes();

        layoutpars.screenBrightness = brightness / (float)85;

        window.setAttributes(layoutpars);

   } 
catch (SettingNotFoundException e) 
   {   
      Log.e("Error", "Cannot access system brightness");
      e.printStackTrace();
   }

Follow the link from where this solution was found, along with other answers and possible ways to solve:

https://stackoverflow.com/questions/18312609/change-the-system-brightness-programmatically

Browser other questions tagged

You are not signed in. Login or sign up in order to post.