Android detect webview URL change

2020-01-29 06:42发布

I have a webview in my android app and would like to detect when the url changes.

I want to use this to hide the info button in the top bar when the user is on the info.php page and show it again when he is not on the info.php page.

I googled but can't find any working code, can anybody help me?

10条回答
forever°为你锁心
2楼-- · 2020-01-29 07:19

if you override this method of WebViewClient you will be able to handle url changes, and javascript url changes:

public class YXWebViewClient extends WebViewClient {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    Log.i("Listener", "Start");

}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    Log.i("Listener", "Finish");
   }

}

and then in your WebView set the WebViewClient

 yxWebViewClient = new YXWebViewClient();
 webview.setWebViewClient(yxWebViewClient);
查看更多
干净又极端
3楼-- · 2020-01-29 07:22

Method onPageFinished seems to work at least in modern WebViews.

查看更多
Explosion°爆炸
4楼-- · 2020-01-29 07:22

I used this method:

webView.setWebChromeClient(new WebChromeClient()
        {
            @Override public void onProgressChanged(WebView view, int newProgress) {
                if (view.getUrl().equals(mUrl))
                {

                }
                else
                {
                    mUrl = view.getUrl();
                    // onUrlChanged(mUrl) // url changed
                }
                super.onProgressChanged(view, newProgress);
            }
        };
);

mUrl is a fields as String...

查看更多
放我归山
5楼-- · 2020-01-29 07:26

I had an interesting problem that brought me to this question and happy to provide a solution that may help someone else if they come this way due to a googlebingyahoo search.

mwebVew.reload() would not pick up the new URL I wanted to load once I returned from a preference screen where I either logged on the user or logged off the user. It would update the shared preferences well enough but not update and replace the url web page. So after a few hours of not seeing an answer I tried the following and it worked a real treat.

mWebView.resumeTimers();

Here is the link to the doco if you want more info.

Hope it saves someone else the pain I went through, and happy to have someone tell me I should of implemented it another way.

查看更多
Rolldiameter
6楼-- · 2020-01-29 07:28

You can use WebViewClient.shouldOverrideUrlLoading to detect every URL changes on your WebViewClient

查看更多
三岁会撩人
7楼-- · 2020-01-29 07:33

I know I'm late to the game but I ran into this issue over and over ... I finally found a sloution which is pretty straight forward. Just override WebViewClient.doUpdateVisitedHistory

override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
    // your code here
    super.doUpdateVisitedHistory(view, url, isReload)
}

It works with all url changes even the javascript ones!

If this does not make you happy then I don't know what will :)

查看更多
登录 后发表回答