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;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐