本文介绍了在Android上的WebView中阻止URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想阻止链接加载到Webview中.
I want to block a link from loading within a 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
.如何使用此代码执行此操作?谢谢.
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
. How can I do this with this code? Thank you.
推荐答案
shouldOverrideUrlLoading
将检查加载到WebView中的网页URL和页面内容中加载的所有URL
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;
}
}
这篇关于在Android上的WebView中阻止URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!