简体   繁体   English

获得光传感器价值

[英]Getting Light Sensor Value

So I'm looking for a way to get the current value of the Light Sensor (in Lux obviously) on a button press. 所以我正在寻找一种方法来获得按钮按下时光传感器的当前值(显然是在Lux中)。 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; ?

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. 如前所述在这里 ,它是不可能直接将电流传感器值,而更近一些的Android版本将尽快注册该侦听器产生onSensorChanged事件(让你得到一个基线),早期版本的不要吨。 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 (eg) : 因此,您唯一能做的就是希望用户运行更新版本的Android或者更改值,然后从onSensorChanged中的event.values [0]中检索当前的lux值(例如):

 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). (将maxLux和currentLux更改为浮点数后)。 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. 这就留下了根据按下按钮检索当前值的问题,这可以通过查看按下按钮时currentLux的值来完成。

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

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