【问题标题】:Changing screen brightness programmatically (as with the power widget)以编程方式更改屏幕亮度(与电源小部件一样)
【发布时间】:2011-11-30 14:30:19
【问题描述】:

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

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

【问题讨论】:

标签: android screen brightness screen-brightness


【解决方案1】:

在该链接中使用 Tor-Morten 的解决方案,但也可以像这样设置系统亮度设置:

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

其中bright 是一个介于 1 到 255 之间的整数。

【讨论】:

  • 我遇到了一些异常我猜我没有权限,你知道权限吗
  • 有人知道我需要什么权限吗?
  • 我添加了权限 并且它不会抛出异常但这根本不会改变亮度,你能高吗我有什么问题?
  • 在调整窗口亮度之前尝试执行上述代码。我认为您可能需要等待一小段时间才能在 Activity 上调用 finish() 。因此,如果您在设置亮度后立即关闭它,可能会添加一个小延迟。
  • 运行活动后它保持活动状态,我没有关闭它,但它仍然没有改变亮度。有没有可以看到完整运行代码的地方?
【解决方案2】:

这是我发现工作的完整代码:

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(),这样亮度的变化才会生效。

【讨论】:

  • 奇怪的是你必须这样做。 :/ 你用的是什么手机?
  • 这并不奇怪,我使用 htc 的愿望,我也尝试了三星 Galaxy 上的代码......这不是设备问题,而是 android 的工作方式,它根本不应用亮度,直到获胜经理得到刷新,他通过按返回、主页按钮或创建新活动来刷新......
  • 也许如果你使用一个小部件不需要刷新,但如果你把代码放在 oncreate 中,它肯定需要启动额外的活动,(至少这是我的情况)没有那个应用程序不起作用的虚拟活动属性
  • @Lukap 我在 AppWidget 类中使用 Intent 调用虚拟活动,但它强制关闭。
  • 这适用于 2.3 的 HTC 愿望(但我从未在 4.0 上尝试过)
【解决方案3】:

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

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

我在 onSensorChanged() 方法中有这段代码,当它被触发时,它会使显示变暗。

【讨论】:

  • 永远不要将其设置为零。将其设置为 0.00001f。将其设置为零会导致一些手机无限期关闭屏幕,迫使您通过按住电源按钮或取出电池来重置手机。
  • 谢谢,修改了答案。
【解决方案4】:

我今天解决了这个问题。

首先您必须在您的 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" />

【讨论】:

【解决方案5】:

复杂示例:

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

【讨论】:

  • 别忘了权限!否则什么都不会发生!
猜你喜欢
  • 1970-01-01
  • 2011-04-13
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
相关资源
最近更新 更多