使用android电话技术的xamarin.forms调用挂起事件

使用android电话技术的xamarin.forms调用挂起事件,xamarin.forms,Xamarin.forms,在我的xamarin.forms应用程序中,我正在访问android电话API。在我的应用程序中,用户可以通过单击按钮进行呼叫。我创建了一个广播接收器,并在OnReceive中提供了PhoneStateListener。我在呼叫时使用共享代码呼叫广播接收器。但是,CallState.Idle将在第一次调用。我在CallState.Idle添加了一个消息中心。但问题是,它将在广播接收机呼叫时呼叫。我想要的是CallState.Idle事件在call挂起时的状态。如何做到这一点 我的主要功能是广播接

在我的xamarin.forms应用程序中,我正在访问android电话API。在我的应用程序中,用户可以通过单击按钮进行呼叫。我创建了一个广播接收器,并在OnReceive中提供了PhoneStateListener。我在呼叫时使用共享代码呼叫广播接收器。但是,CallState.Idle将在第一次调用。我在CallState.Idle添加了一个消息中心。但问题是,它将在广播接收机呼叫时呼叫。我想要的是CallState.Idle事件在call挂起时的状态。如何做到这一点

我的主要功能是广播接收器。

   [BroadcastReceiver(Name = "com.companyname.SampleOutgoingcall.OutgoingCallBroadcastReceiver")]
        [IntentFilter(new[] { Intent.ActionNewOutgoingCall, TelephonyManager.ActionPhoneStateChanged })]

        public class OutgoingCallBroadcastReceiver : BroadcastReceiver
        {
            public int READ_CALL_LOG { get; private set; }

            public override void OnReceive(Context context, Intent intent)
            {
                StateListener phoneStateListener = new StateListener();
                TelephonyManager telephonyManager = (TelephonyManager)context.GetSystemService(TelephonyService);
                telephonyManager.Listen(phoneStateListener, PhoneStateListenerFlags.CallState);
            }
        }



        public class StateListener : PhoneStateListener
        {
            public override void OnCallStateChanged(CallState state, string incomingNumber)
            {

                base.OnCallStateChanged(state, incomingNumber);
                switch (state)
                {
                    case CallState.Ringing:
                        break;
                    case CallState.Offhook:

                        try
                        {
                            MessagingCenter.Send<Object>(new Object(), "CallAndroidRinging");
                        }
                        catch (Exception ex)
                        {
                        }
                        break;
                    case CallState.Idle:
// Trying to get the call hung up event
                        try
                        {
                            MessagingCenter.Send<Object>(new Object(), "CallEndedAndroid");
                        }
                        catch (Exception ex)
                        {
                        }
                        break;
                }
            }

        }
DependencyService.Get<OutgoingcallTrack>().Send(phonenumber.Text);
我的android类

[assembly: Dependency(typeof(OutgoingCall))]
namespace SampleOutgoingcall.Droid
{
    class OutgoingCall : OutgoingcallTrack
    {
        public void Send(string text)
        {
            Intent intent = new Intent("com.companyname.SampleOutgoingcall.OutgoingCallBroadcastReceiver");        
            Forms.Context.SendBroadcast(intent);
        }
    }
}

在您的情况下,您希望仅在收到呼叫时发送消息,因此我建议在OFFHOOK中将bool变量设置为true以了解呼叫已收到,稍后在IDLE中,如果变量为true,则发送消息并为下一次呼叫设置变量false。你不介意是来电还是外线

bool iscallreceived=false;
开关(状态)
{
案例呼叫状态。振铃:
打破
案例CallState.Offhook:
尝试
{
Send(新对象(),“callandroidring”);
iscallreceived=true;
}
捕获(例外情况除外)
{
}
打破
案例调用状态。空闲:
//试图让电话挂断事件
如果(iscallreceived)
{
尝试
{
Send(新对象(),“CallendedRoid”);
iscallreceived=false;
}
捕获(例外情况除外)
{
}
}
打破
}

在您的情况下,您希望仅在收到呼叫时发送消息,因此我建议在摘机中将bool变量设置为true,以了解是否收到呼叫,稍后在空闲中发送消息,并为下一次呼叫设置变量false。你不介意是来电还是外线

bool iscallreceived=false;
开关(状态)
{
案例呼叫状态。振铃:
打破
案例CallState.Offhook:
尝试
{
Send(新对象(),“callandroidring”);
iscallreceived=true;
}
捕获(例外情况除外)
{
}
打破
案例调用状态。空闲:
//试图让电话挂断事件
如果(iscallreceived)
{
尝试
{
Send(新对象(),“CallendedRoid”);
iscallreceived=false;
}
捕获(例外情况除外)
{
}
}
打破
}

我只希望传出呼叫事件在您的情况下,只有在传出呼叫时才会调用OnCallStateChanged。所以你不用担心这个。我只希望传出呼叫事件在你的情况下OnCallStateChanged只会在传出呼叫时被调用。所以你不用担心这个。
switch (state)
{
    case CallState.Ringing:
        break;


    case CallState.Offhook:

        try
        {
           MessagingCenter.Send<Object>(new Object(), "CallAndroidRinging");
           iscallreceived=true;
        }

        catch (Exception ex)
        {
        }
        break;


    case CallState.Idle:
    // Trying to get the call hung up event
    if(iscallreceived)
    {
      try
      {
         MessagingCenter.Send<Object>(new Object(), "CallEndedAndroid");
         iscallreceived=false;
      }

      catch (Exception ex)
      {
      }
    }
    break;
}