Anand
Anand

Reputation: 1959

Xamarin forms get call duration

I have a xamarin.forms application which will open dialer when a label clicks.What I am trying to achieve is

  1. User clicks on Label--> Dialer(Phone App in ios) opens
  2. User call and End--> Return to app

I can open dialer when click on the label.

Can I get the call duration in my app? Is it possible?.If not,Is there any other workaround like counting the idle state time when moves from XF app to dialer.Please guide

Upvotes: 0

Views: 1181

Answers (1)

Anand
Anand

Reputation: 1959

Android Part

I used Xamarin essential for move to dialer.

*For get the duration of last called number :

Created a dependency in Android folder named Dialer

[assembly: Dependency(typeof(Dialer))]
namespace DialerDemo.Droid
{
    class Dialer : ICallerDialer
    {
        public string GetCallLogs()
        {
            string queryFilter = String.Format("{0}={1}", CallLog.Calls.Type, (int)CallType.Outgoing);
            string querySorter = String.Format("{0} desc ", CallLog.Calls.Date);
            ICursor queryData1 = Android.App.Application.Context.ContentResolver.Query(CallLog.Calls.ContentUri, null, queryFilter ,null, querySorter);
            int number = queryData1.GetColumnIndex(CallLog.Calls.Number);
            int duration1 = queryData1.GetColumnIndex(CallLog.Calls.Duration);
            if (queryData1.MoveToFirst() == true)
            {
                String phNumber = queryData1.GetString(number);
                String callDuration = queryData1.GetString(duration1);

                return callDuration;
            }
            return string.Empty;
        }
    }
}

In My shared code Created the interface.

namespace DialerDemo
{
    public interface ICallerDialer
    {
        string GetCallLogs(); 
    }
} 

For getting call duration in android ,in My MainPage.xaml.cs I called it like this.

 var duration = DependencyService.Get<ICallerDialer>().GetCallLogs();

ios Part

In Appdelegate class I added the apple telephony API code.

public CTCallCenter c { get; set; }

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App());

    c = new CTCallCenter();

    c.CallEventHandler = delegate (CTCall call)
    {

        if (call.CallState == call.StateIncoming)
        {                                

        }
        else if (call.CallState == call.StateDialing)
        {

        }
        else if (call.CallState == call.StateConnected)
        {
            try
            {
                MessagingCenter.Send<Object>(new Object(), "CallConnected");
            }
            catch (Exception ex)
            {
            }
        }
        else if (call.CallState == call.StateDisconnected)
        {

         try {                     
                MessagingCenter.Send<Object>(new Object(), "CallEnded");

             }
             catch( Exception ex)
             {
             }
        }
    };

    return base.FinishedLaunching(app, options);
}

For ios I calculated the time difference according to the messaging center values and got the call duration in my shared code something like this,

 try
            {

                PhoneDialer.Open(number);           
                MessagingCenter.Subscribe<Object>(this, "CallConnected", (sender) => {
                     CallStartTime = DateTime.Parse(DateTime.Now.ToString("hh:mm:ss"));
                });
                MessagingCenter.Subscribe<Object>(this, "CallEnded", (sender) => {
                CallEndTime = DateTime.Parse(DateTime.Now.ToString("hh:mm:ss"));
                CallDuration = CallEndTime - CallStartTime;
                });


            }
            catch (FeatureNotSupportedException ex)
            {
                // Phone Dialer is not supported on this device.  
            }

Upvotes: 2

Related Questions