How to listen for a WebView finishing loading a URL?

前端 未结 17 746
予麋鹿
予麋鹿 2020-11-22 05:40

I have a WebView that is loading a page from the Internet. I want to show a ProgressBar until the loading is complete.

How do I listen for

17条回答
  •  有刺的猬
    2020-11-22 06:31

    You can trace the Progress Staus by the getProgress method in webview class.

    Initialize the progress status

    private int mProgressStatus = 0;
    

    then the AsyncTask for loading like this:

    private class Task_News_ArticleView extends AsyncTask {
        private final ProgressDialog dialog = new ProgressDialog(
                your_class.this);
    
        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            try {
                while (mProgressStatus < 100) {
                    mProgressStatus = webview.getProgress();
    
                }
            } catch (Exception e) {
    
            }
            return null;
    
        }
    
        protected void onPostExecute(Void result) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        }
    }
    

提交回复
热议问题