Complient链接URL不会在WebView KitKat 4.4.4中调用onPageFinished

时间:2014-10-22 06:43:16

标签: android ruby-on-rails webview

我有一个适用于所有Api包括Kitkat 4.4.2的应用程序。

但是,在KitKat 4.4.4中,onPageFinished()没有为1个链接触发:

  

HREF =" HTTP://app.host.com/projects/30/edit/"

我知道KitKat WebView问题,据我所知,这个URL应该有用。

与此网址对应的网络应用程序是用RoR编写的,并且已禁用turbolinks

我的WebView客户端如下所示。

我花了几天时间试图让它发挥作用。

如果有人发现任何错误,请告诉我。

// Set WebView client
        mWebAppView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                Log.d(TAG, "#shouldOverrideUrlLoading url called: " + url);

                // Open an email client on device when openning a mailto: link
                if (MailTo.isMailTo(url)) {
                    Intent intent = null;
                    try {
                        intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    view.getContext().startActivity(intent);
                    return true;
                    // open the help link in a browser.
                } else if (url
                        .contains("www.host.com/expertise/business-development/index.cfm")) {
                    Log.d(TAG, "should open browser");
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                } else {
                    view.loadUrl(url);
                    return true;
                }

            }

            @SuppressLint("InlinedApi")
            @Override
            public void onPageFinished(WebView view, String url) {
//              super.onPageFinished(view, url);
                view.clearCache(true);
                Log.d(TAG, "#onPageFinished loaded page is :"+url);
                if (pd.isShowing() && pd != null) {
                    pd.dismiss();
                    Log.d(TAG, "Inside #onPageFinished, pd dismissed");
                }

                // Change orientation for the table screen for phones so that
                // the tables show correctly
                if ((url.contains("projects"))
                        && ((url.contains("stepfirst"))
                                || (url.contains("stepsecond")) || (url
                                    .contains("edit"))) && !isTablet) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else if (!isTablet) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onLoadResource(view, url);
                Log.d(TAG, "Inside #onLoadResource url :"+url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
                Log.d(TAG, "Inside #onPageStarted url :"+url);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view,
                    String url) {
                // TODO Auto-generated method stub
                Log.d(TAG, "Inside #shouldInterceptRequest url :"+url);
                return super.shouldInterceptRequest(view, url);

            }



        });

1 个答案:

答案 0 :(得分:0)

所以,这是我决定使用的丑陋黑客:

onPageFinished(),onPageStart()和shouldOverrideUrlLoading()并不总是被URL调用,即使它们格式正确。

我发现shouldInterceptRequest()是由我尝试过的所有无法比较的网址(仅限制且不完全科学)触发的,并且它为我提供了相同的数据来控制网址的流量,尽管在早期阶段页面加载。

所以,我使用了shouldInterceptRequest()而不是VERY限制onPageFinished(),onPageStart()和shouldOverrideUrlLoading()。

这是代码:

// Set WebView client
    mWebAppView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            Log.d(TAG, "#shouldOverrideUrlLoading url called: " + url);

            // Open an email client on device when openning a mailto: link
            if (MailTo.isMailTo(url)) {
                Intent intent = null;
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                view.getContext().startActivity(intent);
                return true;
                // open the help link in a browser.
            } else if (url
                    .contains("www.host.com/expertise/business-development/index.cfm")) {
                Log.d(TAG, "should open browser");
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            } else {
                view.loadUrl(url);
                return true;
            }

        }

        @SuppressLint("InlinedApi")
        @Override
        public void onPageFinished(WebView view, String url) {
            // super.onPageFinished(view, url);
            view.clearCache(true);
            Log.d(TAG, "#onPageFinished loaded page is :" + url);
            if (pd.isShowing() && pd != null) {
                pd.dismiss();
                Log.d(TAG, "Inside #onPageFinished, pd dismissed");
            }

            // Change orientation for the table screen for phones so that
            // the tables show correctly

            if ((url.endsWith("stepfirst/") || url.endsWith("stepsecond/"))&& !isTablet) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            } else if (!isTablet && !url.endsWith("edit/")){
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }

        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                String url) {
            // TODO Auto-generated method stub
            Log.d(TAG, "Inside #shouldInterceptRequest url :" + url);

            //This is a hack as the "edit" url isn't firing the onPageFinished.
             if (url.contains("projects") && url.contains("edit")) {
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
             }else if(url.contains("users") || url.endsWith("help/") || url.endsWith("projects/")){
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


             }
            return super.shouldInterceptRequest(view, url);
        }
    });

是的它是一个黑客,是的它有限......但Android强迫我的手。

希望它有所帮助。