2012-08-17 45 views
2

我想创建一个应用程序,它将以编程方式设置android设备的亮度。我已经管理使用下面的代码更改设备的亮度:如何检索当前系统Android的屏幕亮度值?

BackLightValue = (float)arg1/100; 
BackLightSetting.setText(String.valueOf(BackLightValue)); 

WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
layoutParams.screenBrightness = BackLightValue; 
getWindow().setAttributes(layoutParams); 

我使用下面的代码检索当前的系统亮度值:

try { 
    BackLightValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); 
} catch(Exception e) {} 

上面的代码应该给我电流设定值亮度,但是当我启动一个应用程序,它显示默认值为50%。

什么问题?

回答

4

这是正确的行为,因为将screenBrightness设置为-1会将亮度设置为当前系统亮度级别。

int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS,-1); 

Manifest文件中的权限。

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

感谢您的回复。 – 2012-08-17 07:17:13

+2

那么,你不需要调用'getInt()'的权限。只有当你想调用'putInt()'时才需要它 – 2013-12-16 10:15:19