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 ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.