How to get event when i am answering outgoing call

后端 未结 1 1573
终归单人心
终归单人心 2021-01-26 07:16

Can you help me to understand how to detect whether the outgoing call is answered or not ( I need to record call starting from answer till dropping)? I can detect it for incomin

相关标签:
1条回答
  • 2021-01-26 08:09

    Use TelephonyManager.ActionPhoneStateChanged to monitor the TelephonyManager state, upon receiving TelephonyManager.ExtraStateIdle you know when phone radio is now idling (no call in process).

    Inbound & Outbound BroadcastReceiver Example:

    [BroadcastReceiver(Name = "com.sushhangover.OutgoingCallBroadcastReceiver")]
    [IntentFilter(new[] { Intent.ActionNewOutgoingCall, TelephonyManager.ActionPhoneStateChanged })]
    public class OutgoingCallBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            switch (intent.Action)
            {
                case Intent.ActionNewOutgoingCall:
                    var outboundPhoneNumber = intent.GetStringExtra(Intent.ExtraPhoneNumber);
                    Toast.MakeText(context, $"Started: Outgoing Call to {outboundPhoneNumber}", ToastLength.Long).Show();
                    break;
                case TelephonyManager.ActionPhoneStateChanged:
                    var state = intent.GetStringExtra(TelephonyManager.ExtraState);
                    if (state == TelephonyManager.ExtraStateIdle)
                        Toast.MakeText(context, "Phone Idle (call ended)", ToastLength.Long).Show();
                    else if (state == TelephonyManager.ExtraStateOffhook)
                        Toast.MakeText(context, "Phone Off Hook", ToastLength.Long).Show();
                    else if (state == TelephonyManager.ExtraStateRinging)
                        Toast.MakeText(context, "Phone Ringing", ToastLength.Long).Show();
                    else if (state == TelephonyManager.ExtraIncomingNumber)
                    {
                        var incomingPhoneNumber = intent.GetStringExtra(TelephonyManager.ExtraIncomingNumber);
                        Toast.MakeText(context, $"Incoming Number: {incomingPhoneNumber}", ToastLength.Long).Show();
                    }
                    break;
                default:
                    break;
            }
        }
    }
    

    Note: Make sure you add permissions to ReadPhoneState and ProcessOutgoingCalls for this example to work.

    0 讨论(0)
提交回复
热议问题