以编程方式更改系统亮度
我想以编程方式更改系统亮度。 为此我使用这个代码:
WindowManager.LayoutParams lp = window.getAttributes(); lp.screenBrightness = (255); window.setAttributes(lp);
因为我听说最大值是255。
但它什么都不做。请build议任何可以改变亮度的东西。 谢谢
您可以使用以下内容:
//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>
我有同样的问题。
两种解决scheme
这里,亮度= (int) 0 to 100 range
因为我使用的是进度条
1解决scheme
float brightness = brightness / (float)255; WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = brightness; getWindow().setAttributes(lp);
2解决scheme
当我的进度条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来清单。
希望它可以帮助!
我尝试了其他人发布的几个解决scheme,而且没有一个完全正确。 geet的答案基本正确,但有一些语法错误。 我在我的应用程序中创build并使用了以下function,并且效果很好。 请注意,这会特别改变原始问题中所述的系统亮度。
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); }
参考链接
WindowManager.LayoutParams layout = getWindow().getAttributes(); layout.screenBrightness = 1F; getWindow().setAttributes(layout);
这工作对我来说,直到kitkat 4.4,但不是在Android L
private void stopBrightness() { Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 0); }
私人SeekBar亮度=空;
@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); }
请尝试这个,它可以帮助你。 为我工作得很好
根据我的经验
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的亮度值。使用第二种方法的主要问题是它会在Android设备中以增加的forms显示亮度,但实际上不会增加Android设备亮度。这是因为它需要一些清爽。
这就是为什么我一起使用两个代码找出解决scheme。
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); }
它适合我的工作
这是如何改变系统亮度的完整代码
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; } } }); }
或者您可以查看本教程的完整代码
快乐编码:)
WindowManager.LayoutParams params = getWindow().getAttributes(); params.screenBrightness = 10; // range from 0 - 255 as per docs getWindow().setAttributes(params); getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);
这对我有效。 不需要虚拟活动。 这只适用于您当前的活动。