0

I have two AsyncTasks, one is used to download xml file (DownloadTask), another is for parsing the file (ParseXMLTask). There are two cases of using this tasks:

1) File doesn't exists > execute DownloadTask, onPostExecute > ParseXMLTask

2) File exists > execute only ParseXMLTask.

Everything is working, but the thing is, while performing the second case, there is a blocking the ui about 3 sec (black screen) that surely would make a user annoyed. This is absolutely confusing me, because the job in the second case seems to be easier.

So when I am testing my app, a situation is like that: I click on the button for the first time, file is being downloaded, saved on the sd card, parsed and finally opened. Then I go back and click on the button again. Now I see that lag while switching between activities.

Code:

Executing the tasks

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]);
    }
}
}
sonderlain
  • 312
  • 2
  • 15
  • How have you come to the conclusion it is blocking the UI? What do you expect the screen to be doing? – Blundell May 12 '13 at 15:31
  • During the execution of tasks I want to see my layout with the progress bar. But I can see the layout only about 1ms, than the black screen, and then the activity is called. – sonderlain May 12 '13 at 15:41
  • I do not see any onPreExecute() function in your DownloadFile class, which I believe that is where your progressBar should have been called `show()`. Are you initializing it before downloadPack() is called? If yes, can you show us also that part of code? – Chor Wai Chun May 13 '13 at 03:32
  • `loadingBar = (ProgressBar) findViewById(R.id.loadingBar);` before the `downloadPack()` method. – sonderlain May 13 '13 at 05:15

1 Answers1

0

There is nothing wrong with the above.

The parsing is pretty fast that is why you only see the layout for a split second.

The black screen will be the PackActivity loading, check this activity for what is blocking the UI thread.

You could have also put LogCat messages in to show that the parsing has finished and onCreate of the next Activity is called.

Blundell
  • 75,855
  • 30
  • 208
  • 233