简体   繁体   English

在 Android 上的 WebView 中阻止 URL

[英]Block a URL in a WebView on Android

I want to block a link from loading within a Webview.我想阻止在 Web 视图中加载链接。

Code代码

public class WebMy extends Activity {


   
    private WebView mWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
            setContentView(R.layout.pantalla);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);  
            
    
             
            mWebview  = new WebView(this);
            mWebview.setWebViewClient(new WebViewClient()); 
            mWebview.getSettings().setJavaScriptEnabled(true); // Enable JavaScript.

            mWebview .loadUrl("http://www.myweb.com");
            setContentView(mWebview );
           
    }

Potential Solution潜在解决方案

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) {
        if (Uri.parse(url).getHost().equals("www.google.com")) {
             // This is my web site, so do not override; let my WebView load the page.
             return false;
        }

        // Reject everything else.
        return true;
    }
}

I don´t know how I have to use this in my code.我不知道如何在我的代码中使用它。 For example, if I want to block this url http://www.myweb.com/pepito .例如,如果我想阻止这个网址http://www.myweb.com/pepito How can I do this with this code?我如何使用此代码执行此操作? Thank you.谢谢你。

shouldOverrideUrlLoading will examine the web page URL loaded into the WebView and all URLs loaded within the page content. shouldOverrideUrlLoading将检查加载到 WebView 中的网页 URL 和页面内容中加载的所有 URL。

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;
    }
}

this will cause nothing to happen when the link " http://www.myweb.com/pepito " is clicked当点击链接“ http://www.myweb.com/pepito ”时,这不会导致任何事情发生

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");
    }
}
    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





        }
    }

For Xamarin Developers (it can be adapted to Native also), this is an updated answer because the method ShouldOverrideUrlLoading(WebView view, string url) is obsolete :对于 Xamarin 开发人员(它也可以适用于 Native),这是一个更新的答案,因为方法ShouldOverrideUrlLoading(WebView view, string url)过时

[System.Obsolete]
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
    return !Uri.Parse(url).Host.Contains("YOUR_URL");
}

public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
    return !request.Url.ToString().Contains("YOUR_URL");
}

Solution 1解决方案1

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val webView : WebView = findViewById(R.id.webView)
    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            // This is my web site, so do not override; let the WebView load the page.
            if (Uri.parse(url).host == "google.com") return false
            // Reject everything else.
            return true
        }
     }
   }
 }

Solution 2解决方案2

class MainActivity : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val webView : WebView = findViewById(R.id.webView)
    webView.webViewClient = MyWebViewClient()
}

private class MyWebViewClient : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
        // This is my web site, so do not override; let the WebView load the page.
        if (Uri.parse(url).host == "google.com") return false
        // Reject everything else.
        return true
    }
  }
}

Kotlin <3科特林 <3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM