Skip to content

DownloadListener

huxq17 edited this page May 19, 2020 · 7 revisions

DownloadListener()

空的构造函数,适用于后台下载
Empty constructor,for download in the background.

DownloadListener(FragmentActivity)

DownloadListener可以感知activity的生命周期,并且在页面销毁的时候自动移除监听,很智能并且不会造成内存泄漏
DownloadListener can aware of activity's lifecycle, and will remove this listener when the activity is destroyed.

DownloadListener(Fragment)

DownloadListener可以感知fragment的生命周期,并且在页面销毁的时候自动移除监听。
DownloadListener can aware of fragment's lifecycle, and will remove this listener when the fragment is destroyed.

disable()

禁用此监听,并且Pump会移除它。
Disable this downloadListener and Pump will remove this downloadListener later.

enable()

启用此监听,开始接收下载进度和结果。
Enable this downloadListener,start receiving download progress and results.

onProgress(int)

downloading.

onSuccess()

download success.

onFailed()

download failed.

getDownloadInfo()

Get DownloadInfo.

getStatus()

Get download status.

filter(DownloadInfo)

Filter the download information to be received, all received by default.

Example

Listen all download task:

 DownloadListener downloadListener = new DownloadListener() {
        @Override
        public void onProgress(int progress) {
            progressDialog.setProgress(progress);
        }

        @Override
        public boolean filter(DownloadInfo downloadInfo) {
            String url = downloadInfo.getUrl();
            return url.equals(url5);
        }

        @Override
        public void onSuccess() {
            progressDialog.dismiss();
            String apkPath = getDownloadInfo().getFilePath();
            APK.with(MainActivity.this)
                    .from(apkPath)
           //       .forceInstall();
                    .install();
            Toast.makeText(MainActivity.this, "Download Finished", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed() {
            progressDialog.dismiss();
        }
    };
    @Override
    protected void onStart() {
        super.onStart();
        //Enable this Observer.
        downloadListener.enable();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //Disable this listener and Pump will remove this observer later.
        downloadListener.disable();
    }

Listen special task:

    Pump.newRequest(url5, pipixiaFile.getAbsolutePath())
        //listen url5 download progress.
        .listener(new DownloadListener(MainActivity.this) {
        @Override
        public void onProgress(int progress) {
             progressDialog.setProgress(progress);
        }

         @Override
         public void onSuccess() {
            Toast.makeText(MainActivity.this, "Download Finished", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed() {
            Toast.makeText(MainActivity.this, "Download failed", Toast.LENGTH_SHORT).show();
        }
     })
     .submit();