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

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

我正在搜索如何以编程方式更改屏幕的亮度,我发现这是一个非常好的解决方案,它工作得很好,但它只在我的应用程序处于活动状态时才有效。我的应用程序关闭后,亮度返回与我启动应用程序之前相同的值。

我希望能够改变亮度就像我从电源小部件按下亮度按钮一样。在来自android的电源小部件中有3个状态。一个非常明亮的一个非常黑暗,一个介于两者之间 是否有可能像有人按下这个小部件一样改变亮度?

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

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


呼啦一阵风
浏览 416回答 3
3回答

慕工程0101907

这是我发现的完整代码: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));我的问题中的代码不起作用,因为屏幕没有刷新。因此,刷新屏幕的一个方法是启动虚拟活动,而不是在创建虚拟活动时调用,finish()以便亮度的更改生效。

德玛西亚99

在该链接中使用Tor-Morten的解决方案,但也设置系统亮度设置如下:android.provider.Settings.System.putInt(getContext().getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS, bright);其中bright整数范围是1到255。

RISEBY

我今天解决了这个问题。首先,您必须将权限放入AndroidManifest.xml文件中:<uses-permission&nbsp;android:name="android.permission.WRITE_SETTINGS"&nbsp;/>把它放在文件中的确切位置在哪里?<manifest> &nbsp;&nbsp;&nbsp;&nbsp;<uses-permission&nbsp;android:name="android.permission.WRITE_SETTINGS"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;<application> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<activity&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;</application></manifest>此权限说明,您也可以更改影响其他应用程序的设置。现在您可以打开和关闭亮度自动模式Settings.System.putInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE,&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);&nbsp;&nbsp;//this&nbsp;will&nbsp;set&nbsp;the&nbsp;automatic&nbsp;mode&nbsp;onSettings.System.putInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE,&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);&nbsp;&nbsp;//this&nbsp;will&nbsp;set&nbsp;the&nbsp;manual&nbsp;mode&nbsp;(set&nbsp;the&nbsp;automatic&nbsp;mode&nbsp;off)现在是自动模式打开还是关闭?您可以获取信息int&nbsp;mode&nbsp;=&nbsp;-1;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;mode&nbsp;=&nbsp;Settings.System.getInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE);&nbsp;//this&nbsp;will&nbsp;return&nbsp;integer&nbsp;(0&nbsp;or&nbsp;1)}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{}因此,如果您想手动更改亮度,则应首先设置手动模式,然后可以更改亮度。注意:SCREEN_BRIGHTNESS_MODE_AUTOMATIC为1注意:SCREEN_BRIGHTNESS_MODE_MANUAL为0你应该用它if&nbsp;(mode&nbsp;==&nbsp;Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//Automatic&nbsp;mode}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//Manual&nbsp;mode}而不是这个if&nbsp;(mode&nbsp;==&nbsp;1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//Automatic&nbsp;mode}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//Manual&nbsp;mode}现在您可以手动更改亮度Settings.System.putInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS,&nbsp;brightness);&nbsp;&nbsp;//brightness&nbsp;is&nbsp;an&nbsp;integer&nbsp;variable&nbsp;(0-255),&nbsp;but&nbsp;dont&nbsp;use&nbsp;0并读取亮度try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;brightness&nbsp;=&nbsp;Settings.System.getInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS);&nbsp;&nbsp;//returns&nbsp;integer&nbsp;value&nbsp;0-255}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{}现在一切都设置得很好,但是......你还看不到变化。你还需要一件事才能看到变化!刷新屏幕......所以这样做:&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;br&nbsp;=&nbsp;Settings.System.getInt(getContentResolver(),&nbsp;Settings.System.SCREEN_BRIGHTNESS);&nbsp;&nbsp;//this&nbsp;will&nbsp;get&nbsp;the&nbsp;information&nbsp;you&nbsp;have&nbsp;just&nbsp;set... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WindowManager.LayoutParams&nbsp;lp&nbsp;=&nbsp;getWindow().getAttributes(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lp.screenBrightness&nbsp;=&nbsp;(float)&nbsp;br&nbsp;/&nbsp;255;&nbsp;//...and&nbsp;put&nbsp;it&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getWindow().setAttributes(lp); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{}不要忘记许可......<uses-permission&nbsp;android:name="android.permission.WRITE_SETTINGS"&nbsp;/>
打开App,查看更多内容
随时随地看视频慕课网APP