在android中以编程方式改变屏幕亮度

发布于 2024-09-24 12:57:16 字数 292 浏览 6 评论 0原文

我想在 android 中以编程方式更改屏幕亮度。 目前我使用这个代码:

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

但是这个示例代码适用于纸杯蛋糕,而不适用于最新版本。我正在使用最新版本的 SDK。较新 Android 版本的首选解决方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七禾 2024-10-01 12:57:16

这可以通过使用来完成:

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

另请参阅:
http://developer.android.com/reference/android/view /WindowManager.LayoutParams.html#screenBrightness

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

枯叶蝶 2024-10-01 12:57:16

在创建Window之前,您必须向它添加参数,否则会抛出java.lang.IllegalArgumentException:添加窗口后无法更改窗口类型。

请参阅示例使用 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();

请注意,亮度值介于 0.0F 和 1.0F 之间。

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.

红尘作伴 2024-10-01 12:57:16

为此使用 IHardwareService 接口怎么样?可以在此 教程

更新:教程链接仍然有效,但实际代码也可以在下一个答案中找到。

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.

此刻的回忆 2024-10-01 12:57:16

答案太晚了,但想要改进..

我尝试使用 Tor-morten 的代码,但它是针对特定屏幕本身的,我想在任何地方进行更改,我为此提供了服务。

在android中根据周围的光线改变亮度

希望,它会对别人有用。

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.

漆黑的白昼 2024-10-01 12:57:16
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文