Intercepting phone calls

An app might need to keep track of, or be notified of, incoming and outgoing phone calls.

How to do it...

In order to intercept incoming and outgoing phone calls, we listen for broadcasts:

  1. First, we require the permissions to intercept phone calls:
    [assembly: UsesPermission(
      Manifest.Permission.ReadPhoneState)]
    [assembly: UsesPermission(
      Manifest.Permission.ProcessOutgoingCalls)]
  2. Next, we need to create a broadcast receiver that can handle phone state changes and call events:
    [BroadcastReceiver]
    [IntentFilter(new[] {
      TelephonyManager.ActionPhoneStateChanged,
      Intent.ActionNewOutgoingCall })]
    public class PhoneCallReceiver : BroadcastReceiver {
      public override void OnReceive(
      Context context, Intent intent) {
      }
    }
  3. Now, in order to intercept incoming calls, we listen for phone state changes:
    if (intent.Action == TelephonyManager.ActionPhoneStateChanged) {
      var number = intent.GetStringExtra(
        TelephonyManager.ExtraIncomingNumber);
      if (!string.IsNullOrEmpty(number)) {
        // incoming call
      }
    }
  4. We can also intercept outgoing calls by listening for the ActionNewOutgoingCall intent action:
    if (intent.Action == Intent.ActionNewOutgoingCall) {
      var number = intent.GetStringExtra(Intent.ExtraPhoneNumber);
      if (!string.IsNullOrEmpty(number)) {
        // outgoing call
      }
    }

How it works...

If we wish to be notified when a call is made or there is a new incoming call, we can listen for these events using a broadcast receiver.

Note

It will be useful to first verify that the telephony features are supported on the device before attempting to use them.

Before we can interact with the phone aspects of the device, we have to request permission to do so. We will need the ReadPhoneState permission if we want to be notified of incoming calls, and we will need the ProcessOutgoingCalls permission if we want to be notified of outgoing calls.

Now, we can create the broadcast receivers that will handle the phone events. We can create two separate receivers, each listening for a specific broadcast, but this is not required. When creating a receiver that listens for multiple messages, we can combine the intent actions in the array passed to the [IntentFilter] attribute.

Tip

Broadcast receivers can listen for multiple intents if they are registered using multiple action intents with the intent filter.

We should listen for the TelephonyManager.ActionPhoneStateChanged intent if we want to be notified of incoming calls and the Intent.ActionNewOutgoingCall intent if we want to be notified of outgoing calls.

The TelephonyManager.ActionPhoneStateChanged action is broadcast for various reasons as the phone state changes, but we are only interested in the event if the ExtraIncomingNumber extra is there. The existence of this extra means that there is an incoming call.

Note

If the incoming call is from a private number, the value of the extra will be a negative integer.

To intercept outgoing calls, we respond to the Intent.ActionNewOutgoingCall intent action. When we receive this action, we can obtain the phone number using the ExtraPhoneNumber extra.

There's more...

We can cancel an outgoing call by setting the ResultData property of the broadcast receiver to null. And, we can change the number that is called by setting the ResultData property of the broadcast receiver as the new number. This, however, only applies to normal numbers. Emergency numbers cannot be manipulated or canceled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset