Share via

How can I block access to certain web pages in Android using Xamarin.Android in Visual Studio 2019?

kevin david 161 Reputation points
Dec 23, 2021, 5:37 AM

I am using Visual Studio 2019 and I need to block certain web pages of the smartphone, I work with Xamarin in android and the emulators that come by default in Visual Studio.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,191 Reputation points Microsoft External Staff
    Dec 24, 2021, 2:58 AM

    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.


1 additional answer

Sort by: Most helpful
  1. kevin david 161 Reputation points
    Jan 7, 2022, 8:02 PM

    Thank you very much, it has been very useful

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.