2012-08-17 91 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