跟踪WebView结束URL

时间:2017-04-11 10:54:31

标签: android url webview

我正在尝试静默检查网址是否重定向到特定的结束网址。主网址是一个优惠网址,该网址在普通网络浏览器中加载到Google Play市场网址。

我正在尝试使用WebView实现相同的目标。

以下是我的尝试:

 private void loadUrl() {


        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); // load online by default
        if (Build.VERSION.SDK_INT >= 19) {
            webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        } else {
            webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }


        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {

                super.onReceivedError(view, errorCode, description, failingUrl);
                webview.setVisibility(View.GONE);


            }

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

            @Override
            public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
                super.onTooManyRedirects(view, cancelMsg, continueMsg);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("URL -->" + url);

                // Here put your code
                Log.d("My Webview", url);
                view.loadUrl(url);

                // return true; //Indicates WebView to NOT load the url;
                return false; //Allow WebView to load url
            /*    if (url.startsWith("https://play.google.com")) {
                    //Offer is available and
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(offerUrl)));
                        finish();
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(offerUrl)));
                        finish();
                    }
                    return true;
                } else {
                    view.loadUrl(url);
                    return false;
                }*/
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("URL OnPageFinished -->" + url);
                if (url.startsWith("https://play.google.com") || url.startsWith("market://details?")) {
                    //Offer is available and
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(finalUrl)));
                        finish();
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(finalUrl)));
                        finish();
                    }
                } else {
                    updateUi();
                }
                super.onPageFinished(view, url);
            }
        });

        webview.loadUrl(offerUrl);
    }

非常感谢帮助!

0 个答案:

没有答案