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