Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent webview from loading some URLs [duplicate]

I create a android app that host website, the website loads some external links "URL".

I need to block some URLs that store in file on /res/raw/ from loading in webview.

I try override shouldInterceptRequest(WebView view, String url), but I don't know how use it.

like image 282
Mike jess Avatar asked Mar 17 '17 15:03

Mike jess


People also ask

How do I stop a WebView from loading a URL?

You have to override the method shouldOverrideUrlLoading (see here) of your WebViewClient , rather than the one you are overriding. Just return false if you want to load the page; true if you want to block loading.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.

Is WebView fast?

Using WebViews in your native application is very common these days but when it comes to performance, rendering of a WebView is quite slow.

How can I make WebView keep a video or audio playing in the background?

I did something similar: Just extend WebView and override onWindowVisibilityChanged . This way, the audio continues to play if the screen is locked or another app is opened. However, there will be no controls in the notification bar or on the lockscreen.


2 Answers

You can use shouldOverrideUrlLoading

method of the WebViewClient

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

in code:

    public class MyWebViewClient extends WebViewClient {
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (Uri.parse(url).getHost().equals("http://Your_website_url")) {
             // This is my web site, so do not override; let my WebView load the page
             return false;
        }

        // reject anything other
        return true;
    }
}


mWebview.setWebViewClient(new MyWebViewClient());  //set the webviewClient
like image 173
rafsanahmad007 Avatar answered Sep 30 '22 17:09

rafsanahmad007


You have to override the method shouldOverrideUrlLoading (see here) of your WebViewClient, rather than the one you are overriding. Just return false if you want to load the page; true if you want to block loading.

like image 43
Massimo Avatar answered Sep 29 '22 17:09

Massimo