Skip to content

DownloadListener

huxq17 edited this page Jul 6, 2019 · 7 revisions

filter

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

onProgress

downloading.

onSuccess

download success.

onFailed

download failed.

getDownloadInfo

Get DownloadInfo.

getStatus

Get download status.

disable

Disable this observer and Pump will remove this observer later.

enable

Enable this Observer.

Example

Listen all download task:

 DownloadListener downloadObserver = 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.
        downloadObserver.enable();
    }

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

Listen special task:

    Pump.newRequest(url5, pipixiaFile.getAbsolutePath())
        //listen url5 download progress.
        .listener(new DownloadListener(url5) {
        @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();

    @Override
    protected void onStop() {
        super.onStop();
        //unSubscribe url5 download listener.
        Pump.unSubscribe(url5);
    }
Clone this wiki locally