Xamarin表格获取通话时间 [英] Xamarin forms get call duration
问题描述
我有一个xamarin.forms应用程序,它将在标签单击时打开拨号程序.我想要实现的是
I have a xamarin.forms application which will open dialer when a label clicks.What I am trying to achieve is
- 用户单击标签"->拨号器(iOS中的Phone App)打开
- 用户通话和结束->返回应用程序
单击标签时,我可以打开拨号器.
I can open dialer when click on the label.
我可以在我的应用程序中获取通话时间吗?可能吗?如果没有,是否还有其他变通方法,例如计算从XF应用程序移动到拨号程序时的空闲状态时间.请指导
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
推荐答案
Android部件
我使用了Xamarin来进行拨号.
I used Xamarin essential for move to dialer.
*用于获取最后通话号码的持续时间:
在名为 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();
}
}
为了在android中获取通话时间,在My MainPage.xaml.cs中,我这样称呼它.
For getting call duration in android ,in My MainPage.xaml.cs I called it like this.
var duration = DependencyService.Get<ICallerDialer>().GetCallLogs();
iOS部件
在Appdelegate类中,我添加了苹果电话API代码.
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);
}
对于ios,我根据消息传递中心的值计算了时差,并在共享代码中获得了类似这样的通话时长,
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.
}
这篇关于Xamarin表格获取通话时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!