在 Android 上的 WebView 中阻止 URL
Block a URL in a WebView on Android
我想阻止 link 在 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;
}
}
我不知道如何在我的代码中使用它。比如我要屏蔽这个urlhttp://www.myweb.com/pepito
。我怎样才能用这段代码做到这一点?谢谢。
单击 link“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");
}
}
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;
}
}
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
}
}
解决方案 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
This code wont allow to go google
对于 Xamarin Developers(它也可以适应 Native),这是一个更新的答案,因为方法 ShouldOverrideUrlLoading(WebView view, string url)
是 obsolete:
[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");
}
我想阻止 link 在 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;
}
}
我不知道如何在我的代码中使用它。比如我要屏蔽这个urlhttp://www.myweb.com/pepito
。我怎样才能用这段代码做到这一点?谢谢。
单击 link“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");
}
}
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;
}
}
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
}
}
解决方案 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 This code wont allow to go google
对于 Xamarin Developers(它也可以适应 Native),这是一个更新的答案,因为方法 ShouldOverrideUrlLoading(WebView view, string url)
是 obsolete:
[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");
}