编写文本复制以前的数据

时间:2016-08-31 22:04:06

标签: java

所以我(可能是微不足道的)一个我无法解决的问题。所以我正在使用Android(传感器等standard libraries)设备收集一些传感器数据,这个设备就像这样(非常标准):

private final SensorEventListener MultiSensorListener
            = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

    // Printing of data to screen happens here. File will be written here async.
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
            String sensorReading = "\nLight Reading: " + String.valueOf(event.values[0]);
            side_output.append(sensorReading);                
        }
        //previous code, let's assume that both or any one has been done
        if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
            //textLIGHT_reading.setText("LIGHT: " + event.values[0]);
            side_output.append("\nProximity Reading: " + String.valueOf(event.values[0]));
        }
        if()...

        // Many more can go
    }
};

所以我想要的是:首先,在event.values[index]被触发时,首先得到所有传感器值onSensorChanged(SensorEvent event),然后创建一个string表(csv,可能存储在静态变量中) )。

对于下面的示例,我们假设第一个是Acclerometer,然后是Gyro最后Ambient Light等。即:

+--------------------+--------------------+--------------------+
| event.values[0]    | event.values[0]    | event.values[0]    |
+--------------------+--------------------+--------------------+
| event.values[new]  | copy from previous | copy from previous |
+--------------------+--------------------+--------------------+
| copy from previous | event.values[new]  | copy from previous |
+--------------------+--------------------+--------------------+

这里发生的是:

  1. 使用第一个读取值填充第一行
  2. 每次onSensorChanged(SensorEvent event)更新该特定传感器的行,并复制以前传感器中未更改的值。
  3. 如何做到这一点?我尝试使用+=进行字符串操作,但事实是数据量巨大,处理成为一个大问题,我真的不希望我的数据收集速度慢!

    谢谢和问候。

1 个答案:

答案 0 :(得分:1)

您可以向传感器侦听器或使用匿名类实例化的任何其他对象添加方法,然后在逻辑中调用这些日志记录方法:

private final SensorEventListener MultiSensorListener
            = new SensorEventListener() {

    // the overriden methods here ...

    private Map<String, String> map = new HashMap<>();

    private void logHistory(String key, String value) {
        // ...
        // for example:
        map.put(key, value);
    }

    private Map<String, String> getLoggedHistory() {
        Map<String, String> mapCopy = new HashMap<>();
        mapCopy.putAll(map);
        return mapCopy;
    }
    ...
    // Printing of data to screen happens here. File will be written here async.
    @Override
    public void onSensorChanged(SensorEvent event) {
       ...
       // log event:
       map.put(event.getId(), event); // just an example
    }