Android将加速度计数据解析成xml

Posted

技术标签:

【中文标题】Android将加速度计数据解析成xml【英文标题】:Android parse accelerometer data into xml 【发布时间】:2015-01-24 19:17:08 【问题描述】:

嘿:) 当单击开始按钮时,我试图将加速度计数据解析为 xml 文件,但问题是它只解析我的第一个加速度计数据,而不是其他加速度计数据,或者如果我像下面的代码一样编写它它解析 1000 次相同的值

谢谢

public class Secondet extends Activity implements SensorEventListener 

TextView currentX;

TextView currentY;

TextView currentZ;

TextView locationx;

TextView locationy;

float deltaX = 0;
float deltaY = 0;
float deltaZ = 0;
double X = 0;
double Y = 0;

protected LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity);
    initializeViews();

    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    Sensor accelerometer = sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    sensorManager.registerListener(this, accelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);



public void initializeViews() 

    currentX = (TextView) findViewById(R.id.currentX);
    currentY = (TextView) findViewById(R.id.currentY);
    currentZ = (TextView) findViewById(R.id.currentZ);
    final Button start = (Button) findViewById(R.id.start);



@Override
public void onAccuracyChanged(Sensor arg0, int arg1) 
    // TODO Auto-generated method stub



@Override
public void onSensorChanged(SensorEvent event) 

    float lastX = 0, lastY = 0, lastZ = 0;

    deltaX = (Math.abs(lastX - event.values[0]));
    deltaY = (Math.abs(lastY - event.values[1]));
    deltaZ = (Math.abs(lastZ - event.values[2]));

    displayCurrentValues();


public void location() 

    Location location = locationManager
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    X = location.getLongitude();
    Y = location.getLatitude();



public void displayCurrentValues() 

    currentX.setText(Float.toString(deltaX));
    currentY.setText(Float.toString(deltaY));
    currentZ.setText(Float.toString(deltaZ));
    final Button start = (Button) findViewById(R.id.start);
    start.setOnClickListener(new View.OnClickListener() 
        public void onClick(View v) 
            // Perform action on click
            parse();
        
    );


public void parse() 

    String xAxis = Float.toString(deltaX);
    String yAxis = Float.toString(deltaY);
    String zAxis = Float.toString(deltaZ);


    // Creat XML File
    File newxmlfile = new File(Environment.getExternalStorageDirectory()
            + "/newtest1.xml");
    try 
        newxmlfile.createNewFile();
     catch (IOException e) 
        Log.e("IOException", "exception in createNewFile() method");
    
    // we have to bind the new file with a FileOutputStream
    FileOutputStream fileos = null;
    try 
        fileos = new FileOutputStream(newxmlfile);
     catch (FileNotFoundException e) 
        Log.e("FileNotFoundException", "can't create FileOutputStream");
    

    XmlSerializer serializer = Xml.newSerializer();
    try 
        // we set the FileOutputStream as output for the serializer, using
        // UTF-8 encoding
        serializer.setOutput(fileos, "UTF-8");
        // Write <?xml declaration with encoding (if encoding not null) and
        // standalone flag (if standalone not null)
        serializer.startDocument(null, Boolean.valueOf(true));
        // set indentation option
        serializer.setFeature(
                "http://xmlpull.org/v1/doc/features.html#indent-output",
                true);
        // start a tag called "root"
        serializer.startTag(null, "root");
        // i indent code just to have a view similar to xml-tree
        // int i = 0;
        final Button stop = (Button) findViewById(R.id.Stop);
        stop.setOnClickListener(new View.OnClickListener() 
            public void onClick(View v) 
                // Perform action on click
                int i = 2;
            
        );

        for (int i = 0; i <= 1000; i++) 
            // serializer.startTag(null, "Location");
            // serializer.startTag(null, "Longitude");
            // serializer.text(locationX);
            // serializer.endTag(null, "Longitude");
            // serializer.startTag(null, "Latitude");
            // serializer.text(locationY);
            // serializer.endTag(null, "Latitude");
            // serializer.endTag(null, "Location");
            serializer.startTag(null, "Acceleration");
            serializer.startTag(null, "X-Axis");
            serializer.text(Float.toString(deltaX));
            serializer.endTag(null, "X-Axis");
            serializer.startTag(null, "Y-Axis");
            serializer.text(Float.toString(deltaX));
            serializer.endTag(null, "Y-Axis");
            serializer.startTag(null, "Z-Axis");
            serializer.text(Float.toString(deltaX));
            serializer.endTag(null, "Z-Axis");
            serializer.endTag(null, "Acceleration");

        // while(i == 0);

        serializer.endTag(null, "root");
        serializer.endDocument();
        // write xml data into the FileOutputStream
        serializer.flush();
        // finally we close the file stream
        fileos.close();

        Toast.makeText(getApplicationContext(),
                "file has been created on SD card)", Toast.LENGTH_LONG)
                .show();
     catch (Exception e) 
        Log.e("Exception", "error occurred while creating xml file");
    

【问题讨论】:

【参考方案1】:

你需要做一个循环。现在,您的代码只需调用一次“parse”,这就是它解析相同值 1000 次的原因。进行多项操作:

    打开 XML 文件的操作

    关闭 XML 文件的操作

    将值添加到 XML 文件的操作

按下开始按钮时,初始化 XML 文件。然后,每次调用 onSensorChanged() 时,将值添加到 XML 文件。一旦你有 1000 个值(实现一个简单的计数器),关闭加速度计并调用关闭 XML 文件的操作。 XML 文件需要是全局变量,而不是局部变量,以便所有操作都可以访问它。

【讨论】:

以上是关于Android将加速度计数据解析成xml的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Nike+ 记录加速度计数据?

过滤加速度计数据噪声

存储和比较 android 传感器(加速度计)数据集以匹配模式

Android Freeline加速编译App方案 使用和总结

Android解析Paint类中MaskFilter的使用

Android Freeline加速编译App方案 使用和总结