android 拦截webview加载url_如何在WebView(Android)中拦截URL加载?
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of myActivity is..
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
解决方案
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
更多推荐



所有评论(0)