0

I am trying to silently check if an URL redirects to a particular end URL, The main URL is an offer URL which ends loading to Google Play Market URL in the normal web browser.

I am trying to achieve the same using a WebView.

Here is what I have tried:

 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);
    }

A help would be very appreciated!

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
Bhavik Mehta
  • 573
  • 8
  • 21
  • Have you checked http://stackoverflow.com/questions/30171230/android-webview-why-i-can-not-able-to-redirect-to-play-store-from-webview? – Eugen Martynov Apr 11 '17 at 11:00
  • @EugenMartynov: My case is a bit different and complex, please go through the question – Bhavik Mehta Apr 11 '17 at 11:16
  • Ok, the only one difference for me that you have to first go through all redirection and finally launch market application. Correct? – Eugen Martynov Apr 11 '17 at 14:53
  • @EugenMartynov:That's absolutely correct, but the onPageFinished() is being called twice or multiple times, in that scenario, and some of the expired offer links dont redirect to google play, so basically i want to redirect user to market when the link is alive, and message them if not – Bhavik Mehta Apr 12 '17 at 05:38
  • Do not listen for the finish, but listen for every redirection. Detect if it is play store link or show message if it is expired. – Eugen Martynov Apr 12 '17 at 08:14
  • @EugenMartynov: That is the only issue, i have setup the code to check if a google play link is there or another, but the problem is onPageFinished is called multiple times, and so my logic gets fail, however it should be called once, how to know if the url i get in onPageFinished is END url and there are no more redirections..? – Bhavik Mehta Apr 12 '17 at 09:49

0 Answers0