我无法从 onSensorChanged 方法获取时间戳字符串,而不是将其写入日志文件作为示例 Magnitic Timestamp 列,我正在通过创建传感器应用程序来研究 Java。
在这个应用程序中,我使用日历和日期方法来获取其他传感器的时间戳,但在这种情况下,我只想使用时间戳。
结果是:1970/01/01_08:59:59:926 但我想像这样得到现在的日期和时间:2013/11/01_14:17:02:673
我正在使用以下代码编写日志文件:
final SensorEventListener magniListener = new SensorEventListener() {
private float magnidT;
private long magniLogFileTimestamp;
@Override
public void onSensorChanged(SensorEvent event) {
magniLogFileTimestamp = (event.timestamp - System.nanoTime()) / 1000000L;
if (magnitimestamp != 0) {
magnidT = (event.timestamp - magnitimestamp);
magniValueX = event.values[0];
magniValueY = event.values[1];
magniValueZ = event.values[2];
//here write the sensor values and timestamp to CSV file
if (magniFile != null) {
magniFile.print(String.valueOf(sensorTimestamp
.format(magniLogFileTimestamp)));
//sensorTimestamp = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss:SSS");
magniFile.print("," + String.valueOf(magniValueX) + ","
+ String.valueOf(magniValueY) + ","
+ String.valueOf(magniValueZ));
magniFile.println();
}
}
magnitimestamp = event.timestamp;
magniX.setText("X-axis: " + String.valueOf(magniValueX));
magniY.setText("Y-axis: " + String.valueOf(magniValueY));
magniZ.setText("Z-axis: " + String.valueOf(magniValueZ));
magnDelayValue.setText("Magnitic Delay: "
+ String.valueOf(magnidT * NS2MS) + " ms");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
magnitimestamp = 0;
magniLogFileTimestamp = 0;
}
};
我做错了。有什么建议么?谢谢你。