【问题标题】:using progressbar with AsyncTask [duplicate]使用带有 AsyncTask 的进度条 [重复]
【发布时间】:2015-03-23 12:33:11
【问题描述】:

我实现了从 url 下载文件的代码,效果很好(我正在使用 asynctask 下载文件)。

现在我想在下载中添加进度条,我经过了一些线程,发现我需要实现protected void onPreExecute() & protected void onPostExecute()。我做了第一部分。我怎样才能用我的例子来实现protected void onPostExecute()

我的全部主要活动如下

    public class MainActivity extends ActionBarActivity {
    private ProgressDialog pdia;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public void download(View v)
        {
            Log.i("download...", "download");
            new DownloadFile().execute("http://10.0.2.2/filedemo/file1.pdf", "myfile1.pdf"); 
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class DownloadFile extends AsyncTask<String, Void, Void>{

        @Override
        protected void onPreExecute(){ 
           super.onPreExecute();
                    pdia = new ProgressDialog(getApplicationContext());
                    pdia.setMessage("Downloading...");
                    pdia.show(); 

        }   

        @Override
        protected Void doInBackground(String... strings) {
            String fileUrl = strings[0];   // -> url to file
            String fileName = strings[1];  // -> myfile.pdf
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "myapplication");
            folder.mkdir();

            File pdfFile = new File(folder, fileName);

            try{
                pdfFile.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
            FileDownloader.downloadFile(fileUrl, pdfFile);
            return null;
        }

        protected void onPostExecute(Void result) {
            //called on ui thread
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (pdia != null) {
                        pdia.dismiss();
                    }
                }
            });


            }  
    }   
 }

谢谢...

【问题讨论】:

  • 当前代码有什么问题?
  • @ρяσѕρєяK 问题不存在,但我不知道如何添加它
  • 如果你真的想要ProgressBar,那么你很可能想在doInBackground() 中使用publishProgress() 并实现onProgressUdpate()
  • @codeMagic 检查与此相关的新 so 线程。

标签: android file android-asynctask download


【解决方案1】:

您从doInBackground 返回的结果将传递给onPostExecute。由于您返回null,您可以在 onPostExecute 中关闭对话框:

protected void onPostExecute(Void result) {
//called on ui thread
            if (pdia != null) {
                pdia.dismiss();
            }

}       

【讨论】:

  • 我应该使用 runonuithread() 方法还是其他任何东西,对 asynctask 有点陌生
  • 没有。你不需要。 onPostExecute 在 UI 线程上调用,因此您不需要使用 runonuithread()。我添加了评论以帮助您
【解决方案2】:

您可以查看 here 一个 API 以使用进度条,here 是一个很好的例子。

【讨论】:

  • 已经检查过了,但是那里的例子使用了处理程序
  • 对不起...我更改了答案中的示例。试试这个例子……很简单。
猜你喜欢
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多