我想阻止链接在 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
。我怎么能用这段代码做到这一点?谢谢你。