可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
You can use WebViewClient.shouldOverrideUrlLoading to detect every URL changes on your WebViewClient
回答2:
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 :)
回答3:
For those load url by javascript. There is a way to detect the url change by JavascriptInterface. Here I use youtube for example. Use JavaScriptInteface has async issue, luckily its just a callback here so that would be less issue. Notice that @javascriptinterface annotation must be existed.
{
youtubeView.getSettings().setJavaScriptEnabled(true);
youtubeView.setWebViewClient(mWebViewClient);
youtubeView.addJavascriptInterface(new MyJavaScriptInterface(),
"android");
youtubeView.loadUrl("http://www.youtube.com");
}
WebViewClient mWebViewClient = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
@JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
回答4:
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);
回答5:
Method onPageFinished seems to work at least in modern WebViews.
回答6:
Try to use onLoadResource
. It will be called at least 1 time even if you are using
JS to change your url. But it may be called more than one time, so be careful.
回答7:
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.
回答8:
I had the same problem. So i've solved this problem by overriding public void onPageStarted(WebView view, String url, Bitmap favicon)
method as follows:
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// Here you can check your new URL.
Log.e("URL", url);
super.onPageStarted(view, url, favicon);
}
});
Also you can overridepublic void onPageFinished(WebView view, String url)
method to get a new URL at the end of page load process.
回答9:
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
...
回答10:
This will help to detect redirect caused by javascript.
WebViewClient mWebViewClient = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);"); }
};
myWebView.setWebViewClient(mWebViewClient);
}
@JavascriptInterface
public void onUrlChange(String url) {
Toast.makeText(mContext, "Url redirected",Toast.LENGTH_SHORT).show();
}