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

发布于 2024-12-08 04:09:04 字数 763 浏览 2 评论 0原文

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

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

在此处输入图像描述

编辑1: 我创建了这个,并向我的清单添加了权限,但是当应用程序启动时,我没有看到亮度有任何变化,我尝试使用 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);
}

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. 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 ?

enter image description here

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 ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

肤浅与狂妄 2024-12-15 04:09:04

这是我发现有效的完整代码:

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

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.

放飞的风筝 2024-12-15 04:09:04

使用该链接中的 Tor-Morten 解决方案,同时设置系统亮度设置,如下所示:

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

其中 bright 是一个范围从 1 到 255 的整数。

Use Tor-Morten's solution in that link, but also set the system brightness setting like so:

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

Where bright is an integer ranging from 1 to 255.

流绪微梦 2024-12-15 04:09:04

我今天已经解决了这个问题。

首先,您必须将权限放入 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" />

I have solved this problem today.

As the first you have to put a permission into your AndroidManifest.xml file:

<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

note: SCREEN_BRIGHTNESS_MODE_MANUAL is 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" />
谎言月老 2024-12-15 04:09:04

在设置参数时传递活动的上下文也可以完成工作,而无需启动另一个活动。
以下内容对我有用 -

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

我在 onSensorChanged() 方法中添加了这段代码,每当触发时,它就会使显示变暗。

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.

允世 2024-12-15 04:09:04

复杂的例子:

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

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) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文