如何使用 Xamairn 表单拨打电话? [英] How to make phone calls using Xamairn forms?

查看:32
本文介绍了如何使用 Xamairn 表单拨打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xamarin 新手,我想直接拨打电话(无需打开拨号器).我试过这个例子,但它不起作用.点击请帮忙

I'm new in xamarin and I want make a phone call directly (without opening the dialler). I tried with this example but it doesn't work. Click Please help

public class PhoneCall_Droid : IPhoneCall
{
    public void MakeQuickCall(string PhoneNumber)
    {
        try
        {
            var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
            var intent = new Intent(Intent.ActionCall, uri);
            Xamarin.Forms.Forms.Context.StartActivity(intent);
        }
        catch (Exception ex)
        {
            new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
            {
                //User pressed OK
            })
            .SetMessage(ex.ToString())
            .SetTitle("Android Exception")
            .Show();
        }
    }        
}

推荐答案

上面的代码有两个错误:

there are two error in your code above:

1.Xamarin.Forms.Forms.Context 无法获取正确的上下文.

1.Xamarin.Forms.Forms.Context could not get the correct context.

您可以在 MainActiviy 中定义一个静态变量,例如:

you could defined a static variable in MainActiviy like :

public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
   {
      TabLayoutResource = Resource.Layout.Tabbar;
      ToolbarResource = Resource.Layout.Toolbar;

      base.OnCreate(savedInstanceState);
      global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

      Instance = this;

      LoadApplication(new App());

    }

您也可以使用当前活动插件,您可以参考当前活动

you also could use the Current Activity Plugin,you could refer to Current Activity

2.Android6.0之后你应该请求运行时权限官方文档

2.After Android6.0 you should requests the runtime permissions and the official doucument

这是一个简单的例子:

[assembly: Xamarin.Forms.Dependency(typeof(PhoneCall_Droid))]
namespace App18.Droid
{
  class PhoneCall_Droid: IPhoneCall
   {
     public void MakeQuickCall(string PhoneNumber)
      {
        try
        {
            if(ActivityCompat.CheckSelfPermission(MainActivity.Instance, Android.Manifest.Permission.CallPhone) != Android.Content.PM.Permission.Granted ){

               ActivityCompat.RequestPermissions(MainActivity.Instance, new string[] {Android.Manifest.Permission.CallPhone }, 1);
                return;
            }
            else
            {
            var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
            var intent = new Intent(Intent.ActionCall, uri);
            MainActivity.Instance.StartActivity(intent);
            }
        }
        catch (Exception ex)
        {
            new AlertDialog.Builder(MainActivity.Instance).SetPositiveButton("OK", (sender, args) =>
            {
                //User pressed OK
            })
            .SetMessage(ex.ToString())
            .SetTitle("Android Exception")
            .Show();
        }
    }     
  }
}

并且您也可以使用 nugetpackage Plugin.Permissions 来请求运行时权限(Permission.Location)参考 Plugin.Permissions

and you also could to use the nugetpackage Plugin.Permissions to request runtime permissions(Permission.Location) refer to Plugin.Permissions

最后你可以这样称呼

DependencyService.Get<IPhoneCall>().MakeQuickCall(phonenumber);

这篇关于如何使用 Xamairn 表单拨打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆