如何才能持久更改 Android 屏幕亮度?

发布于 2024-11-11 07:02:12 字数 437 浏览 5 评论 0原文

我已经设法使用以下代码更改活动(以及扩展整个应用程序)中的屏幕亮度:

Settings.System.putFloat(this.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, ((float)LocationService.settings.screenBrightness));

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

但是,一旦我关闭应用程序,亮度就会恢复到之前的设置。是否有办法使这些更改在应用程序的生命周期之外持续存在?

谢谢!

I've managed to change screen brightness within an activity (and by extension the entire app) using the following code:

Settings.System.putFloat(this.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, ((float)LocationService.settings.screenBrightness));

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

However, as soon as I close the app the brightness returns to its previous settings. Is there anyway to make these changes persist outside of the lifecycle of the app?

Thanks!

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

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

发布评论

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

评论(4

命硬 2024-11-18 07:02:12

这里也许对你有帮助:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);

Here maybe help you:

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();  
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);
丘比特射中我 2024-11-18 07:02:12

尝试一下

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

int brightnessLevel = 255;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessLevel);

您将需要在 AndroidManifest.xml

http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html

Try this

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

int brightnessLevel = 255;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessLevel);

You will need the WRITE_SETTINGS permission set in your AndroidManifest.xml

http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html

只怪假的太真实 2024-11-18 07:02:12

设置应用程序这样做:

private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;

System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value + MINIMUM_BACKLIGHT);

但我怀疑如果没有系统共享用户 ID,您就无法更改系统设置

Settings application does it this way:

private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;

System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value + MINIMUM_BACKLIGHT);

but I suspect you cannot change a System Settings without having system shared user id

情泪▽动烟 2024-11-18 07:02:12

在 androidMainfest.xml 中提供权限

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

并提供 0 到 255 之间的整数

//number>=0 && number<=255
//If you have passed context then you can use context.getContentResolver()
android.provider.Settings.System.putInt(cgetContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS,
            number);

Provide the permission in androidMainfest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

And provide a integer number between 0 to 255

//number>=0 && number<=255
//If you have passed context then you can use context.getContentResolver()
android.provider.Settings.System.putInt(cgetContentResolver(),
            android.provider.Settings.System.SCREEN_BRIGHTNESS,
            number);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文