尝试在 Android 手机上设置屏幕亮度

发布于 2024-12-06 15:24:37 字数 371 浏览 0 评论 0原文

我正在尝试设置屏幕亮度,但是当我尝试使用 this.getWindow() 获取当前窗口时,我得到 null。这是为什么呢?我将在 setBrightness() 方法中发布所有代码。

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
            brightness);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / (float) 255;
window.setAttributes(lp);

I am trying to set the screen brightness but when I try and get the current window with this.getWindow() I get null. Why is this? I will post all of my code in my setBrightness() method.

System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
            brightness);
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness / (float) 255;
window.setAttributes(lp);

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

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

发布评论

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

评论(3

江城子 2024-12-13 15:24:37

不要使用 System.putInt()...您已经在 lp 中设置了 screenBrightness!

以下代码有效:

Window window = getWindow();
float brightness = 100.0f;
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness/255.0f;
window.setAttributes(lp);

Don't use System.putInt()... You already set the screenBrightness in the lp!

The following code works:

Window window = getWindow();
float brightness = 100.0f;
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = brightness/255.0f;
window.setAttributes(lp);
季末如歌 2024-12-13 15:24:37

如果您使用搜索栏,请尝试这些行,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);

If you are using a seekbar, try these lines,

System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
window.setAttributes(layoutpars);
寄风 2024-12-13 15:24:37

如果你从片段中调用它,请像这样在 getWindow() 之前添加 getActivity()

getActivity().getWindow().setAttributes(layoutParams);

如果使用eekbar,不要让你的 brogress 为 0,因为它会让你的屏幕完全变黑(在 android 2.3 上)

If you call it from fragment add getActivity() before getWindow() like this

getActivity().getWindow().setAttributes(layoutParams);

And if use seekbar dont let your brogress be 0, cause it can make your screen goes completly dark(on android 2.3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文