如何更改进度栏状态

时间:2019-02-07 15:09:09

标签: java android asynchronous

我的Firebase上有一个PDF文件。我正在使用AndroidPDFViewer library。然后,我使用AsyncTask检索数据。

我想知道如何使用视图类中的isFinishedI()函数停止ProgressBar,因为while循环不会退出?

public class PDFHandler {

    private PDFView pdfView =  null;
    private RetriverPDFStream retriverPDFStream;

    private RetriverPDFStream getRetriverPDFStream(){ return this.retriverPDFStream; }

    public void setPdfView(PDFView pdfView)
    {
        this.pdfView = pdfView;
    }

    public void openOnline(String link)
    {
        retriverPDFStream = new RetriverPDFStream();
        retriverPDFStream.execute(link);
    }

    public boolean isFinished ()
    {
        if(getRetriverPDFStream().getStatus() == AsyncTask.Status.RUNNING){
            return false;
        }else{
            return true;
        }
    }


    //This class to retrieve pdf online asynchronously
    public class RetriverPDFStream extends AsyncTask<String,Void, InputStream> {

        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            try{
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
                if(urlConnection.getResponseCode() == 200)
                {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
            }catch (IOException e){
                return null;
            }
            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream).load();
        }
    }
}

关于创建方法

protected void onCreate(Bundle savedInstanceState) {
    pdfView = findViewById(R.id.pdfView);
    progressBar = findViewById(R.id.progressBar);
    //This is function read PDF from URL
    PDFHandler pdfHandler = new PDFHandler();
    pdfHandler.setPdfView(pdfView);
    pdfHandler.openOnline(link);
    progressBar.setVisibility(View.VISIBLE);
    while(!pdfHandler.isFinished()){
         progressBar.setVisibility(View.INVISIBLE);
}

1 个答案:

答案 0 :(得分:0)

public boolean isFinished ()
{
    if(getRetriverPDFStream().getStatus() == AsyncTask.Status.FINISHED)
    {
        return true;
     }
    else
    {
        return false;
    }
}