2012-03-14 52 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); 
     } 

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




} 

}