0

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

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Anand
  • 1,866
  • 3
  • 26
  • 49
  • 1
    In android platform, you can achieve it by dependence service, then read the call duration by Android Database through Cursor. There is a simialr case https://forums.xamarin.com/discussion/155644/how-to-get-call-information-after-every-incoming-outgoing-call-in-the-xamarin-android – Leon Jun 07 '19 at 13:24
  • @LeonLu-MSFT bro what about ios? – Anand Jun 07 '19 at 14:25
  • Hey @AndroDevil Yes it is possible to do it on iOS as well, using the CallKit https://learn.microsoft.com/en-us/xamarin/ios/platform/callkit You can override the Setter on the IsConnected property, so that when it's true, you start the timer, and when it's false, you stop the timer. There is no property as such. – Saamer Jun 07 '19 at 16:43
  • @Saamer Thanks for the info.Let me check that – Anand Jun 08 '19 at 16:37
  • @Saamer bro callkit is for Voip right? I dont want voip call.I simply want normal call logs – Anand Jun 08 '19 at 16:39
  • you could monitor call status,record the time of Call connected and disconnected,and the call duration is the time difference. – Leo Zhu Jun 10 '19 at 06:17
  • @LeoZhu-MSFT Hi, I am not familiar with call kit. What I want is user should place call from phone app(Dialer). will it possible with call kit? – Anand Jun 10 '19 at 06:31
  • maybe this would give you a direction,[call](https://stackoverflow.com/a/18310457/10768653),I'm not very familiar with ios, but it should be able to monitor call status like android – Leo Zhu Jun 10 '19 at 06:49
  • @LeoZhu-MSFT Thanks for the reply. Can I use Callkit for normal calls. ie; making call from callkit UI but it will call the normal phone call(not voip)? – Anand Jun 10 '19 at 06:57
  • yes,you could,the monitor is like a system broadcast,when call it will send to you the status – Leo Zhu Jun 10 '19 at 07:12

1 Answers1

2

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.  
            }
Anand
  • 1,866
  • 3
  • 26
  • 49