android - 无法构建来自 AsyncTask 的进度对话框的更新进度

标签 android eclipse android-asynctask download

我无法构建我的项目,因为 eclipse 给出错误

这里是开始下载的按钮点击事件

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ProgressDialog mProgressDialog;
        mProgressDialog = new ProgressDialog(Test.this);
        mProgressDialog.setMessage("Pobieranie");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute("http://google.com");
    }
});

和AsyncTask函数

private class DownloadFile extends AsyncTask<String, Integer, String> {
    [...]
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show(); // Here eclipse tell that "mProgressDialog cannot be resolved"
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]); // Here eclipse tell that "mProgressDialog cannot be resolved"
    }
}

那么我应该在哪里插入创建我的进度对话框的代码?

最佳答案

 private class DownloadFile extends AsyncTask<String, Integer, String> {
    ProgressDialog  mProgressDialog;
    String fileName=null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create ProgressBar
         mProgressDialog = new ProgressDialog(Task.this);
        // Set your ProgressBar Title
        mProgressDialog.setTitle("Downloads");
        mProgressDialog.setIcon(R.drawable.dwnload);
        // Set your ProgressBar Message
        mProgressDialog.setMessage("Updating App Version, Please Wait!");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // Show ProgressBar
        mProgressDialog.setCancelable(false);
      //  mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... sUrl) {
        try {
                    String apkurl = "http://google.com/"+fileName;
                    URL url = new URL(apkurl);
                    HttpURLConnection c = (HttpURLConnection) url
                            .openConnection();
                    c.setRequestMethod("GET");
                    c.setDoOutput(true);
                    c.connect();
                    int fileLength = c.getContentLength();    

                    //File file = new File(PATH);
                    if(file.exists())
                    {
                        file.delete();
                    }
                    file.mkdirs();
                    File outputFile = new File(file, fileName);
        final FileOutputStream fos = new FileOutputStream(outputFile);

                    final InputStream is = c.getInputStream();


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

            // Close connection
            fos.flush();
            fos.close();
            is.close();
            }
    }

    catch (Exception e) {
            // Error Log
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
 }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        mProgressDialog.dismiss();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // Update the ProgressBar
        mProgressDialog.setProgress(progress[0]);
    }
}

关于android - 无法构建来自 AsyncTask 的进度对话框的更新进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17830963/

相关文章:

java - JVM 的 Eclipse 启动错误已终止。升级 Ubuntu 更新后退出代码=13

android - EventBus 在同一 fragment 中发布和订阅

android - Recycler View 添加删除项目动画未显示在 swapCursor 中

java - 如何在 Android 手机唤醒时可靠地检测互联网连接

android - 显示完整 Android 应用程序的计时器。

java - 如何清除 eclipse 中的虚假错误?

java - Android - 使用push()插入多个标记

eclipse - Java Eclipse 中的排序和排序库

java - AsyncTask 未正确运行

java - doInBackground 需要很长时间才能启动