以编程方式更改屏幕亮度(与电源控件一样)

我正在寻找如何以编程方式改变屏幕的亮度,我发现这是非常好的解决scheme,它工作的很好,但它只适用于我的应用程序是活动的。 我的应用程序closures后,亮度返回到我开始我的应用程序之前相同的价值。

我想能够改变亮度,就像当我按下我的电源小部件的亮度button时一样。 在来自android的电源小部件有3个状态。 一个非常明亮的非常黑暗,一个在中间。 是否有可能改变亮度,就像有人按下这个小部件?

在这里输入图像描述

编辑1:我创build了这个,我添加permision到我的清单,但是当应用程序启动,我没有看到任何亮度的变化,我尝试了10与100,现在与200但没有任何改变的build议?

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); android.provider.Settings.System.putInt(this.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, 200); } 

这是我发现工作的完整代码:

 Settings.System.putInt(this.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 20); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness =0.2f;// 100 / 100.0f; getWindow().setAttributes(lp); startActivity(new Intent(this,RefreshScreen.class)); 

我的问题的代码不起作用,因为屏幕不刷新。 因此,一个刷新屏幕上的黑客开始虚拟活动,并在创build该虚拟活动调用finish()使亮度的变化生效。

在该链接中使用Tor-Morten的解决scheme,还可以设置系统亮度设置:

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

其中, bright是从1到255的整数。

我今天解决了这个问题。

作为第一个你必须把权限放到你的AndroidManifest.xml文件中:

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

哪里是放在文件中的确切位置?

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

此权限表示,您也可以更改影响其他应用程序的设置。

现在您可以开启和closures亮度自动模式

 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); //this will set the automatic mode on Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); //this will set the manual mode (set the automatic mode off) 

自动模式现在是打开还是closures? 你可以得到这些信息

 int mode = -1; try { mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); //this will return integer (0 or 1) } catch (Exception e) {} 

所以,如果你想手动改变亮度,你应该首先设置手动模式,之后你可以改变亮度。

注意:SCREEN_BRIGHTNESS_MODE_AUTOMATIC是1

注意:SCREEN_BRIGHTNESS_MODE_MANUAL是0

你应该使用这个

 if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { //Automatic mode } else { //Manual mode } 

而不是这个

 if (mode == 1) { //Automatic mode } else { //Manual mode } 

现在您可以手动更改亮度

 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); //brightness is an integer variable (0-255), but dont use 0 

并读取亮度

 try { int brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); //returns integer value 0-255 } catch (Exception e) {} 

现在一切正常,但是…你看不到变化。 你还需要一件事才能看到变化! 刷新屏幕…所以这样做:

  try { int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); //this will get the information you have just set... WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = (float) br / 255; //...and put it here getWindow().setAttributes(lp); } catch (Exception e) {} 

不要忘记权限…

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

在设置参数的同时传递活动的上下文也可以完成工作,而无需启动另一个活动。 以下为我工作 –

 WindowManager.LayoutParams lp = this.getWindow().getAttributes(); lp.screenBrightness =0.00001f;// i needed to dim the display this.getWindow().setAttributes(lp); 

我在onSensorChanged()方法里面有这样一段代码,当触发它的时候它会使显示变暗。

复杂的例子:

  try { //sets manual mode and brightnes 255 Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); //this will set the manual mode (set the automatic mode off) Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); //this will set the brightness to maximum (255) //refreshes the screen int br = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.screenBrightness = (float) br / 255; getWindow().setAttributes(lp); } catch (Exception e) {}