点击按钮可降低屏幕亮度

时间:2012-03-14 05:38:44

标签: android

我有一个问题,我想在按钮点击时降低屏幕的亮度,但没有这样做。我不知道为什么?我写了几行代码,但它对我不起作用。请建议我正确的解决方案。

代码:

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();
        }
      }  

2 个答案:

答案 0 :(得分: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);
        }

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




}

}

答案 1 :(得分: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();

<强>更新

主要代码是,

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

也试试这个.. 试试这个,让我知道发生了什么,