I currently have created a function that uses XmlSerializer to create an XML file from objects. I have been researching about using different forms of multi threading in order to save the file in the background whilst the GUI is still usable and is still updating. I looked at using AsyncTask to do this, but am not to sure on what is the best way to implement it. Please can anyone help me with this and thank you in advance.
Here is the code that I have so far:
private String fileName;
private DataObjects dataObjects;
public SetCachedValuesFile()
{
}
public void setFileName(String refFileName)
{
fileName = refFileName;
}
public void setDataObjects(DataObjects refDataObjects)
{
dataObjects = refDataObjects;
}
public String getFileName()
{
return fileName;
}
public DataObjects getDataObjects()
{
return dataObjects;
}
public void updateValues()
{
ArrayList<DataObject> arrayListDataObject = dataObjects.getDataObjects();
try
{
/* Creates a new file and its directory. */
File directory = new File(Environment.getExternalStorageDirectory() + "/XML_FILES/");
directory.mkdirs();
File newFile = new File(directory, fileName + ".xml");
FileOutputStream fos = new FileOutputStream(newFile);
/* Creates a new XML serializer which creates the structure of the XML file. */
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, true);
serializer.startTag("", "CachedValues");
for(DataObject dataObject : arrayListDataObject)
{
if(dataObject.getClass().equals(StringDataObject.class))
{
StringDataObject stringDataObject = (StringDataObject) dataObject;
String address = HexFunctions.toString(stringDataObject.getAddress());
String value = stringDataObject.getValue();
serializer.startTag("", "DataObject");
serializer.startTag("", "Address");
serializer.text(address);
serializer.endTag("", "Address");
serializer.startTag("", "Value");
serializer.text(value);
serializer.endTag("", "Value");
serializer.endTag("", "DataObject");
System.out.println("String data object added to file.");
}
else if(dataObject.getClass().equals(IntDataObject.class))
{
IntDataObject intDataObject = (IntDataObject) dataObject;
String address = HexFunctions.toString(intDataObject.getAddress());
String value = Integer.toString(intDataObject.getValue());
serializer.startTag("", "DataObject");
serializer.startTag("", "Address");
serializer.text(address);
serializer.endTag("", "Address");
serializer.startTag("", "Value");
serializer.text(value);
serializer.endTag("", "Value");
serializer.endTag("", "DataObject");
System.out.println("Int data object added to file.");
}
else if(dataObject.getClass().equals(FloatDataObject.class))
{
FloatDataObject floatDataObject = (FloatDataObject) dataObject;
String address = HexFunctions.toString(floatDataObject.getAddress());
String value = Float.toString(floatDataObject.getValue());
serializer.startTag("", "DataObject");
serializer.startTag("", "Address");
serializer.text(address);
serializer.endTag("", "Address");
serializer.startTag("", "Value");
serializer.text(value);
serializer.endTag("", "Value");
serializer.endTag("", "DataObject");
System.out.println("Float data object added to file.");
}
else if(dataObject.getClass().equals(DoubleDataObject.class))
{
DoubleDataObject doubleDataObject = (DoubleDataObject) dataObject;
String address = HexFunctions.toString(doubleDataObject.getAddress());
String value = Double.toString(doubleDataObject.getValue());
serializer.startTag("", "DataObject");
serializer.startTag("", "Address");
serializer.text(address);
serializer.endTag("", "Address");
serializer.startTag("", "Value");
serializer.text(value);
serializer.endTag("", "Value");
serializer.endTag("", "DataObject");
System.out.println("Double data object added to file.");
}
}
serializer.endTag("", "CachedValues");
serializer.endDocument();
serializer.flush();
fos.close();
System.out.println("File created");
System.out.println("File name: " + newFile.getAbsolutePath());
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}