gpt4 book ai didi

android - 通过安卓应用改变屏幕亮度

转载 作者:行者123 更新时间:2023-11-30 02:19:47 26 4
gpt4 key购买 nike

我有和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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com