[英]How can I make changes to android screen brightness persistent?
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!谢谢!
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您需要在 AndroidManifest.xml 中设置 WRITE_SETTINGS 权限
http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html http://android-er.blogspot.com/2011/02/change-system-screen-brightness-using.html
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但我怀疑如果没有系统共享用户 ID,您将无法更改系统设置
Provide the permission in androidMainfest.xml在 androidMainfest.xml 中提供权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
And provide a integer number between 0 to 255并提供一个介于 0 到 255 之间的 integer 数
//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);
Here maybe help you:这里也许可以帮助你:
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.buttonBrightness = value;
getWindow().setAttributes(layoutParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.