简体   繁体   English

在Android中获取首选屏幕亮度

[英]Get preferred screen brightness in Android

How do you get the preferred screen brightness in Android? 如何在Android中获得首选屏幕亮度?

To change the screen brightness I use WindowManager.LayoutParams.screenBrightness . 要更改屏幕亮度,请使用WindowManager.LayoutParams.screenBrightness According to the documentation: 根据文件:

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表示使用首选屏幕亮度。 0 to 1 adjusts the brightness from dark to full bright. 0到1可将亮度从暗调整为全亮。

When screenBrightness is less than 0 I would like to start with the preferred screen brightness. screenBrightness小于0时,我想从首选的屏幕亮度开始。 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. 我的简短回答是:经过广泛的研究, 我从未找到过在自动模式下获得当前亮度级别的方法

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- Get brightness setting value 1-获取亮度设定值

When screenBrightness is less than 0 I would like to start with the preferred screen brightness. screenBrightness小于0时,我想从首选的屏幕亮度开始。 How can I get this value? 我怎样才能获得这个价值?

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

This is how Android's BrightnessPreference.java do it in 2.3.5 (see line 68). 这就是Android的BrightnessPreference.java在2.3.5中的表现(见第68行)。 It's also the same (for practical reasons) of what lfor posted. lfor发布的内容也是相同的(出于实际原因)。

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- Listen to screen brightness changes 2-收听屏幕亮度变化

As we know, generally, when we want to listen to changes in Android, we can try listening to Intent actions, registering BroadcastReceiver s and all that. 我们知道,通常,当我们想要听取Android中的更改时,我们可以尝试侦听Intent操作,注册BroadcastReceiver以及所有这些。 However, AFAIK , there is no ACTION_ for brightness changes, like SCREEN_[ON|OFF] , for example. 但是, AFAIK ,亮度变化没有ACTION_ ,例如SCREEN_[ON|OFF] 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. 也许有一个隐藏在系统中,但我从未见过它,如果它不公开,你就有可能陷入困境。

There is a message, dated Dec 2010, from Dianne Hackborn, where she says that "there is no broadcast for screen brightness changes" . 2010年12月,来自Dianne Hackborn的消息称, “屏幕亮度变化没有广播”

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- Listen to ambient light changes 3-听取环境光线的变化

You can, however, aprox. 但是,你可以aprox。 detect the amount of light. 检测光量。 You need to implements SensorEventListener and: 您需要implements SensorEventListener并:

// 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- Guess the brightness change 4-猜测亮度变化

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. 我很高兴看到Android如何将环境lux映射到屏幕亮度。 I'm quite sure it's not a linear thing (map Sensor.getMaximumRange() linearly to the 0..1 float interval). 我很确定它不是线性的(将Sensor.getMaximumRange()线性映射到0..1浮点间隔)。 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. 也许每个人都使用建议的默认方式,但我不是专家。

If you could find how Android does it by looking into the place in the sources where it does that, that would be great. 如果您可以通过查看源代码中的位置来了解 Android的用途,那就太棒了。 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] . 我发现Sensor.getMaximumRange()与您从SensorEvent values[0]获得的内容SensorEvent 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. 在这里,我有一个Sharp传感器(Nexus S),虽然报告最大值为3,000,但我看到的值高达10,240。

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. 我猜测,通过我看到的使用Android的行为,它以某种方式将getMaximumValue()映射到最大亮度,并且它上面的任何值都没有区别。

5- Where to go from here 5-从这里开始

I'd look more into Android sources. 我会更多地了解Android来源。 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). 考虑到Dianne Hackborn说的话,我想知道这是否可靠(他们似乎不希望你这样做)。 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. 它要么需要提供他自己的应用范围级别,要么在极端情况下,为系统范围的替换或管理者提供自己的“自动生成改变方法”(如上面第4点所述)。 Perhaps that's what the original developers thought. 也许这就是原始开发者的想法。

Anyway, if anyone find other details, I'd be extremely happy to know. 无论如何,如果有人发现其他细节,我会非常高兴知道。

Well to answer the question as asked you can get the prefered brightness from Settings.system.SCREEN_BRIGHTNESS The range looks to be 0-255. 很好回答问题,你可以从Settings.system.SCREEN_BRIGHTNESS获得首选亮度。范围看起来是0-255。

int UserBrightness = Settings.System.getInt(getContentResolver(), 
            Settings.System.SCREEN_BRIGHTNESS,-1); 

What the curent brightness actualy is at the moment, as set by Automatic mode or an app setting it's own brightness is a different question realy and I would also be interested in the answer. 目前的实际亮度是什么,由自动模式设置或应用程序设置它自己的亮度是一个不同的问题,我也会对答案感兴趣。

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

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