本文介绍了Firebase Android:如何取消下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何取消从Firebase的下载任务?

How can I cancel my downloading task from Firebase?

只要单击 ProgressDialog

这是我的下载活动 ExamesActivity.java 所在的部分。看起来像:

Here is the part where my Download Activity ExamesActivity.java is. It looks like:

//Download the File on Button(Download) click:
        bDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Initalizing teh Spinner-to-String functions:
                Grade = spClasse.getSelectedItem().toString();
                Type = spEpoca.getSelectedItem().toString();
                Subject = spDisciplina.getSelectedItem().toString();
                Year = spAno.getSelectedItem().toString();

                //Download the File:
                //First Check if ON the Spinner, everything is choosen. It should be. If not, show error Toast.
                if (Grade.equals("...") | Type.equals("...") | Disciplina.equals("...") | Year.equals("..."){

                    //Show the The Error Toast:
                    Toast.makeText(ExamesActivity.this, "everything shall be choosen", Toast.LENGTH_SHORT).show();

                } else {                                          //What the dir would look like: "Subject/Grade/Year-Type.extension"
                    pdfRef = mStorageRef.child(Subject + "/" + Grade + "/" + Year + "-" + Type + ".pdf");
                    File root = android.os.Environment.getExternalStorageDirectory();
                    File dir = new File(root.getAbsolutePath() + "/Exams-App/");

                    //Show the ProgressDialog while downloading:
                    progressDialog.show();

                    if (!dir.exists()) {
                        dir.mkdirs();
                    }

                    localFile = new File(dir, Subject + "-" + Year + "-" + Grade + "-" + Type + ".pdf");


                    pdfRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            // Local temp file has been created
                            progressDialog.dismiss();
                            Toast.makeText(ExamesActivity.this, "Exam was successfully downloaded!️",  Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle any errors
                            progressDialog.dismiss();
                            Toast.makeText(ExamesActivity.this, "Exam not found on the server.", Toast.LENGTH_LONG).show();
                        }

                    }).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            //Some math to get the Percentage of the Download :)
                            double progressPercentage = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                            double size = (taskSnapshot.getTotalByteCount()) / (1000000);
                            progressDialog.setMessage("PDF Size: " + (size) + " - " + ((int) progressPercentage) + "% - Click away to cancel the download.");

                        }
                    });
                }
            }
        });


推荐答案

pdfRef。[getFile] [1](localFile)返回。该对象是的子类,它具有 cancel()方法。您需要保留对此任务的引用,并调用其cancel方法来取消下载。

pdfRef.[getFile][1](localFile) returns a FileDownloadTask. This object is a subclass of CancellableTask, which has a cancel() method. You will need to hold a reference to this task and call its cancel method to cancel the download.

这篇关于Firebase Android:如何取消下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:30