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

时间:2015-01-24 19:17:09

标签: android xml button accelerometer acceleration

嘿:)当点击开始按钮时,我试图将加速度计数据传递到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 个答案:

答案 0 :(得分:0)

你需要制作一个循环。现在,您的代码只需调用&#34;解析&#34;曾经,这就是为什么它解析相同的值1000次。进行多项操作:

  
      
  1. 打开XML文件的操作

  2.   
  3. 关闭XML文件的操作

  4.   
  5. 将值添加到XML文件

  6. 的操作   

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