0

2 つの AsyncTasks があります。1 つは xml ファイルのダウンロード (DownloadTask) に使用され、もう 1 つはファイルの解析 (ParseXMLTask) に使用されます。このタスクを使用するケースは 2 つあります。

1) ファイルが存在しない > DownloadTask を実行、onPostExecute > ParseXMLTask

2) ファイルが存在する > ParseXMLTask のみを実行する。

すべてが機能していますが、問題は、2 番目のケースを実行しているときに、約 3 秒間 UI がブロックされ (黒い画面)、ユーザーを確実にイライラさせることです。2 番目のケースの仕事の方が簡単に見えるので、これは私を完全に混乱させます。

したがって、アプリをテストしているときの状況は次のようなものです。ボタンを初めてクリックすると、ファイルがダウンロードされ、SD カードに保存され、解析され、最後に開かれます。次に、戻ってボタンをもう一度クリックします。アクティビティを切り替える際にラグが発生するようになりました。

コード:

タスクの実行

private void downloadPack() {
    if (packDownloaded) {
        parseXML();
    } else {    
        download = new DownloadFile(fileName, this, loadingBar);
        download.execute(serverURL + fileName + ".xml");
    }
}

private void parseXML() {
    ParseXMLTask parseTask = new ParseXMLTask(this, this);
    parseTask.execute(PATH + fileName + ".xml");
}

public void postDownload(File result) {
        parseXML();     
}   

public void postParse() {               
    Intent packIntent = new Intent(this, PackActivity.class);
    startActivity(packIntent);      
}

ParseXMLTask.java

public class ParseXMLTask extends AsyncTask<String, Integer, Void> {    

private Context context;
private XmlPullParser xpp;
private IPostParse iPostParse;

public ParseXMLTask(Context context, IPostParse iPostParse) {
    this.context = context;
    this.iPostParse = iPostParse;

}

@Override
protected Void doInBackground(String... params) {               
    File file = new File(params[0]);

    /* doing the job */
}

@Override
protected void onPostExecute(Intent result) {
    iPostParse.postParse(result);
}
}

DownloadFile.java

public class DownloadFile extends AsyncTask<String, Integer, File> {

private static final String PATH = Environment
        .getExternalStorageDirectory().getPath() + "/.chgkgame/";;
private File dir;
private ProgressBar progressBar;
private String fileName;
private IPostDownload postDownload;
private boolean download;


public DownloadFile(String name, IPostDownload pDownload, ProgressBar pBar) {
    progressBar = pBar;
    fileName = name;
    postDownload = pDownload;
}


@Override
protected File doInBackground(String... sUrl) {
    URL url;
    try {
        url = new URL(sUrl[0]);
        URLConnection urlConnection = url.openConnection();
        urlConnection.connect();            
        int fileLength = urlConnection.getContentLength();

        dir = new File(PATH + fileName + ".xml");
        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(dir);

        byte[] data = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int) (total * 100 / fileLength));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return dir;
}

@Override 
protected void onPostExecute(File result) {
    if (postDownload != null) postDownload.postDownload(result);
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);

    if (progressBar != null) {
        progressBar.setProgress(values[0]);
    }
}
}
4

1 に答える 1