简体   繁体   English

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

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

I was searching how to change the brightness of the screen programmatically and I found this it is very good solution and it works nice, but it works only while my app is active. 我正在搜索如何以编程方式更改屏幕的亮度,我发现是一个非常好的解决方案,它工作得很好,但它只在我的应用程序处于活动状态时才有效。 After my application is shutdown then the brightness is returned back the the same value as before I start my app. 我的应用程序关闭后,亮度返回与我启动应用程序之前相同的值。

I want to be able to change the brightness just like when I press on the brightness button from my power widget. 我希望能够改变亮度就像我从电源小部件按下亮度按钮一样。 In the power widget that comes from android there are 3 states. 在来自android的电源小部件中有3个状态。 One very bright one very dark and one in between. 一个非常明亮的一个非常黑暗,一个介于两者之间 Is it possible to change the brightness just like when someone press on this widget ? 是否有可能像有人按下这个小部件一样改变亮度?

在此输入图像描述

Edit1: I created this and I added permision to my manifest but when the app is started I do not see any changes to the brightness, I tried with 10 with 100 and now with 200 but no changes any suggestions ? 编辑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);
}

This is the complete code I found to be working: 这是我发现的完整代码:

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

The code from my question does not work because the screen doesn't get refreshed. 我的问题中的代码不起作用,因为屏幕没有刷新。 So one hack on refreshing the screen is starting dummy activity and than in on create of that dummy activity to call finish() so the changes of the brightness take effect. 因此,刷新屏幕的一个方法是启动虚拟活动,而不是在创建虚拟活动时调用finish()以便亮度的更改生效。

Use Tor-Morten's solution in that link, but also set the system brightness setting like so: 在该链接中使用Tor-Morten的解决方案,但也设置系统亮度设置如下:

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

Where bright is an integer ranging from 1 to 255. 其中bright是1到255之间的整数。

I have solved this problem today. 我今天解决了这个问题。

As the first you have to put a permission into your AndroidManifest.xml file: 首先,您必须将权限放入AndroidManifest.xml文件中:

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

Where is the exact place to put it in the file? 把它放在文件中的确切位置在哪里?

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

This permission says, that you are allowed to change settings that affect other applications too. 此权限说明,您也可以更改影响其他应用程序的设置。

Now you can set brightness automatic mode on and off 现在您可以打开和关闭亮度自动模式

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)

Is the automatic mode turned on or off right now? 现在是自动模式打开还是关闭? You can get the information 您可以获取信息

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

So, if you want to change brightness manually, you are supposed to set the manual mode first and after that you can change the brightness. 因此,如果您想手动更改亮度,则应首先设置手动模式,然后可以更改亮度。

note: SCREEN_BRIGHTNESS_MODE_AUTOMATIC is 1 注意:SCREEN_BRIGHTNESS_MODE_AUTOMATIC为1

note: SCREEN_BRIGHTNESS_MODE_MANUAL is 0 注意:SCREEN_BRIGHTNESS_MODE_MANUAL为0

You should use this 你应该用它

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

instead of this 而不是这个

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

Now you can change the brightness manually 现在您可以手动更改亮度

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

and read brightness 并读取亮度

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

Now everything is set properly, but... you can't see the change yet. 现在一切都设置得很好,但是......你还看不到变化。 You need one more thing to see the change! 你还需要一件事才能看到变化! Refresh the screen... so do this: 刷新屏幕......所以这样做:

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

Don't forget the permission... 不要忘记许可......

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

Passing the context of the activity while setting up the parameters would also get the job done without any need to start another activity. 在设置参数的同时传递活动的上下文也可以完成工作,而无需启动其他活动。 The following worked for me- 以下为我工作 -

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

i had this piece of code inside onSensorChanged() method which dimmed the display whenever it was triggered. 我在onSensorChanged()方法中有这段代码,它会在触发时使显示变暗。

COMPLEX EXAMPLE: 复杂的例子:

    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