从Android中的另一个方法调用publishProgress

时间:2016-03-14 10:34:16

标签: android

我正在尝试从Android服务器下载文件并更新进度..

在后台的Async方法我正在调用一个方法,将文件下载到我的SD卡现在我想更新进度因此尝试publishProgress(“”+ progressvalue);我无法从这种方法访问。如何访问publishProgress?

以下是我的尝试:

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

        @Override
        protected Void doInBackground(String... param) {
            downloadFileToSdCard(param[0], "File" + counter + ".mp4");
            return null;
        }

        protected void onProgressUpdate(String... values)
        {
            Log.d("ANDRO_ASYNC", values[0]);
        }
}

downloadFileToSdCard方法如下所示:

private void downloadImagesToSdCard(String downloadUrl, String fileName)]
{
        FileOutputStream fos;
        InputStream inputStream = null;

        try {
            URL url = new URL(downloadUrl);
            String sdCard = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(sdCard, "AppDownload");
            if (!myDir.exists()) {
                myDir.mkdir();
            }

            String fname = fileName;
            File file = new File(myDir, fname);
            if (file.exists())
                file.delete();

            URLConnection ucon = url.openConnection();
            final String contentLengthStr=ucon.getHeaderField("content-length");

            int lenghtOfFile = Integer.valueOf(contentLengthStr);

            HttpURLConnection httpConn = (HttpURLConnection) ucon;
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            inputStream = httpConn.getInputStream();

            fos = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            long total = 0;
            int progress = 0;
            while ((bufferLength = inputStream.read(buffer)) > 0)
            {

                total += bufferLength;
                int progress_temp = (int) total * 100 / lenghtOfFile;
                publishProgress("" + progress_temp);
                if (progress_temp % 10 == 0 && progress != progress_temp)
                {
                    progress = progress_temp;
                }
                fos.write(buffer, 0, bufferLength);
                int downloadedSize = Integer.valueOf(contentLengthStr);
            }
            inputStream.close();
            fos.close();
            Log.e("Value", "File Saved in sdcard..");
        } catch (IOException io) {
            inputStream = null;
            fos = null;
            io.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

有人可以帮我解决这个问题吗?谢谢!

2 个答案:

答案 0 :(得分:1)

Triy this:

让你在AsyncTask Class中保持功能

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

            @Override
            protected Void doInBackground(String... param) {
                downloadFileToSdCard(param[0], "File" + counter + ".mp4");
                return null;
            }

            protected void onProgressUpdate(String... values)
            {
                Log.d("ANDRO_ASYNC", values[0]);
            }

            private void downloadImagesToSdCard(String downloadUrl, String fileName)
            { 
               //Publish Progress calling code
            }
}

答案 1 :(得分:0)

第一件事就是简单地将AsyncTask传递给你的sdcard方法。 AsyncTask.publishProgress受到保护,因此只要您的两个类在同一个包中,您就应该能够从sdcard下载的方法中调用publishProgress。

更清洁/更漂亮的方法是定义一个回调接口,例如ProgressUpdater,让AsyncTask实现它,并让sdcard方法将ProgressUpdater作为参数。此接口通常具有类似甚至相等的publishProgress方法。但是,这会将AsyncTask本身与程序的其余部分分离。