0

Estou usando o seguinte códgio:

    //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();
    }

Porém, só consigo alterar o brilho da activity que chamo o método. Eu precisava mudar de todo o tablet. Existe algum modo de fazer isto sem intervenção do usuário? Agradeço desde já.

2 Respostas 2

3

Você pode criar uma "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();
    }
}
}

E "Extender" suas Activities utilizando esta como base:

public class ActivityMain extends ActivityBase {

Espero poder ter te ajudado!

1

Primeiramente você precisa da permissão

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

Seta as variáveis

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

E dentro do seu 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();
   }

Segue o link de onde foi encontrada essa solução, juntamente de outras respostas e possíveis formas de solucionar:

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .