【问题标题】:android :: show progress dialog till asynctask does not get completeandroid :: 显示进度对话框,直到 asynctask 没有完成
【发布时间】:2015-06-25 13:30:33
【问题描述】:

我正在下载一个 15 mb 的 zip 文件,然后将其解压缩到 sd 卡中。 我正在使用进度对话框来显示状态。第一次完美运行,当我更改服务器上的数据库版本号以下载新文件并再次启动应用程序时,进度对话框在两者之间消失并导致应用程序崩溃。

下面是代码。

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

下载代码

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

有什么想法吗?

【问题讨论】:

  • 请分享错误日志。
  • 您的代码看起来不错。与您的代码共享错误。如果可能的话,downloadDB(); 代码也可以。但同样,您已经通过将异常放入 try - catch 来处理异常。
  • 用下载代码更新问题..

标签: android android-asynctask progressdialog


【解决方案1】:

你应该在 stopWorking(); 之前加上解压码在 onPostExecute 中。

在所有事情发生之前它不会关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2019-12-31
    • 2018-04-15
    相关资源
    最近更新 更多