0

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.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
PrabhuPrakash
  • 261
  • 2
  • 7

1 Answers1

1

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.

Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • thanks. but i want to detect whether the outgoing call is answered. these events is not triggering after i attend the call. – PrabhuPrakash May 29 '17 at 09:19
  • @PrabhuPrakash You can track from the outbound call state is *starting* to the point that the call ended (*idle*). Accessing that the call is answered is ***only*** available to service apps/rooted phones via reflection on the `PreciseCallState` that added in API 21+ – SushiHangover May 29 '17 at 09:31
  • oh okey.. thank you.. any other options to get this event? – PrabhuPrakash May 29 '17 at 09:59
  • @PrabhuPrakash No other way that I know of, you can search StackOverflow, Google, etc... but the only broadcast that I know if to get this is within the PreciseCallState: https://stackoverflow.com/a/29490832/4984832 – SushiHangover May 29 '17 at 10:01
  • For me outgoing calls are not triggering here in the line "case Intent.ActionNewOutgoingCall:" – Sujatha Nov 07 '18 at 05:13
  • @SushiHangover Hi, How can I call this broadcastreciver from my shared code? – Anand Sep 04 '19 at 05:19