单击按钮后 x 秒内获取传感器数据
我仍在学习 Android 开发,如果这个问题看起来很愚蠢,我很抱歉,但我试图仅在有限的时间内从手机获取加速度计数据。目标是在单击按钮后仅记录约 3 秒的传感器 x 坐标值。
public class AccelerometerTestingActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
float x_event[];
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
// check sensor type
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
x_event=addArrayElement(x_event,x);
}
}
private float[] addArrayElement(float[] currentArray, float x) {
// TODO Auto-generated method stub
float newArray[] = new float[currentArray.length+1];
int i= 0;
for(i=0;i<currentArray.length;i++)
{
newArray[i] = currentArray[i];
}
newArray[i+1]=x;
return newArray;
}
所以 x_event 是一个数组,每个 SENSOR_DELAY_GAME 定期填充加速度计的 X 位置。
问题是,此代码记录了活动运行时的所有值。
我想要的是这个数组仅从单击按钮时开始填充,并且仅填充 x 秒(尚未决定 x 的值)。之后,该数组将被传递给另一个函数进行分析。
我只是不明白如何及时限制监听器。
感谢您的帮助。
I'm still learning Android Development so sorry if this question seems stupid but I'm trying to obtain accelerometer data from the Phone for a limited time only. The goal is for the x-coordinate values from the sensor to be recorded only for about 3 seconds after a button is clicked.
public class AccelerometerTestingActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
float x_event[];
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
// check sensor type
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
x_event=addArrayElement(x_event,x);
}
}
private float[] addArrayElement(float[] currentArray, float x) {
// TODO Auto-generated method stub
float newArray[] = new float[currentArray.length+1];
int i= 0;
for(i=0;i<currentArray.length;i++)
{
newArray[i] = currentArray[i];
}
newArray[i+1]=x;
return newArray;
}
So x_event is an array filled with the X position of the Accelerometer periodically every SENSOR_DELAY_GAME.
The thing is, this code records all values while activity is running.
What I would like is for this array to be filled only from the time a button is clicked, and filled only for x seconds (haven't decided on the value of x yet). After that the array would be passed to another function for analysis.
I just don't understand how to limit the Listener in time.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个快速解决方案;
在注册监听器后,在
onCreate()
中,您可以执行以下操作:您还应该考虑使用列表而不是数组来保存值,这样您就不必每次都重新创建它获得新值的时间:
并添加项目:
Here's a quick solution;
in your
onCreate()
after you have registered your listener, you can do this:You should also consider using a list rather than an array to save your values, so you don't have to recreate it every time you get a new value:
And to add items:
您是否考虑过使用一个计时器,在 3 秒延迟后,
- 取消注册监听器
- 调用您的分析功能
?
Have you considered using a timer that would, after a 3 secs delay,
- unregister the listener
- call your analysis function
?
解决了问题
只需使用 Timer 类方法每 1 秒执行一次
我认为更好的解决方案
Solved the Problem
Just use Timer class method do this every 1s
A better Solution I think