PrabhuPrakash
PrabhuPrakash

Reputation: 261

How to get event when i am answering outgoing call

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 incoming calls but not for outgoing. So please help.

Upvotes: 0

Views: 1893

Answers (1)

SushiHangover
SushiHangover

Reputation: 74144

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.

Upvotes: 1

Related Questions