studio programacion móviles funciona desarrollo curso celular brillo aplicaciones android brightness screen-brightness

android - programacion - Cambiar el brillo del sistema mediante programación



manual de programacion android pdf (9)

Quiero cambiar el brillo del sistema programáticamente. para ese propósito estoy usando este código:

WindowManager.LayoutParams lp = window.getAttributes(); lp.screenBrightness = (255); window.setAttributes(lp);

porque escuché que el valor máximo es 255.

pero no hace nada. Sugiero cualquier cosa que pueda cambiar el brillo. Gracias


Este es el código completo sobre cómo cambiar el brillo del sistema

private SeekBar brightbar; //Variable to store brightness value private int brightness; //Content resolver used as a handle to the system''s settings private ContentResolver Conresolver; //Window object, that will store a reference to the current window private Window window; /** Called when the activity is first created. */ @Override public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Instantiate seekbar object brightbar = (SeekBar) findViewById(R.id.ChangeBright); //Get the content resolver Conresolver = getContentResolver(); //Get the current window window = getWindow(); brightbar.setMax(255); brightbar.setKeyProgressIncrement(1); try { brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { Log.e("Error", "Cannot access system brightness"); e.printStackTrace(); } brightbar.setProgress(brightness); brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness); LayoutParams layoutpars = window.getAttributes(); layoutpars.screenBrightness = brightness / (float) 255; window.setAttributes(layoutpars); } public void onStartTrackingTouch(SeekBar seekBar) { } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (progress <= 20) { brightness = 20; } else { brightness = progress; } } }); }

O puede consultar este tutorial para obtener el código completo
feliz codificación :)


Por favor, intente esto, es posible que lo ayude. Funcionó bien para mí

De acuerdo con mi experiencia

1st method. WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 75 / 100.0f; getWindow().setAttributes(lp);

donde el valor de brillo muy de acuerdo con 1.0f.100f es el brillo máximo.

El código mencionado anteriormente aumentará el brillo de la ventana actual. Si queremos aumentar el brillo de todo el dispositivo Android, este código no es suficiente, para eso tenemos que usar

2nd method. android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 192);

Donde 192 es el valor de brillo que muy de 1 a 255. El principal problema de utilizar el segundo método es que mostrará el brillo en forma creciente en el dispositivo Android, pero en realidad no podrá aumentar el brillo del dispositivo Android. Esto es porque necesita un poco de refrescante .

Es por eso que descubro la solución usando ambos códigos juntos.

if(arg2==1) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = 75 / 100.0f; getWindow().setAttributes(lp); android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 192); }

Funcionó bien para mí


Probé varias soluciones que otros publicaron y ninguna funcionó exactamente bien. La respuesta de geet es básicamente correcta, pero tiene algunos errores sintácticos. Creé y usé la siguiente función en mi aplicación y funcionó muy bien. Tenga en cuenta que esto específicamente cambia el brillo del sistema como se le preguntó en la pregunta original.

public void setBrightness(int brightness){ //constrain the value of brightness if(brightness < 0) brightness = 0; else if(brightness > 255) brightness = 255; ContentResolver cResolver = this.getApplicationContext().getContentResolver(); Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness); }


Puedes usar lo siguiente:

//Variable to store brightness value private int brightness; //Content resolver used as a handle to the system''s settings private ContentResolver cResolver; //Window object, that will store a reference to the current window private Window window;

En su escritura onCreate:

//Get the content resolver cResolver = getContentResolver(); //Get the current window window = getWindow(); try { // To handle the auto Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); //Get the current system brightness brightness = System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS); } catch (SettingNotFoundException e) { //Throw an error case it couldn''t be retrieved Log.e("Error", "Cannot access system brightness"); e.printStackTrace(); }

Escribe el código para monitorear el cambio en el brillo.

luego puede configurar el brillo actualizado de la siguiente manera:

//Set the system brightness using the brightness variable value System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness); //Get the current window attributes LayoutParams layoutpars = window.getAttributes(); //Set the brightness of this window layoutpars.screenBrightness = brightness / (float)255; //Apply attribute changes to this window window.setAttributes(layoutpars);

Permiso en manifiesto:

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

Para API> 23, debe solicitar el permiso a través de la Actividad de configuración, que se describe aquí: No se puede obtener el permiso WRITE_SETTINGS


SeekBar Brighness privada = null;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lcd_screen_setting); initUI(); setBrightness(); } private void setBrightness() { Brighness.setMax(255); float curBrightnessValue = 0; try { curBrightnessValue = android.provider.Settings.System.getInt( getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } int screen_brightness = (int) curBrightnessValue; Brighness.setProgress(screen_brightness); Brighness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { int progress = 0; @Override public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) { progress = progresValue; } @Override public void onStartTrackingTouch(SeekBar seekBar) { // Do something here, // if you want to do anything at the start of // touching the seekbar } @Override public void onStopTrackingTouch(SeekBar seekBar) { android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress); } }); } initUI(){ Brighness = (SeekBar) findViewById(R.id.brightnessbar); }


Yo tuve el mismo problema.

Dos soluciones:

aquí, brillo = (int) 0 to 100 range ya que estoy usando la barra de progreso

1 SOLUCIÓN

float brightness = brightness / (float)255; WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = brightness; getWindow().setAttributes(lp);

2 SOLUCIÓN

Acabo de utilizar la actividad ficticia para llamar cuando mi barra de progreso stop buscar.

Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class); Log.d("brightend", String.valueOf(brightness / (float)255)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important //in the next line ''brightness'' should be a float number between 0.0 and 1.0 intent.putExtra("brightness value", brightness / (float)255); getApplication().startActivity(intent);

Ahora viene a DummyBrightnessActivity.class

public class DummyBrightnessActivity extends Activity{ private static final int DELAYED_MESSAGE = 1; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == DELAYED_MESSAGE) { DummyBrightnessActivity.this.finish(); } super.handleMessage(msg); } }; Intent brightnessIntent = this.getIntent(); float brightness = brightnessIntent.getFloatExtra("brightness value", 0); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = brightness; getWindow().setAttributes(lp); Message message = handler.obtainMessage(DELAYED_MESSAGE); //this next line is very important, you need to finish your activity with slight delay handler.sendMessageDelayed(message,200); } }

no te olvides de registrar DummyBrightnessActivity para manifestar.

¡¡Espero eso ayude!!


esto funcionó para mí hasta kitkat 4.4 pero no en Android L

private void stopBrightness() { Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); }


Link de referencia

WindowManager.LayoutParams layout = getWindow().getAttributes(); layout.screenBrightness = 1F; getWindow().setAttributes(layout);


WindowManager.LayoutParams params = getWindow().getAttributes(); params.screenBrightness = 10; // range from 0 - 255 as per docs getWindow().setAttributes(params); getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);

Esto funcionó para mí. No hay necesidad de una actividad ficticia. Esto funciona solo para tu actividad actual.