An app might need to keep track of, or be notified of, incoming and outgoing phone calls.
In order to intercept incoming and outgoing phone calls, we listen for broadcasts:
[assembly: UsesPermission( Manifest.Permission.ReadPhoneState)] [assembly: UsesPermission( Manifest.Permission.ProcessOutgoingCalls)]
[BroadcastReceiver] [IntentFilter(new[] { TelephonyManager.ActionPhoneStateChanged, Intent.ActionNewOutgoingCall })] public class PhoneCallReceiver : BroadcastReceiver { public override void OnReceive( Context context, Intent intent) { } }
if (intent.Action == TelephonyManager.ActionPhoneStateChanged) { var number = intent.GetStringExtra( TelephonyManager.ExtraIncomingNumber); if (!string.IsNullOrEmpty(number)) { // incoming call } }
ActionNewOutgoingCall
intent action:if (intent.Action == Intent.ActionNewOutgoingCall) { var number = intent.GetStringExtra(Intent.ExtraPhoneNumber); if (!string.IsNullOrEmpty(number)) { // outgoing call } }
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.
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.
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.
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.
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.