4

我想阻止链接在 Webview 中加载。

代码

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

潜在解决方案

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

我不知道如何在我的代码中使用它。例如,如果我想阻止这个 url http://www.myweb.com/pepito。我怎么能用这段代码做到这一点?谢谢你。

4

5 回答 5

3

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;
    }
}
于 2016-03-10T09:37:03.230 回答
2

当点击链接“ 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");
    }
}
于 2016-03-10T09:36:36.003 回答
0
    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





        }
    }
于 2019-09-05T11:21:54.283 回答
0

对于 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");
}
于 2021-10-20T05:17:37.800 回答
-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
        }
     }
   }
 }

解决方案 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
    }
  }
}

if (Uri.parse(url).host == "google.com") return false 此代码不允许访问 google

于 2021-05-20T13:53:20.947 回答