Hey:) I am trying to pasre to accelerometer data into an xml file when the start button is clicked but the problem is it only parses me the first accelerometer data so not the others or if i write it like the code down here it parse 1000 times the same value
Thank you
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");
}
}
}