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 SecondActivity 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;
// static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
// static final long MINIMUM_TIME_BETWEEN_UPDATES = 100; // in Milliseconds
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);
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
// MINIMUM_TIME_BETWEEN_UPDATES,
// MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
}
public void initializeViews() {
currentX = (TextView) findViewById(R.id.currentX);
currentY = (TextView) findViewById(R.id.currentY);
currentZ = (TextView) findViewById(R.id.currentZ);
locationx = (TextView) findViewById(R.id.locationx);
locationy = (TextView) findViewById(R.id.locationy);
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) {
// TODO Auto-generated method stub
//Accelerometer acc = new Accelerometer();
float lastX = 0, lastY = 0, lastZ = 0;
// display the current x,y,z accelerometer values
// acc.setxAxis(Math.abs(lastX - event.values[0]));
//acc.setyAxis(Math.abs(lastY - event.values[1]));
// acc.setzAxis(Math.abs(lastZ - event.values[2]));
deltaX = (Math.abs(lastX - event.values[0]));
deltaY = (Math.abs(lastY - event.values[1]));
deltaZ = (Math.abs(lastZ - event.values[2]));
//Location location = locationManager
// .getLastKnownLocation(LocationManager.GPS_PROVIDER);
//GPS test = new GPS();
//test.setLocationX(location.getLatitude());
//test.setLocationY(location.getLongitude());
//X = location.getLongitude();
//Y = location.getLatitude();
displayCurrentValues();
//String xAxis = Float.toString(deltaX);
//String yAxis = Float.toString(deltaY);
//String zAxis = Float.toString(deltaZ);
//String locationX = Double.toString(X);
//String locationY = Double.toString(Y);
// 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://ift.tt/1ePGMF2",
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");
}
}
public void location() {
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
X = location.getLongitude();
Y = location.getLatitude();
}
/*
* public class MyLocationListener implements LocationListener {
*
* @Override public void onLocationChanged(Location location) {
*
* GPS test = new GPS(); test.setLocationX(location.getLatitude());
* test.setLocationY(location.getLongitude());
*
* X = location.getLongitude(); Y = location.getLatitude(); }
*
* @Override public void onProviderDisabled(String provider) { // TODO
* Auto-generated method stub
*
* }
*
* @Override public void onProviderEnabled(String provider) { // TODO
* Auto-generated method stub
*
* }
*
* @Override public void onStatusChanged(String provider, int status, Bundle
* extras) { // TODO Auto-generated method stub
*
* }
*
* }
*/
public void displayCurrentValues() {
currentX.setText(Float.toString(deltaX));
currentY.setText(Float.toString(deltaY));
currentZ.setText(Float.toString(deltaZ));
//locationx.setText(Double.toString(X));
//locationy.setText(Double.toString(Y));
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);
//String locationX = Double.toString(X);
//String locationY = Double.toString(Y);
// 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://ift.tt/1ePGMF2",
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");
}
}
}
No comments:
Post a Comment