每次页面加载WebView android时调用超时线程

时间:2015-07-10 08:20:06

标签: android android-webview

第一次加载webview,在x秒后调用线程,当在webview中单击一个url时,为什么线程不再被触发。 pageStart中的线程只加载一次,但每次在webview中单击一个URL时都需要触发。

  public class MyAppWebViewClient extends WebViewClient {

        public MyAppWebViewClient() {
            timeout = true;
        }


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            showProgressDialog();

            new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(Constants.DELAYMILIS);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(timeout) {
                            // do what you want

                            systemUtils.dismissProgressDialog(activity.getResources().getString(R.string.app_name),activity.getResources().getString(R.string.chargement_message_dialog));

                        }
                    }
            }).start();



        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            super.shouldOverrideUrlLoading(view, url);

            webContainer.loadUrl(url);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(Constants.DELAYMILIS);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(timeout) {
                        // do what you want

                        systemUtils.dismissProgressDialog(activity.getResources().getString(R.string.app_name),activity.getResources().getString(R.string.chargement_message_dialog));

                    }
                }
            }).start();


            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            timeout = false;

            cancelCurrentLoading();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.e("ERROR_LOADING_CODE: ", description);

            timeout = false;

            noConnection();

        }
    }

/ *        Mehtod解雇加载微调器     * /     public static void cancelCurrentLoading(){

    showWebView();

    if (progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}





 /*
        Method to show and hide webview
     */
    public static void showWebView(){

        webContainer.setVisibility(View.VISIBLE);
    }

    public static void hideWebView(){

        webContainer.setVisibility(View.GONE);

    }

0 个答案:

没有答案