2011-02-17 56 views
13

我使用以下設置系統自動亮度模式和水平:無法應用系統屏幕亮度編程中的Android

android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0); 
    android.provider.Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1); 

我可以改變自動brighess和關閉,並設置不同級別的。設置似乎正確應用 - 我可以進入設置 - >顯示 - >亮度,並且無論什麼時候我設置的設置實際顯示正確。但是,實際屏幕不會改變其亮度。如果我只是點擊顯示設置中的滑塊,然後一切都得到應用。

我shoudl提到,我跑withat主要活動的應用程序,並且這些設置在廣播接收器得到應用。我確實試圖創建一個虛擬活動並測試那裏的東西,但得到了相同的結果。

回答

16

OK,在這裏找到了答案: Refreshing the display from a widget?

基本上,必須進行處理亮度變化的透明活動。有什麼理由不帖子中提到的是,你必須做的:

Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0); 
Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessLevel); 

然後做

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    lp.screenBrightness = brightness; 
    getWindow().setAttributes(lp); 

如果您在應用更改後立即調用finish(),亮度會從來沒有真正改變,因爲佈局必須在亮度設置應用之前創建。所以我最終創建了一個延遲300ms的線程,然後調用finish()。

+0

可惜這種方法不穩定可靠(Android的缺陷)。由於所有的時機,你可以找到失敗的角落案例 - 例如當鎖定(這裏我設置新的亮度)和解鎖手機。如果在這兩個事件之間存在暫停,但是如果操作彼此相鄰,則亮度變化將被忽略。 – greenoldman 2012-10-20 19:59:22

2

我做在我的應用程序的一個屏幕亮度類似的東西,而我通過窗口管理器做它和它的作品。我使用下面的代碼來獲取當前屏幕亮度(並保存供以後),並將其設置爲全:

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    previousScreenBrightness = lp.screenBrightness; 
    float brightness = 1; 
    lp.screenBrightness = brightness; 
    getWindow().setAttributes(lp); 
+2

感謝,這絕對是一種改進,但它確實無法正常工作 - 只有在這種情況下才有效虛擬活動正在運行,並且必須打開虛擬活動才能改變環境。我試圖在改變後立即完成活動,但亮度不會改變。 – user496854 2011-02-17 18:30:33

+4

此外,我只是意識到,這隻控制當前活動的亮度。一旦關閉,亮度將重置爲默認的系統設置。所以我們回到原來的問題 - 我如何才能將亮度系統設置應用到應用程序? – user496854 2011-02-20 03:20:06

+0

太棒了!這正是我所需要的。我直接設置屏幕亮度,並沒有更新。我其實需要設置窗口屬性來強制更新。乾杯! – Pellet 2018-03-09 02:18:28

1

我在應用類,我從我的所有Activity.onResume()方法調用中創建一個靜態方法。

MyApplication extends Application { 
    ... 
    public static void setBrightness(final Activity context) { 
     // get the content resolver 
     final ContentResolver cResolver = context.getContentResolver(); 
     // get the current window 
     final Window window = context.getWindow(); 

     try { 
      // get the current system brightness 
      int brightnessLevel = System.getInt(cResolver,System.SCREEN_BRIGHTNESS); 
      // get the current window attributes 
      LayoutParams layoutpars = window.getAttributes(); 
      // set the brightness of this window 
      layoutpars.screenBrightness = brightnessLevel/(float) 255; 
      // apply attribute changes to this window 
      window.setAttributes(layoutpars); 
     } catch (SettingNotFoundException e) { 
      // throw an error cuz System.SCREEN_BRIGHTNESS couldn't be retrieved 
      Log.e("Error", "Cannot access system brightness"); 
      e.printStackTrace(); 
     } 
    } 
} 

MyActivity extends Activity { 
    ... 
    public void onResume() { 
     super.onResume(); 
     Log.d(TAG, "onResume()"); 
     MyApplication.setBrightness(this); 
    } 
} 
1

如果你正在服用的最大screenBrightness = 255,那麼使用的 「user496854」 上述
給出了答案,而這樣做

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
lp.screenBrightness = brightness; getWindow().setAttributes(lp); 

鴻溝screenBrightness由255像

WindowManager.LayoutParams lp = getWindow().getAttributes(); 
lp.screenBrightness = brightness/(float)255; 
getWindow().setAttributes(lp);