如何使用 AsyncTask 类更新将文件复制到另一个目录的进度?

发布于 2024-11-08 04:59:36 字数 116 浏览 3 评论 0原文

我应该如何使用 AsyncTask 类和进度条来执行将文件复制到手机 SD 卡本地上下文中的另一个目录的过程?我在[这里][1]看到了一个类似的例子,但我不知道如何合并差异/修改代码的上下文以适应我的上下文以使其工作?

How should I be using AsyncTask class coupled with a progress bar to perform the copying process of a file to another directory in the local context of the phone sdcard? I have seen a similar example [here][1], but I have no idea how to incorporate the differences/modify the context of the code to suit my context to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-11-15 04:59:36

例如

// Params are input and output files, progress in Long size of 
// data transferred, Result is Boolean success.
public class MyTask extends AsyncTask<File,Long,Boolean> {
   ProgressDialog progress; 

  @Override
  protected void onPreExecute() {
    progress = ProgressDialog.show(ctx,"","Loading...",true);
  }

  @Override
  protected Boolean doInBackground(File... files) {
    copyFiles(files[0],files[1]);
    return true;
  }

  @Override
  protected void onPostExecute(Boolean success) {
    progress.dismiss();
    // Show dialog with result
  }

  @Override
  protected void onProgressUpdate(Long... values) {
    progress.setMessage("Transferred " + values[0] + " bytes");
  }
}

,现在,在 copyFiles 中,您必须调用publishProgress() 并传递传输的数据大小。请注意,进度通用参数为 Long。您可以使用 commons-io 中的 CountingInputStream 包装器来实现此目的。

还有许多其他事情需要顶层处理,但简而言之就是这样。

开始:

  MyTask task = new MyTask();
  task.execute(src,dest);

It would be something like

// Params are input and output files, progress in Long size of 
// data transferred, Result is Boolean success.
public class MyTask extends AsyncTask<File,Long,Boolean> {
   ProgressDialog progress; 

  @Override
  protected void onPreExecute() {
    progress = ProgressDialog.show(ctx,"","Loading...",true);
  }

  @Override
  protected Boolean doInBackground(File... files) {
    copyFiles(files[0],files[1]);
    return true;
  }

  @Override
  protected void onPostExecute(Boolean success) {
    progress.dismiss();
    // Show dialog with result
  }

  @Override
  protected void onProgressUpdate(Long... values) {
    progress.setMessage("Transferred " + values[0] + " bytes");
  }
}

Now, inside copyFiles you will have to call publishProgress() with size of data transferred, for example. Note that progress generic parameter is Long. You can use CountingInputStream wrapper from commons-io for that.

There are number of additional things top take care of, but in the nutshell that is it.

To start:

  MyTask task = new MyTask();
  task.execute(src,dest);
水中月 2024-11-15 04:59:36

尝试使用异步任务,如下所示:

 try{
    class test extends AsyncTask{


         TextView tv_per;
         int mprogress;

        Dialog UpdateDialog = new Dialog(ClassContext);

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            mprogress = 0;

            UpdateDialog.setTitle(getResources().getString(R.string.app_name));
            UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
            TextView dialog_message =  (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
            dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
            dialog_message.setGravity(Gravity.RIGHT);
            UpdateDialog.setCancelable(false);
            UpdateDialog.show();
            super.onPreExecute();
        }



        @Override
        protected void onProgressUpdate(Object... values) {
            // TODO Auto-generated method stub
            ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
            update.setProgress((Integer) values[0]);
            int percent =  (Integer) values[0];
            if(percent>=100)
            {
                percent=100;
            }
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
             tv_per.setText(""+percent);
        }



        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            //your code
}

            super.onPostExecute(result);
            UpdateDialog.dismiss();
        }

     }
     new test().execute(null);

 }
 catch(Exception e)
 {
     e.printStackTrace();
 }

Try using Async task as shown below:

 try{
    class test extends AsyncTask{


         TextView tv_per;
         int mprogress;

        Dialog UpdateDialog = new Dialog(ClassContext);

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            mprogress = 0;

            UpdateDialog.setTitle(getResources().getString(R.string.app_name));
            UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
            TextView dialog_message =  (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
            dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
            dialog_message.setGravity(Gravity.RIGHT);
            UpdateDialog.setCancelable(false);
            UpdateDialog.show();
            super.onPreExecute();
        }



        @Override
        protected void onProgressUpdate(Object... values) {
            // TODO Auto-generated method stub
            ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
            update.setProgress((Integer) values[0]);
            int percent =  (Integer) values[0];
            if(percent>=100)
            {
                percent=100;
            }
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
             tv_per.setText(""+percent);
        }



        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            //your code
}

            super.onPostExecute(result);
            UpdateDialog.dismiss();
        }

     }
     new test().execute(null);

 }
 catch(Exception e)
 {
     e.printStackTrace();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文