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:
First, we require the permissions to intercept phone calls:
[assembly: UsesPermission( Manifest.Permission.ReadPhoneState)] [assembly: UsesPermission( Manifest.Permission.ProcessOutgoingCalls)]
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) { } }
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...