java 获取光传感器值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6433889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Light Sensor Value
提问by Cistoran
So I'm looking for a way to get the current value of the Light Sensor (in Lux obviously) on a button press. Here is the code I'm using to implement control of the light sensor
所以我正在寻找一种方法来在按下按钮时获得光传感器的当前值(显然是勒克斯)。这是我用来实现光传感器控制的代码
public class SensorActivity extends Activity implements SensorEventListener
{
private final SensorManager mSensorManager;
private final Sensor mLight;
int minLux = 0;
int currentLux = 0;
int maxLux;
public SensorActivity()
{
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}
protected void onResume()
{
super.onResume();
mSensorManager.registerListener((SensorEventListener) this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause()
{
super.onPause();
mSensorManager.unregisterListener((SensorListener) this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
if(sensor.getType() == Sensor.TYPE_LIGHT)
{
}
}
public void onSensorChanged(SensorEvent event)
{
if( event.sensor.getType() == Sensor.TYPE_LIGHT)
{
}
}
}
I also have this code making the button to call a function
我也有这个代码使按钮调用函数
<Button android:layout_width="122px" android:id="@+id/widget35" android:text="Start" android:layout_height="wrap_content" android:layout_x="112dp" android:layout_y="249dp" android:onClick="onButtonDown"></Button>
How would I get the current lux value while the app is running in the background, and how would I get and store the current lux value on button press into int maxLux;
?
当应用程序在后台运行时,我将如何获取当前的勒克斯值,以及如何在按下按钮时获取和存储当前的勒克斯值int maxLux;
?
回答by Mark
As noted here, it isn't possible to directly get the current sensor value and, while some more recent versions of Android will generate an onSensorChanged event as soon as the listener is registered (allowing you to get a baseline), earlier versions don't. So the only thing you can really do is hope that the user is either running a more recent version of Android or that the value changes and then retrieve the current lux value from event.values[0] inside onSensorChanged (e.g.) :
如前所述在这里,它是不可能直接将电流传感器值,而更近一些的Android版本将尽快注册该侦听器产生onSensorChanged事件(让你得到一个基线),早期版本的不要吨。因此,您真正能做的唯一一件事就是希望用户要么运行更新版本的 Android,要么希望值发生变化,然后从 onSensorChanged 中的 event.values[0] 检索当前的勒克斯值(例如):
public void onSensorChanged(SensorEvent event)
{
if( event.sensor.getType() == Sensor.TYPE_LIGHT)
{
currentLux = event.values[0];
if (currentLux > maxLux)
maxLux = currentLux;
}
}
(After changing maxLux and currentLux to floats). This then leaves the issue of retrieving the current value based on a button press, which can be done by looking at the value of currentLux at the time the button is pressed.
(将 maxLux 和 currentLux 更改为浮点数后)。这就留下了基于按钮按下检索当前值的问题,这可以通过查看按下按钮时 currentLux 的值来完成。