Get current SensorEvent value

余生颓废 提交于 2019-12-11 01:39:25

问题


I am using Sensor.TYPE_STEP_COUNTER. As far as I know, the android SensorEvent maintains a value since reboot. I can use the following codes to get the value once the onSensorChanged method is called.

Like:

public void onSensorChanged(SensorEvent event) {
    System.out.println(event.values[0] );
}

However, how can I get the current value without using such a method? I try to use :

SensorEvent event = new SensorEvent();
float value = event.value[0]; 

This won't work. Is there a similar way to get the current Sensor value?


回答1:


The sensor information is only available as SensorEvent in the method onSensorChanged. The sensor can not be polled like you try to do.

What you can do is to store the float[] event.values during the onSensorChanged method in a class member variable. This way you can retrieve it in the method where you want to use your event.values.



来源:https://stackoverflow.com/questions/29052204/get-current-sensorevent-value

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