如何指定在循环内x秒后调用的publishProgress()?

时间:2014-02-19 13:15:14

标签: java android

由于垃圾收集器被多次调用,我一直在寻找一种在循环语句中在x秒后调用publishProgress()的方法。

这就是我所拥有的:

protected Void doInBackground(Void... parms) {

    Long size = source.length();

    InputStream input = new FileInputStream(source);
    OutputStream output = new FileOutputStream(destination);

    // Transfer bytes from input to output
    byte[] buf = new byte[1024];

    int len;

    long written = 0;

    while ((len = input.read(buf)) > 0)
    {
        output.write(buf, 0, len);

        written += 1024;

        //This should be called after x seconds
        publishProgress((int) (written * 100 / size));
    }

    input.close();
    output.close();
}

我找到ScheduledExecutorService但我不知道如何在循环中实现它。

3 个答案:

答案 0 :(得分:2)

将此行放入循环

 handler.postDelayed(runnable, x);

并在您的活动中

Runnable runnable = new Runnable() {

    @Override
    public void run() {

        publishProgress((int) (written * 100 / size));

    }
};


Handler handler = new Handler();

答案 1 :(得分:2)

此解决方案不会阻止UI线程,因为它使用AsyncTask

  Long size = source.length();
        InputStream input = new FileInputStream(source);
        OutputStream output = new FileOutputStream(destination);

            // Transfer bytes from input to output
            byte[] buf = new byte[1024];

            int len;

            long written = 0;

            while ((len = input.read(buf)) > 0) {
                output.write(buf, 0, len);

                written += 1024;

                Long size = source.length();

                InputStream input = new FileInputStream(source);
                OutputStream output = new FileOutputStream(destination);

                // Transfer bytes from input to output
                byte[] buf = new byte[1024];

                int len;

                long written = 0;

                while ((len = input.read(buf)) > 0) {
                    output.write(buf, 0, len);

                    written += 1024;

                    (new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... parms) {
                            mEventDataSource.deleteAll();
                            try {
                                Thread.currentThread().sleep(x * 1000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                            // This should be called after x seconds
                            publishProgress((int) (written * 100 / size));
                        }
                    }).execute();

                }

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

答案 2 :(得分:0)

您可以按如下方式使用CountdownTimer课程,

while ((len = input.read(buf)) > 0)
{
    output.write(buf, 0, len);

    written += 1024;

    // will call publicshProgress after 30 seconds.
    new CountDownTimer(30000, 1000) 
    {
         public void onTick(long millisUntilFinished) {}

         public void onFinish() 
         {
                 //This should be called after x seconds
             publishProgress((int) (written * 100 / size));
          }
      }.start();
}