如何使用接口

时间:2017-06-23 07:22:29

标签: android android-asynctask progress-bar

我已经创建了一个App,其中我已经使用了Fragment类和另一个Async Class来下载两个不同的类现在我必须显示进度条水平更新进度条onProgressUpdate下载了多少。但onProgressUpdate我回调的接口不是wrking而且它没有更新ProgressBar。

FragmentA.class

private void downloadAndUnzipContent(String url){
    progressBar.setVisibility(View.VISIBLE);
    progressBar.setProgress(0);
    DownloadFileAsync download = new DownloadFileAsync(path+"content.zip", mContext,
            new DownloadFileAsync.PostDownload(){
        @Override
        public void downloadDone(File file) {
            Log.i(TAG, "file download completed");
        }
    }, new DownloadFileAsync.ProgressUpdate() {

        @Override
        public void ProgressUpdate(int progress) {
            progressBar.setProgress(progress);
        }

    });
    download.execute(url);
}

在DownloadFileAsync.class中我可以下载文件

public class DownloadFileAsync extends AsyncTask<String, Integer, String> {

private static final String TAG = "DOWNLOADFILE";

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private PostDownload callback;
private ProgressUpdate callProgress;
private Context context;
private FileDescriptor fd;
private File file;
private String downloadLocation;

public DownloadFileAsync(String downloadLocation, Context context, PostDownload callback, ProgressUpdate callProgress) {
    this.context = context;
    this.callback = callback;
    this.callProgress = callProgress;
    this.downloadLocation = downloadLocation;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(String... aurl) {
    int count;

    try {
        URL url = new URL(aurl[0]);
        URLConnection connection = url.openConnection();
        connection.connect();

        int lenghtOfFile = connection.getContentLength();
        Log.d(TAG, "Length of the file: " + lenghtOfFile);

        InputStream input = new BufferedInputStream(url.openStream());
        file = new File(downloadLocation);
        FileOutputStream output = new FileOutputStream(file); //context.openFileOutput("content.zip", Context.MODE_PRIVATE);
        Log.d(TAG, "file saved at " + file.getAbsolutePath());
        fd = output.getFD();

        byte data[] = new byte[1024];
        long total = 0;
        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress((int) ((total * 100) / lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }
    return null;

}

protected void onProgressUpdate(Integer... progress) {
    //Log.d(TAG,progress[0]);
    if(callProgress != null)
        callProgress.ProgressUpdate(progress[0]);
}

@Override
protected void onPostExecute(String unused) {
    //progressBar.setVisibility(View.GONE);
    if (callback != null) callback.downloadDone(file);
}

public  interface PostDownload {
    void downloadDone(File fd);
}

public interface ProgressUpdate {
    void ProgressUpdate(int progress);
}
}

请帮助我,我在代码中犯了错误

提前完成

2 个答案:

答案 0 :(得分:0)

您的代码没问题。 请调试检查

 int lenghtOfFile = connection.getContentLength();

&#34; lenghtOfFile&#34;是对的。 并非所有服务器都返回ContentLength。 有时为-1。

答案 1 :(得分:-1)

您无法在UI线程之外修改UI。快速修复可能是:

    yourFragment.getActivity().runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            progressBar.setProgress(progress);
        }
    });