Block a URL in a WebView on Android

后端 未结 3 1297
猫巷女王i
猫巷女王i 2020-12-12 00:11

I want to block a link from loading within a Webview.

Code

public class WebMy extends Activity {


   
    private WebView mWebview;

         


        
相关标签:
3条回答
  • 2020-12-12 00:26
        webView.setWebViewClient(new myWebClient()); 
    // add this while initializing webview
    
    
    // then do add following code
    
    
    
    
    
    public class myWebClient extends WebViewClient {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
               // super.onPageStarted(view, url, favicon);
                if (Uri.parse(url).getHost().contains("https://qa.mstitute.com/test/build/#!/")) {
                    // This is my web site, so do not override; let my WebView load the page
                    Log.d("web","block");
    
                }else{
                    super.onPageStarted(view, url, favicon);
                }
    
                Log.d("web","Started");
            }
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
    
                if (Uri.parse(url).getHost().equals("https://qa.mstitute.com/test/build/#!/")) {
                    // This is my web site, so do not override; let my WebView load the page
                    Log.d("web","block");
                    return false;
                }else {
                    view.loadUrl(url);
                    return true;
                }
    
            }
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request,
                                        WebResourceError error) {
                super.onReceivedError(view, request, error);
                Log.d("web","got an error");
    
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
    
    
    
    
    
            }
        }
    
    0 讨论(0)
  • 2020-12-12 00:39

    this will cause nothing to happen when the link "http://www.myweb.com/pepito" is clicked

    public class MyWebViewClient extends WebViewClient {
        public boolean shuldOverrideKeyEvent (WebView view, KeyEvent event) {
             // Do something with the event here
             return true;
        }
    
        public boolean shouldOverrideUrlLoading (WebView view, String url) {
            return url.equals("http://www.myweb.com/pepito");
        }
    }
    
    0 讨论(0)
  • 2020-12-12 00:44

    shouldOverrideUrlLoading will examine the web page URL loaded into the WebView and all URLs loaded within the page content.

    public class MyWebViewClient extends WebViewClient {
        public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
             
             return true;
        }
    
        public boolean shouldOverrideUrlLoading (WebView view, String url) {
            if (Uri.parse(url).getHost().equals("http://www.myweb.com/pepito")) {
                 // This is my web site, so do not override; let the WebView load the page.
                 return false;
            }
    
            // Reject everything else.
            return true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题