繁体   English   中英

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

[英]Changing screen brightness programmatically (as with the power widget)

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

我希望能够改变亮度就像我从电源小部件按下亮度按钮一样。 在来自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);
}

这是我发现的完整代码:

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

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

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>

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

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

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)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM