我的活动实施SensorEventListener
,我将sensorManager
注册到三个传感器:
// register all the sensors
if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
if (sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null) {
gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
}
if (sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null) {
magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}
我还在onResume()和onPause()中注册和注销了传感器管理器。
我有一个方法saveData,可以检测所有传感器更改并写入文件。
我使用了以下方法:Is there a way to retrieve multiple sensor data in Android
@Override
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
currentTime = System.currentTimeMillis();
if (currentTime >= startTime && currentTime <= endTime) {
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
} else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gx = event.values[0];
gy = event.values[1];
gz = event.values[2];
} else if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mx = event.values[0];
my = event.values[1];
mz = event.values[2];
}
content += (currentTime - startTime) + ax + " " + ay + " " + az + " " + mx + " " + my + " " + mz + " " + gx + " " + gy + " " + gz + "\n";
}
}
我的文件保存方法:
try {
file = new File(Environment.getExternalStorageDirectory(), filename);
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(title.getBytes());
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (IOException e) {
但问题是内容变量根本不会改变。是因为所有传感器都在同时变化吗?如何实际检索数据到内容并写入文件?