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

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 that I call the method. I needed to change the whole tablet. Is there any way to do this without user intervention? Thank you in advance.

    
asked by anonymous 10.11.2016 / 17:51

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 could have helped you!

    
10.11.2016 / 18:03
1

First you need the permission

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

Arrow the variables

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

And within 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 where this solution was found, along with other answers and possible ways to solve:

link

    
10.11.2016 / 17:57