1
我有一個AsyncTask
,它在我的應用程序退出前執行。它獲取位置並解析我的佈局xml文件。該位置被檢索,但解析沒有得到執行。AsyncTask中的代碼未執行
調用AsyncTask
在我Main Activity
:
public void quitApplication()
{
FinishProcess fProcess = new FinishProcess();
fProcess.execute(this);
}
AsyncTask
:
public class FinishProcess extends AsyncTask<Main, Void, Void>
{
@Override
protected Void doInBackground(Main... params) {
LocationHandler lh = new LocationHandler();
try {
lh.getLocationSingle(null, params[0]);
} catch (InterruptedException e) {
e.printStackTrace();
}
parseXML(params[0]);
return null;
}
private void parseXML(Main params)
{
String ANDROID_ID = "android:id";
Resources resources = params.getResources();
try {
InputStream fXmlFile = resources.openRawResource(R.raw.pages);
//Reads xml file and gets the node types and attributes
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("*");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println(eElement.getNodeName());
}
}
}
catch (Exception e) {
System.out.println("Catch");
e.printStackTrace();
}
}
}
使用Application.onTerminate你只是不能確定它會被調用。 – Orest 2012-01-15 17:58:47
你是對的。對不起,我忽略了。實際上,似乎'onTerminate'只在模擬器中調用,從不在生產設備上調用。 – 2012-01-15 18:03:33
這就是說。現在這個異步任務在哪裏?您可以嘗試從相同位置減去後臺線程調用'parsexml'。那個肯定是個問題。 – 2012-01-15 18:04:38