2013-08-19 69 views
24

我想以编程方式更改系统亮度。为此我使用此代码:以编程方式更改系统亮度

,因为我听说,最大值为255

但它确实nothing.please表明,可以改变亮度的任何事情。 感谢

+0

类似的问题:http://stackoverflow.com/questions/3737579/changing-screen-brightness-programmatically-in-android –

+0

可能的重复[无法在Android中以编程方式应用系统屏幕亮度](http:// stackoverflow.com/questions/5032588/cant-apply-system-screen-brightness-programmatically-in-android) – bummi

回答

32

您可以使用下面:

//Variable to store brightness value 
private int brightness; 
//Content resolver used as a handle to the system's settings 
private ContentResolver cResolver; 
//Window object, that will store a reference to the current window 
private Window window; 

在你的onCreate写:

//Get the content resolver 
cResolver = getContentResolver(); 

//Get the current window 
window = getWindow(); 

    try 
      { 
       // To handle the auto 
       Settings.System.putInt(cResolver, 
       Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 
//Get the current system brightness 
       brightness = System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS); 
      } 
      catch (SettingNotFoundException e) 
      { 
       //Throw an error case it couldn't be retrieved 
       Log.e("Error", "Cannot access system brightness"); 
       e.printStackTrace(); 
      } 

编写代码,以监视亮度的变化。

然后可以设置更新亮度如下:在清单

  //Set the system brightness using the brightness variable value 
      System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness); 
      //Get the current window attributes 
      LayoutParams layoutpars = window.getAttributes(); 
      //Set the brightness of this window 
      layoutpars.screenBrightness = brightness/(float)255; 
      //Apply attribute changes to this window 
      window.setAttributes(layoutpars); 

许可:

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

对于API> 23,则需要请求通过设置活动这里所描述的许可,: Can't get WRITE_SETTINGS permission

+1

这里我必须改变亮度变量与我的变量?像在0 - 255之间? ? ? –

+0

我收到错误 - “SCREEN_BRIGHTNESS无法解析或不是字段”System.SCREEN_BRIGHTNESS。 – Ganesh

+0

@Terril,建议使用代码处理自动编辑的任何特定原因? :) –

5

Reference link

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

我可以给的最大和最小值是多少? –

+0

什么是最小值和最大值? –

+0

0和1.请参阅https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#screenBrightness – bk138

12

我有同样的问题。

两个解决方案:

这里,亮度= (int) 0 to 100 range因为我使用进度

1解决方案

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

2溶液

我只是用虚拟活动拨打我的进度条stop寻求。

Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class); 
        Log.d("brightend", String.valueOf(brightness/(float)255)); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important 
        //in the next line 'brightness' should be a float number between 0.0 and 1.0 
        intent.putExtra("brightness value", brightness/(float)255); 
        getApplication().startActivity(intent); 

现在来到DummyBrightnessActivity.class

public class DummyBrightnessActivity extends Activity{ 

private static final int DELAYED_MESSAGE = 1; 

private Handler handler; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);    
    handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      if(msg.what == DELAYED_MESSAGE) { 
       DummyBrightnessActivity.this.finish(); 
      } 
      super.handleMessage(msg); 
     } 
    }; 
    Intent brightnessIntent = this.getIntent(); 
    float brightness = brightnessIntent.getFloatExtra("brightness value", 0); 
    WindowManager.LayoutParams lp = getWindow().getAttributes(); 
    lp.screenBrightness = brightness; 
    getWindow().setAttributes(lp); 

    Message message = handler.obtainMessage(DELAYED_MESSAGE); 
    //this next line is very important, you need to finish your activity with slight delay 
    handler.sendMessageDelayed(message,200); 
} 

} 

不要忘记注册DummyBrightnessActivity体现。

希望它有帮助!

+0

我正在做一个自定义对话框。它不工作。再次我想改变整个系统的亮度。我运行代码,然后检查系统设置的亮度,并没有影响 –

+0

代码将改变整个系统的亮度。 –

+0

首先在'brightness'中用静态值在0到100之间检查。然后使用动态值.put

1

请试试这个,它可以帮助你。工作得很好,我

根据我的经验

1st method. 
        WindowManager.LayoutParams lp = getWindow().getAttributes(); 
       lp.screenBrightness = 75/100.0f; 
       getWindow().setAttributes(lp); 

在亮度值非常根据1.0f.100f是最大亮度。

上面提到的代码会增加当前窗口的亮度。如果我们想提高整个Android设备这段代码是不够的亮度,为此,我们需要使用

2nd method. 



     android.provider.Settings.System.putInt(getContentResolver(), 
         android.provider.Settings.System.SCREEN_BRIGHTNESS, 192); 

其中192是亮度值非常从1到255使用2号的主要问题方法是它会在Android设备中以增加的形式显示亮度,但实际上它不会增加Android设备的亮度。这是因为它需要更新一些。

这就是为什么我通过同时使用两个代码找出解决方案的原因。

  if(arg2==1) 
       { 

     WindowManager.LayoutParams lp = getWindow().getAttributes(); 
       lp.screenBrightness = 75/100.0f; 
       getWindow().setAttributes(lp); 
       android.provider.Settings.System.putInt(getContentResolver(), 
         android.provider.Settings.System.SCREEN_BRIGHTNESS, 192); 


        } 

一切正常,我

9

我尝试了几种解决方案,其他人发布的,其中没有工作完全正确。 geet的答案基本正确,但有一些语法错误。我在我的应用程序中创建并使用了以下功能,并且效果很好。请注意,这会特别改变原始问题中提到的系统亮度。

public void setBrightness(int brightness){ 

    //constrain the value of brightness 
    if(brightness < 0) 
     brightness = 0; 
    else if(brightness > 255) 
     brightness = 255; 


    ContentResolver cResolver = this.getApplicationContext().getContentResolver(); 
    Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness); 

} 
+0

是的它更好的解决方案。它不会引发ThreadException – Max

2

这个工作对我来说,直到奇巧4.4,但不是在Android L移动

private void stopBrightness() { 
    Settings.System.putInt(this.getContentResolver(), 
      Settings.System.SCREEN_BRIGHTNESS, 0); 
} 
+0

它适用于Android 7.1.1(Nexus 9) –

1

这是一个关于如何改变系统的亮度

private SeekBar brightbar; 

    //Variable to store brightness value 
    private int brightness; 
    //Content resolver used as a handle to the system's settings 
    private ContentResolver Conresolver; 
    //Window object, that will store a reference to the current window 
    private Window window; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //Instantiate seekbar object 
     brightbar = (SeekBar) findViewById(R.id.ChangeBright); 

     //Get the content resolver 
     Conresolver = getContentResolver(); 

     //Get the current window 
     window = getWindow(); 

     brightbar.setMax(255); 

     brightbar.setKeyProgressIncrement(1); 

     try { 
      brightness = System.getInt(Conresolver, System.SCREEN_BRIGHTNESS); 
     } catch (SettingNotFoundException e) { 
      Log.e("Error", "Cannot access system brightness"); 
      e.printStackTrace(); 
     } 

     brightbar.setProgress(brightness); 

     brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       System.putInt(Conresolver, System.SCREEN_BRIGHTNESS, brightness); 

       LayoutParams layoutpars = window.getAttributes(); 

       layoutpars.screenBrightness = brightness/(float) 255; 

       window.setAttributes(layoutpars); 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 

       if (progress <= 20) { 

        brightness = 20; 
       } else { 

        brightness = progress; 
       } 
      } 
     }); 
    } 

或者你可以检查这个tutorial的完整代码完整代码
快乐编码:)

2

private SeekBar Brighness = null;

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_lcd_screen_setting); 
    initUI(); 
    setBrightness(); 
} 

private void setBrightness() { 

    Brighness.setMax(255); 
    float curBrightnessValue = 0; 

    try { 
     curBrightnessValue = android.provider.Settings.System.getInt(
       getContentResolver(), 
       android.provider.Settings.System.SCREEN_BRIGHTNESS); 
    } catch (Settings.SettingNotFoundException e) { 
     e.printStackTrace(); 
    } 
    int screen_brightness = (int) curBrightnessValue; 
    Brighness.setProgress(screen_brightness); 

    Brighness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     int progress = 0; 


     @Override 
     public void onProgressChanged(SeekBar seekBar, int progresValue, 
             boolean fromUser) { 
      progress = progresValue; 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
      // Do something here, 
      // if you want to do anything at the start of 
      // touching the seekbar 
     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
      android.provider.Settings.System.putInt(getContentResolver(), 
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 
        progress); 
     } 
    }); 
} 

initUI(){ 
    Brighness = (SeekBar) findViewById(R.id.brightnessbar); 
} 
+0

和在Manifest中添加一个权限 –

+0

0
WindowManager.LayoutParams params = getWindow().getAttributes(); 
    params.screenBrightness = 10; // range from 0 - 255 as per docs 
    getWindow().setAttributes(params); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED); 

这为我工作。不需要虚拟活动。这仅适用于您当前的活动。

相关问题