我有一个 AsyncTask
,它在我的应用程序退出之前执行。它获取位置并解析我的布局 xml 文件。已检索到位置,但未执行解析。
在我的 Main Activity
中调用 AsyncTask
:
public void quitApplication()
{
FinishProcess fProcess = new FinishProcess();
fProcess.execute(this);
}
异步任务
:
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();
}
}
}
最佳答案
如果您的应用程序在启动此进程后退出,那么主线程可能正在死亡,而后台线程 (AsyncTask
) 正在运行,从而使其成为孤儿。如果需要在关闭期间完成此操作,请尝试在没有 AsyncTask
线程的情况下在 Application.onTerminate
中执行此操作。
关于android - AsyncTask 中的代码未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8871560/