Hello,
Welcome to our Microsoft Q&A platform!
Firstly, if you want to block web pages for Android OS layer, Xamarin.Android application cannot achieve it.
If you want to block the web page in your Android app, If could be achieved by Webview
. Please set the SetWebViewClient
method. then ovrride the ShouldOverrideUrlLoading
method to block the url. Following code is simple about Reject everything except My load url.
WebView webview = FindViewById<WebView>(Resource.Id.webView1);
webview.Settings.JavaScriptEnabled = true;
webview.LoadUrl("https://www.google.com.hk/");
webview.SetWebViewClient(new MyWebviewClient());
internal class MyWebviewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
if (request.Url.ToString().Contains("https://www.google.com.hk/"))
{
// This is my web site, so do not override; let the WebView load the page.
return false;
}
// Reject everything else.
return true;
}
}
If you want to block the specific url. please use following code.
internal class MyWebviewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
{
return !request.Url.ToString().Contains("https://www.google.com.hk/");
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.