2012-03-14 87 views
0

我有一個問題,我想降低按鈕點擊屏幕的亮度,但沒有做到這一點。我不知道爲什麼?我寫了幾行代碼,但它不適用於我。請給我建議正確的解決方案。降低按鈕點擊屏幕的亮度

代碼:

private void setBrightness() { 

     try { 
      int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS); 
      WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
      layoutParams.screenBrightness = curBrightnessValue/100.0f; 
      getWindow().setAttributes(layoutParams); 
     } catch (SettingNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

回答

0

你的代碼似乎不錯,但我認爲採取亮度您的活動必須等待500ms的效果,只需要使用下面給出的代碼,

try { 
     ContentResolver cr = getContentResolver(); 
     int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);    
     Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness); 
     WindowManager.LayoutParams lp = getWindow().getAttributes(); 
     lp.screenBrightness = brightness/255.0f; 
     getWindow().setAttributes(lp); 
     } catch (Exception e) { 
     Log.d("Bright", "toggleBrightness: " + e); 
     } 

     final Activity activity = this; 
     Thread t = new Thread(){ 
     public void run() 
     { 
      try { 
       sleep(500); 
       } catch (InterruptedException e) {} 
       activity.finish(); 
      } 
     }; 
     t.start(); 

UPDATE:

的主要代碼是正義的,

WindowManager.LayoutParams layout = getWindow().getAttributes(); 
layout.screenBrightness = 1F; 
getWindow().setAttributes(layout); 

也可以嘗試這個.. 試試這個,讓我知道發生什麼事,

+0

此代碼完成的活動,同時調用相同的代碼由於activity.finish()語句,當我刪除了這一說法那麼這段代碼執行但屏幕上沒有任何影響。 – 2012-03-14 06:03:22

+0

欲瞭解更多信息看[autobright](http://code.google.com/p/autobright/source/browse/trunk/autobright/src/com/geekyouup/android/autobright/AutoBright.java) – user370305 2012-03-14 06:03:27

+0

嘿Thanx它通過將divident 255.0f增加到1500.0f來工作。 – 2012-03-14 06:19:41

2

使用IHardwareService接口這樣的:

許可

<uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission> 

修改代碼,如:

private void setBrightness(int brightness) { 
    try { 
     IHardwareService hardware = IHardwareService.Stub.asInterface( 
ServiceManager.getService("hardware")); 
     if (hardware != null) { 
     hardware.setScreenBacklight(brightness); 
     } 
    } catch (RemoteException doe) {    
    } 

完整的例子這裏Changing the Screen Brightness

或在您的情況下,也許可能:

WindowManager.LayoutParams lp = getWindow().getAttributes(); 

移動這個給你做setContentView(R.layout.main); 後構造窗口之前,你不能這樣做getWindow().getAttributes()

因此,你的代碼將成爲

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    // MY BRIGHTNESS VARIABLES 


WindowManager.LayoutParams lp; 
float fb; 
float lb = 0; 
float hb = 1; 
////////////////////////////////////////////////////////////////////////// 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    lp = getWindow().getAttributes(); 
    fb = lp.screenBrightness; 

    // MY CODE FROM HERE DOWN 

    Button button1=(Button)findViewById(R.id.button1); 

    button1.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 

     if(lp.screenBrightness==fb) { 
      lp.screenBrightness=lb; 
      getWindow().setAttributes(lp); 
     } 
     if(lp.screenBrightness==lb){ 
      lp.screenBrightness=hb; 
      getWindow().setAttributes(lp); 
     } 
     if(lp.screenBrightness==hb){ 
      lp.screenBrightness=fb; 
      getWindow().setAttributes(lp); 
     } 

    } 
}); 
    ////////////////////////////////////////////// 




} 

}