Android WebView “tel:” links show web page not found Xamarin.android c#

Mahdhi Ali 21 Reputation points
2021-07-04T16:02:36.367+00:00

Android WebView “tel:” links show web page not found Xamarin .android c#

i am using vs 2019
this my 1st project in Xamarin.
please help me

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2021-07-05T07:25:53.687+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    First of all, please make sure your “tel:” links type is tel:124568 simple.

    Then, please check if you have created a new WebViewClient. to handle the open the dial operation.

       web_view.SetWebViewClient(new HelloWebViewClient(this));  
         
        public class HelloWebViewClient : WebViewClient  
           {  
               public HelloWebViewClient(MainActivity mainActivity)  
               {  
                   MainActivity = mainActivity;  
               }  
         
               public MainActivity MainActivity { get; }  
         
                
         
               public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)  
               {  
                   var uri = view.Url;  
         
                       if (uri.ToString().StartsWith("tel:"))  
                   {  
                       //Handle telephony Urls  
         
                      // test ur; var url1 = "tel:124568";  
                       Intent intent = new Intent(Intent.ActionView, Uri.Parse(uri));  
                       MainActivity.StartActivity(intent);  
                   }  
                   else  
                   {  
                           //Handle Web Urls  
                           view.LoadUrl(uri.ToString());                 
                   }  
         
                   return true;  
                    
               }  
           }  
    

    ======================
    Update===========================

    Please override the ShouldOverrideUrlLoading(WebView view, string url) use this url directly, it worked as normal

       public class HelloWebViewClient : WebViewClient  
           {  
               public HelloWebViewClient(MainActivity mainActivity)  
               {  
                   MainActivity = mainActivity;  
               }  
         
               public MainActivity MainActivity { get; }  
         
               public override bool ShouldOverrideUrlLoading(WebView view, string url)  
               {  
         
                 //  var uri = view.Url;  
         
                   if (url.ToString().StartsWith("tel:"))  
                   {  
                       //Handle telephony Urls  
         
                       // test ur; var url1 = "tel:124568";  
                       Intent intent = new Intent(Intent.ActionView, Uri.Parse(url));  
                       MainActivity.StartActivity(intent);  
                   }  
                   else  
                   {  
                       //Handle Web Urls  
                       view.LoadUrl(url.ToString());  
                   }  
         
                   return true;  
         
         
               }  
         
               //public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)  
               //{  
               //    var uri = view.Url;  
         
               //        if (uri.ToString().StartsWith("tel:"))  
               //    {  
               //        //Handle telephony Urls  
         
               //       // test ur; var url1 = "tel:124568";  
               //        Intent intent = new Intent(Intent.ActionView, Uri.Parse(uri));  
               //        MainActivity.StartActivity(intent);  
               //    }  
               //    else  
               //    {  
               //            //Handle Web Urls  
               //            view.LoadUrl(uri.ToString());                 
               //    }  
         
               //    return true;  
                    
               //}  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    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. Mahdhi Ali 21 Reputation points
    2021-07-05T09:05:08.983+00:00

    Thanks Mr. Leon Lu for your support.
    I think almost reach.

    but unfortunately my purpose not complete

    i host a website and i include that website using loadUrl application

    here i directly give "tel:77888544" into var uri. that time dial pad opened
    but its not get url from my webpage

    Html Code

    <a href="tel:77888544"><Hotline : 77888544 </a>

    xamarin

    using Android.App;
    using Android.OS;
    using Android.Runtime;
    using AndroidX.AppCompat.App;
    using Android.Webkit;
    using Android.Content;
    using static Xamarin.Essentials.Resource;
    using Android.Net;

    namespace starfox_serv_4
    {
    [Activity(Label = "@ hide /app_name", Icon ="@mipmap/starfox", Theme = "@STYLE /AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {

        public WebView mwebview;  
        public WebViewClient mwebclient;  
        protected override void OnCreate(Bundle savedInstanceState)  
        {  
            //mycode  
    
            mwebclient = new WebViewClient();  
            mwebview = FindViewById<WebView>(Resource.Id.webView1);  
    
            mwebview.Settings.JavaScriptEnabled = true;  
            mwebview.LoadUrl("https://www.secuviews.com/apk/"); //web path  
            mwebview.SetWebViewClient(mwebclient);  
    
            mwebview.SetWebViewClient(new HelloWebViewClient(this));  
    
        }  
        public class HelloWebViewClient : WebViewClient  
        {  
            public HelloWebViewClient(MainActivity mainActivity)  
            {  
                MainActivity = mainActivity;  
            }  
            public MainActivity MainActivity { get; }  
    
            public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)  
            {  
                **var uri = view.Url;**  
                if (uri.ToString().StartsWith("tel:"))  
                {  
                    //Handle telephony Urls  
                    // test ur; var url1 = "tel:124568";  
                    Intent intent = new Intent(Intent.ActionView, Uri.Parse(uri));  
                    MainActivity.StartActivity(intent);  
                }  
                else  
                {  
                    //Handle Web Urls  
                    view.LoadUrl(uri.ToString());  
                }  
                return true;  
    
            }  
        }
    
    0 comments No comments