Pregunta

Quiero cambiar el brillo de la pantalla mediante programación en Android. En el momento en que utilizo este código:

WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);

Sin embargo, este código de ejemplo funciona en magdalena, no en las versiones más recientes. Estoy usando la última versión del SDK .. ¿Cuál es la solución preferida para las nuevas versiones de Android?

¿Fue útil?

Solución

How about using the IHardwareService interface for this? An example can be found in this tutorial.

Update: tutorial link still works, but actual code is also available in next answer.

Otros consejos

This is possible to do by using:

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

See also: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness

You have to add params to Window before it is created otherwise it will throw java.lang.IllegalArgumentException: Window type can not be changed after the window is added.

See the example with a android.app.Dialog.Dialog.

final Dialog dialog = new Dialog(this) {
            @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                WindowManager.LayoutParams layout = getWindow()
                        .getAttributes();
                layout.screenBrightness = 1F;
                getWindow().setAttributes(layout);
            }
        };
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.show();

Please note that brightness value is between 0.0F and 1.0F.

@kamal_tech_view: You must convert value layout.screenBrightness = value; to float

Too late answer but want to improve..

I tried with Tor-morten's code but it is for particular screen itself, I wanted to change anywhere, I made service for that.

Change brightness according to surrounding light in android

Hope, It will be useful to others.

final Dialog dialog = new Dialog(act);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog
                .setContentView(R.layout.menubase_brightness_control);
        dialog.setCanceledOnTouchOutside(true);

        SeekBar global_brightness_control = (SeekBar) dialog
                .findViewById(R.id.global_brightness_control);
        global_brightness_control.setMax(255);
        global_brightness_control.setProgress(Settings.System.getInt(
                con.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS));

        global_brightness_control
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    public void onProgressChanged(SeekBar seekBar,
                                                  int progress, boolean fromUser) {
                        Settings.System
                                .putInt(con.getContentResolver(),
                                        Settings.System.SCREEN_BRIGHTNESS, progress);
                    }
                });

        dialog.show();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top