admin管理员组文章数量:1516870
在 非 ui 线程 调用 setBrightness
import android.provider.Settings.System
public static void setBrightness(final float fbrightness) {
Log.i( "AdjustBright" , "in setBrightness-1 : " + fbrightness);
act.runOnUiThread(new Runnable() {
public void run() {
WindowManager.LayoutParams lp= act.getWindow().getAttributes(); // 这个值在 0 ~ 1.0 之间, 1 为全亮
if (fbrightness == 0.0 ) {
lp.screenBrightness = 0.005F ;
} else {
lp.screenBrightness = fbrightness;
}
Log.i( "AdjustBright" , "in setBrightness-2 : " + lp.screenBrightness);
act.getWindow().setAttributes(lp);
}
});
}
public static float getBrightness() {
WindowManager.LayoutParams lp= act.getWindow().getAttributes(); //-1 表示使用系统默认值
int br = 255 ;
float fb = 1.0F ;
if (lp.screenBrightness == - 1.0 ) {
try
{
//Get the current system brightness
br = System.getInt(act.getContentResolver(), System.SCREEN_BRIGHTNESS);
fb = br / 255.0F ;
}
catch (android.provider.Settings.SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e( "Error" , "Cannot access system brightness" );
e.printStackTrace();
}
} else {
Log.i( "AdjustBright" , "in getBrightness-1: " + lp.screenBrightness);
fb = lp.screenBrightness;
}
Log.i( "AdjustBright" , "in getBrightness-2: " + fb);
return fb;
}
另外,可通过改系统的亮度设置来改变亮度
android.provider.Settings.System.putInt(act.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(act.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt); // brightnessInt 值的范围0~255
更改系统亮度需要如下权限:
<
uses-permission
android:name
=
"android.permission.WRITE_SETTINGS"
></uses-permission>
参考:
版权声明:本文标题:解锁Android设备的亮度调节,看这篇就够了! 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/biancheng/1773231694a3277107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论