获取pferred $ P $屏幕亮度Android中亮度、屏幕、pferred、Android

2023-09-04 23:55:35 作者:深沼_

你如何获得了preferred屏幕亮度的Andr​​oid?

How do you get the preferred screen brightness in Android?

要改变屏幕亮度我用WindowManager.LayoutParams.screenBrightness.根据该文件:

To change the screen brightness I use WindowManager.LayoutParams.screenBrightness. According to the documentation:

这可以用来覆盖   用户的preferred的亮度   屏幕。小于0的值,则   默认情况下,意味着要使用preferred   屏幕亮度。 0比1调整   亮度从暗到全亮。

This can be used to override the user's preferred brightness of the screen. A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright.

screenBrightness 小于0,我想先从preferred屏幕亮度。我怎样才能得到这个值?

When screenBrightness is less than 0 I would like to start with the preferred screen brightness. How can I get this value?

推荐答案

我会尽力回答,因为我已经有一段时间前搜索此。我的短的答案是:我从来没有找到一种方式来获得当前的亮度水平上时,自动模式,经过广泛的研究

I'll try to answer because I already searched for this some time ago. My short answer is: I've never found a way to get current brightness level when on auto mode, after extensive research.

我会打入多个答案这一点。我不会跳过premises,从而为谷歌能有所帮助。跳过你不需要的东西。

I will break this into multiple answers. I won't skip premises, so as to googlers can find this helpful. Skip what you don't need.

1获取的亮度设定值

screenBrightness 小于0,我想先从preferred屏幕亮度。我怎样才能得到这个值?

When screenBrightness is less than 0 I would like to start with the preferred screen brightness. How can I get this value?

对于一个人谁拥有声誉的量,我相信你已经知道搜索的演练。有吨这个位置上的SO答案。只需选中右侧面板,类似的问题......这么说,我就不详细了。

For a person who has that amount of reputation, I'm sure you already know the drill about searching. There are tons of answers about this here on SO. Just check the right panel with similar questions... that said, I won't detail much.

int oldBrightness = Settings.System.getInt(getContext().getContentResolver(), 
             Settings.System.SCREEN_BRIGHTNESS);

这是Android的Brightness$p$pference.java做到这一点在2.3.5(见第68行)。这也是相同的(由于实际原因)什么lfor张贴。

This is how Android's BrightnessPreference.java do it in 2.3.5 (see line 68). It's also the same (for practical reasons) of what lfor posted.

这不叫的旧的亮度白白。这是某处存储的设定,而不是当前值。你只能确保它是相同的用户正在体验,如果你进入手动亮度模式。否则,你只是不知道。您应该检查:

It's not named old Brightness for nothing. This is a setting stored somewhere, not the current value. You can only be sure that it's the same of what the user is experiencing if you're into manual brightness mode. Otherwise, you just don't know. You should check:

int mode = Settings.System.getInt(getContext().getContentResolver(),
                     Settings.System.SCREEN_BRIGHTNESS_MODE);

2 - 听屏幕亮度的变化

我们知道,通常情况下,当我们要听的变化在Android中,我们可以尝试听意图动作,注册的BroadcastReceiver 和一切。然而, AFAIK 的,没有操作_ 的亮度变化,如SCREEN_[ON|OFF],例如。也许有一个隐藏在系统中,但我从来没有见过它,如果它不是公开的,你必须处理的所有可能的隐患。

As we know, generally, when we want to listen to changes in Android, we can try listening to Intent actions, registering BroadcastReceivers and all that. However, AFAIK, there is no ACTION_ for brightness changes, like SCREEN_[ON|OFF], for example. Maybe there is one hidden in the system, but I've never seen it, and if it's not publicly available, you have all the possible pitfalls of dealing with that.

有一条信息,日期为2010年12月,从戴安娜Hackborn,她说,的没有广播的屏幕亮度的变化的。

There is a message, dated Dec 2010, from Dianne Hackborn, where she says that "there is no broadcast for screen brightness changes".

所以,我不相信有一种方法挂钩到类似的东西。当然,你可以改变设置本身:

So, I don't believe there is a way to hook up into something like that. Of course, you can change the setting itself:

Settings.System.putInt(getContext().getContentResolver(),
    SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

3听周围光线的变化

您可以但是,aprox的。检测的光的量。您需要实现SensorEventListener

You can, however, aprox. detect the amount of light. You need to implements SensorEventListener and:

// class definition:
private SensorManager sensorManager;

@Override
public void onSensorChanged(SensorEvent event) {
    float values = event.values[0]; // Ambient LUX
}
// onCreate:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);   
sensorManager.registerListener(this,
    sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
    SensorManager.SENSOR_DELAY_UI);

4-猜测亮度变化

在所有的说,我还必须重申Hackborn的问题:你有什么要完成? (这是赏金所有者)。我很高兴地看到Android的环境如何勒克斯映射到屏幕亮度。我敢肯定它不是一个线性的事情(图 Sensor.getMaximumRange()线性到0..1浮动区间)。我已经看到了这个普遍被映射对数(提供更好的调整到较低的亮度范围),但我不知道有关的参数。或许有,每个人都使用建议的默认方式,但我不是专家。

After all that said, I must also repeat Hackborn's question: "what are you trying to accomplish?" (this is for the bounty owner). I'd be happy to see how Android maps ambient lux to screen brightness. I'm quite sure it's not a linear thing (map Sensor.getMaximumRange() linearly to the 0..1 float interval). I've seen that generally this is mapped logarithmic (provide finer adjustments into the lower brightness range), but I have no idea about the parameters. Perhaps there is a suggested default way that everyone uses, but I'm no expert.

如果你能找到的如何的机器人做它通过寻找到在它这样做,那将是巨大的资源的地方。但无论如何,我没来这都渴望离开你同样的问题。我所发现的是, Sensor.getMaximumRange()是不是与你从 SensorEvent 值[0] 。在这里,我有一个夏普传感器(Nexus S的),虽然该报告最大是3000,我见过最多10,240的值。

If you could find how Android does it by looking into the place in the sources where it does that, that would be great. But anyway, I didn't come all this long to leave you with the same question. What I've found is that Sensor.getMaximumRange() is not consistent with what you get from the SensorEvent values[0]. Here, I have a Sharp sensor (Nexus S), and although the report max is 3,000, I've seen values of up to 10,240.

因此​​,获得的传感器是不可靠的(?)。我猜,由我使用的是Android看到的行为,这在某种程度上也映射getMaximumValue()到最大亮度,它上面的任何值只是不有所作为。

So, getting the sensor is not reliable (?). I'm guessing, by the behavior I've seen using Android, that somehow it maps getMaximumValue() to the max brightness, and any value above it just doesn't make a difference.

5凡从这里走

我想看看更多的进入Android的源。某处,可能是在一个较低的水平,硬件管理的东西,你可以看到他们是如何映射。不过,我不知道这是否是可靠的,考虑什么戴安娜Hackborn说(他们似乎不希望你这样做)。我猜他们有一个理由,不公开的。也许需要:为什么应用程序必须知道系统的设置?它或者需要提供他自己的,应用范围的水平,或者,在极端情况下,对于全系统的更换或管理者提供它自己的自动亮度调节改变方法(如上述第4点)。也许,这就是最初的开发思想。

I'd look more into Android sources. Somewhere, probably in a lower level, hardware managing stuff, you could see how they map. Still, I wonder if that's reliable, considering what Dianne Hackborn said (they seem to not want you to do that). I'd guess they had a reason to not expose that. Perhaps need: why does the app must know the system setting? It either needs to provide his own, app-wide level, or, in extreme cases, provide its own "autobrightness change method" (as in point 4 above) for a system-wide replacement or the manager. Perhaps that's what the original developers thought.

总之,如果有人找到其他的细节,我会非常高兴知道。

Anyway, if anyone find other details, I'd be extremely happy to know.

 
精彩推荐
图片推荐