Anand
Anand

Reputation: 1959

How to pass value from dependency service to shared code

I am trying to get the last call duration on my xamarin.forms app. On android part I am using dependency service.I can get the call duration. How to pass the duration to shared code back?

My Implementation on Android

class Dialer : ICallerDialer
    {
        public void 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);  

                How to pass this to Shared code back?
            }
           return;
        }
    }

My Interface

public interface ICallerDialer
    {
        void GetCallLogs(); 
    }

Dependency call when button click

  async void btnCall_Clicked(object sender, System.EventArgs e)
        {         
            DependencyService.Get<ICallerDialer>().GetCallLogs();
           //How to get duration here?
        }

Any help is appreciated.

Upvotes: 0

Views: 219

Answers (1)

VenkyDhana
VenkyDhana

Reputation: 905

Just change the return type of your method to string type.

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;
        }
    }

Interface

public interface ICallerDialer
    {
        string GetCallLogs(); 
    }

Dependency call when button click

async void btnCall_Clicked(object sender, System.EventArgs e)
        {         
           var duration = DependencyService.Get<ICallerDialer>().GetCallLogs();

        }

Upvotes: 1

Related Questions