简体   繁体   中英

Check URL change in WebView

This function is not working when I change URL and go to another page in WebView.

Here is the code:

@Deprecated
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    Toast.makeText(this, "Url Change", Toast.LENGTH_SHORT).show();
    String url = view.getUrl();
    return true;
}

from android lolipop, there is a new method. so you have to use the two methods

     mWebView.setWebViewClient(new WebViewClient() {
                         @Override
                        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                 Toast.makeText(this, "Url Change",Toast.LENGTH_SHORT).show();
                                 String url = view.getUrl();
                            }


                            return true;
                        }

                         @Override
                         public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
                           Toast.makeText(this, "Url Change", Toast.LENGTH_SHORT).show();
                             String url = view.getUrl();
                             return true;
                        }
                    });

 });

        mWebView.setWebChromeClient(new WebChromeClient());

as well, make sure you set

mWebView.setWebChromeClient(new WebChromeClient());

like the above code.

Happy Code!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM