简体   繁体   中英

android :: show progress dialog till asynctask does not get complete

I am downloading a zip file of 15 mb and then unzip it in the sd card. I am using progress dialog to show the status. First time it works perfectly, and when I change the db version number on server to download new file and start the app again then progress dialog disappears in between and causes crash in the app.

Below is the code.

class CheckInAppUpdatesAsyncTask extends AsyncTask<Void, Void, Void> {

    Dialog  progress;


    @Override
    protected Void doInBackground(Void... params) {

        try {
            downloadDB();

        } catch (Exception e) {
        } 
     }
@Override
    protected void onPostExecute(final Void result) {

        stopWorking();
    }

    @Override
    protected void onPreExecute() {
         startWorking();
    }
  };5


private void startWorking() {

    synchronized (this.diagSynch) {
        if (this.pDiag != null) {
            this.pDiag.dismiss();
        }

        this.pDiag = ProgressDialog.show(Browse.context, "Working...",
                "Please wait while we load the encyclopedia.", true, false);
    }
}


   private void stopWorking() {

    synchronized (this.diagSynch) {
        if (this.pDiag != null) {
            this.pDiag.dismiss();
        }
    }
}

Download code

URL url = new URL(serverFileURL);
        Log.d("FILE_URLLINK", "serverFileURL " + serverFileURL);
        URLConnection connection = url.openConnection();
        InputStream input = connection.getInputStream();
        connection.getContentLength();

        byte data[] = new byte[1024];
        input = new GZIPInputStream(input);
        InputSource is = new InputSource(input);
        InputStream in = new BufferedInputStream(is.getByteStream());
        String inAppDBName = Constants.NEW_DB_NAME_TO_DOWNLOAD;
         OutputStream output = new BufferedOutputStream(new FileOutputStream(dir + "/" + inAppDBName));

        int length;
        while ((length = in.read(data)) > 0) {
            output.write(data, 0, length);
        }

        output.flush();
        output.close();
        input.close();

Any idea?

You should put unzip code before stopWorking(); in onPostExecute.

It will not close till all things happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM