0

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();
        }
    }        
}
Elletlar
  • 3,136
  • 7
  • 32
  • 38
Mayra Mtz
  • 99
  • 1
  • 1
  • 5
  • "doesn't work" tells us nothing useful. Does it crash? Throw an exception or error? Have you used the debugger to verify that your code is actually being executed? – Jason Jul 04 '19 at 22:26
  • yeah i tested the code but takes the phone number but doesn't call it. – Mayra Mtz Jul 05 '19 at 16:19

1 Answers1

0

there are two error in your code above:

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

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.After Android6.0 you should requests the runtime permissions and the official doucument

here is a simple example:

[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();
        }
    }     
  }
}

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

finally you could call like

DependencyService.Get<IPhoneCall>().MakeQuickCall(phonenumber);
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23