AsyncTask中的代码未执行

时间:2012-01-15 17:04:40

标签: android android-activity android-asynctask execution

我有一个AsyncTask,它在我的应用程序退出之前执行。它获取位置并解析我的布局xml文件。检索位置但解析不会执行。

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();
        }

    }


}

1 个答案:

答案 0 :(得分:1)

如果您的应用程序在启动此过程后退出,那么主要线程可能正在死亡,而后台之一(AsyncTask)正在运行,从而孤立它。如果需要在关机期间完成此操作,请尝试在Application.onTerminate中执行此操作而不使用AsyncTask主题。