为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼啦一阵风 2019-08-19 15:32:32
以编程方式更改屏幕亮度(与电源小部件一样)我正在搜索如何以编程方式更改屏幕的亮度,我发现这是一个非常好的解决方案,它工作得很好,但它只在我的应用程序处于活动状态时才有效。我的应用程序关闭后,亮度返回与我启动应用程序之前相同的值。我希望能够改变亮度就像我从电源小部件按下亮度按钮一样。在来自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);}
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

这是我发现的完整代码:

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()以便亮度的更改生效。


查看完整回答
反对 回复 2019-08-19
?
德玛西亚99

TA贡献1770条经验 获得超3个赞

在该链接中使用Tor-Morten的解决方案,但也设置系统亮度设置如下:

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

其中bright整数范围是1到255。


查看完整回答
反对 回复 2019-08-19
?
RISEBY

TA贡献1856条经验 获得超5个赞

我今天解决了这个问题。

首先,您必须将权限放入AndroidManifest.xml文件中:

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

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

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

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

现在您可以打开和关闭亮度自动模式

Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);  //this will set the automatic mode onSettings.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)

现在是自动模式打开还是关闭?您可以获取信息

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" />


查看完整回答
反对 回复 2019-08-19
  • 3 回答
  • 0 关注
  • 417 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信