문제

I want to change the screen brightness programmatically in android. At the moment I use this code:

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

But this sample code works on cupcake, not on latest versions. I am using the latest version of SDK.. What is the preferred solution for newer Android Versions?

도움이 되었습니까?

해결책

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.

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top