Getting Light Sensor Value

安稳与你 提交于 2019-12-03 07:39:32
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.) :

 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!