我有和this一样的需求,但我发现该问题下的答案无效。
我想制作按钮来在我的应用程序运行时更改我的屏幕亮度。
我找到了这段代码,但是如果我直接将这段代码复制到我的 mainActivity 中,它不起作用。
WindowManager.LayoutParams lp = getWindow().getAttributes();
float brightness=1.0f;
lp.screenBrightness = brightness;
getWindow().setAttributes(lp);
我使用 android studio 并且 API 级别是 21 我添加了用户权限。
这一段代码最接近我的目标,谁能帮我运行这段代码?
最佳答案
这对我有用:
在onCreate()
方法中这样写,
private int brightness;
try{
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
}
catch(SettingNotFoundException e){
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
要更新亮度,
Settings.System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS, brightness);
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
getWindow().setAttributes(layoutpars);
list 中的权限,
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
关于android - 通过安卓应用改变屏幕亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779724/