我正在做一个必须将加速计数据记录在记录文件中的项目。任何人都可以帮助我使用以下代码?为了记录加速度计数据,应该在以下代码中添加的android代码是什么?我可能希望每10毫秒获得一次数据。任何帮助是极大的赞赏。
package com.example.helloandroid;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AccActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object
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_NORMAL);
/* More sensor speeds (taken from api docs)
SENSOR_DELAY_FASTEST get sensor data as fast as possible
SENSOR_DELAY_GAME rate suitable for games
SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes
*/
}
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];
float y=event.values[1];
float z=event.values[2];
xCoor.setText("X: "+x);
yCoor.setText("Y: "+y);
zCoor.setText("Z: "+z);
}
}
}
答案 0 :(得分:1)
您接收数据的速率将取决于操作系统的自由裁量权,因为数据是由触发回调的事件传递的。它也可能依赖于设备。您无法以预定速率轮询传感器。 SensorManager API文档状态
public boolean registerListener (SensorEventListener listener, Sensor sensor, int rate)
费率传感器事件在。这只是对系统的暗示。可以比指定的速率更快或更慢地接收事件。通常会更快地收到事件。该值必须是SENSOR_DELAY_NORMAL,SENSOR_DELAY_UI,SENSOR_DELAY_GAME或SENSOR_DELAY_FASTEST 之一,或者是事件之间所需的延迟(以微秒为单位)。
因此,您可以询问延迟10,000微秒(10毫秒),只有通过实验来测量它才能确定传递给您的onSensorChanged的频率。