changing screen brightness programmatically in android

*爱你&永不变心* 提交于 2019-12-17 05:03:33

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/3737579/changing-screen-brightness-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!