【问题标题】:Open phone dialer using a Android Xamarin WebView html link使用 Android Xamarin WebView html 链接打开电话拨号器
【发布时间】:2017-02-02 11:33:00
【问题描述】:

我正在尝试使用 ShouldOverrideUrlLoading() 方法,但是当我调用它时应用程序崩溃了。

下面是我的代码:

private class HybridWebViewClient : WebViewClient
        {


            public override bool ShouldOverrideUrlLoading(WebView webView, string url)
            {

                var tel = "tel:";
                if (url.StartsWith(tel))
                {

                    var uri = Android.Net.Uri.Parse(url);
                    var intent = new Intent(Intent.ActionDial, uri);
                    var act = new Activity();
                    act.StartActivity(intent);
                }

            }
        }

提前致谢!

【问题讨论】:

    标签: c# android xamarin webview


    【解决方案1】:

    问题出在以下代码sn-p:

    var act = new Activity();
    act.StartActivity(intent);
    

    应该从当前的context 调用方法StartActivity 而不是新的Activity。所以需要将当前的context传递给HybridWebViewClient

    public class HybridWebViewClient : WebViewClient
    {
        Context context;
        public HybridWebViewClient(Context context)
        {
            this.context = context;
        }
    
        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            var tel = "tel:";
            if (url != null)
            {
                if (url.StartsWith(tel))
                {
    
                    var uri = Android.Net.Uri.Parse(url);
                    var intent = new Intent(Intent.ActionDial, uri);
                    context.StartActivity(intent);
                }
            }
            return true;
        }
    }
    

    而在OnCreate 方法中:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        ...
        webview.SetWebViewClient(new HybridWebViewClient(this));
        webview.LoadUrl("http://example.com");
       ...
    }
    

    【讨论】:

      【解决方案2】:

      故障转储中有什么?这有关系吗?

      shouldOverrideUrlLoading(WebView 视图,字符串 url) 此方法在 API 级别 24 中已弃用。请改用 shouldOverrideUrlLoading(WebView, WebResourceRequest)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多