Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Processing Outgoing Phone Calls

Johan Pelgrim


You want to block certain calls, or alter the phone number about to be called.


Listen for the Intent.ACTION_NEW_OUTGOING_CALL broadcast action and set the result data of the broadcast receiver to the new number.


If you want to intercept a call before it is about to be placed you can implement a broadcast receiver and listen for the Intent.ACTION_NEW_OUTGOING_CALL action. This recipe is in essence similar to the recipe Do something when the phone rings, but it is more interesting since we can actually manipulate the phone number in this case!

Here are the steps:

  1. Create an OutgoingCallInterceptor class which extends the BroadcastReceiver
  2. Override the onReceive method.
  3. Extract the phone number which the user originally intended to call via the Intent.EXTRA_PHONE_NUMBER intent extra.
  4. Replace this number by calling setResultData with the new number as the String argument.

Once the broadcast is finished, the result data is used as the actual number to call. If the result data is null, no call will be placed at all!

package nl.codestone.cookbook.outgoingcallinterceptor;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class OutgoingCallInterceptor extends BroadcastReceiver {                            // 1

    public void onReceive(Context context, Intent intent) {                                 // 2
        final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);          // 3
        this.setResultData("0123456789");                                                   // 4
        final String newNumber = this.getResultData();
        String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber;
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();


5. We have to register our OutgoingCallInterceptor as a <receiver> within the <application> element in the AndroidManifest.xml file.

6. We add an <intent-filter> element within this <receiver> declaration and add a android:priority of 1.

7. We add an <action> element within the <intent-filter> to only receive Intent.ACTION_NEW_OUTGOING_CALL intent actions.

8. And we have to hold the PROCESS_OUTGOING_CALLS permission to receive this intent so we register a <uses-permission> to PROCESS_OUTGOING_CALLS right below the <application> element.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />
    <application android:icon="@drawable/icon" android:label="Outgoing Call Interceptor">

        <receiver android:name="OutgoingCallInterceptor">                          // 5
            <intent-filter android:priority="1">                                   // 6
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  // 7


    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />   // 8


Now, when you try to dial the number 11111 you will actually be forwarded to 0123456789 instead!

See Also:

Do something when the phone rings, [1]


The source code for this project can be downloaded from https://github.com/downloads/jpelgrim/androidcookbook/OutgoingCallInterceptor.zip.