获取当前SensorEvent值 [英] Get current SensorEvent value

查看:1208
本文介绍了获取当前SensorEvent值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Sensor.TYPE_STEP_COUNTER 。据我所知,Android的 SensorEvent 维持重启以来的值。我可以用下面的codeS获得一旦 onSensorChanged 方法被称为:

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:

public void onSensorChanged(SensorEvent event) {

System.ou.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 vaue = event.value[0]; 

这是行不通的。有没有类似的方式来获得当前的传感器读数?

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

推荐答案

传感器信息仅作为SensorEvent的方法onSensorChanged。像你试图做的传感器不能被轮询。

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

你可以做的是一个类的成员变量onSensorChanged法期间存储浮法[] event.values​​。这样你可以在你想用你的event.values​​方法检索。

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.

这篇关于获取当前SensorEvent值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆