Detecting incoming and outgoing calls in Android

Download source code of this article

In one of my project, I wanted to detect incoming and outgoing calls. I used BroadcastReceiver with action android.intent.action.NEW_OUTGOING_CALL and android.intent.action.PHONE_STATE.

The android.intent.action.NEW_OUTGOING_CALL will be broadcasted when an outgoing call is initiated. The receiving intent will have an extra string variable Intent.EXTRA_PHONE_NUMBER which contains the outgoing number. This requires the permission android.permission.PROCESS_OUTGOING_CALLS.

To detect the incoming call, we register a BroadcastReceiver for the action android.intent.action.PHONE_STATE. This will be broadcasted when there is a change in phone state. The receiving intent will have an extra string variable TelephonyManager.EXTRA_STATE which describes the phone state. If this state is TelephonyManager.EXTRA_STATE_RINGING then there will be another extra string variable TelephonyManager.EXTRA_INCOMING_NUMBER. This variable contains the incoming phone number. Note that this variable will not be present when the state is not TelephonyManager.EXTRA_STATE_RINGING.

The sample application is a simple one just for demonstration. The application registers for these actions and when the calls are detected it just displays a toast.

8 Responses

  1. Pinakin Shah April 27, 2011 / 5:59 am

    Hello Krishna,

    I have a broadcast receiver that listens to Phone State. I am trying to get the last call log entry when the phone state is idle but it always gives me the previous entry.

    Seems the call log is updated later in the change. I tried setting the priority as 0 so that it is the last receiver.

    Something similar to

    Any idea?

  2. Aditya April 17, 2012 / 7:06 am

    Your code for outgoing call is not working .Its giving null value

  3. Aditya April 17, 2012 / 7:08 am

    Your code for getting outgoing call is not working.It returns null instead of some number.

  4. P C Varma March 16, 2014 / 10:29 am

    I am searching for 2 things
    1. Auto Answer the call, then play a Pre-defined Audio file to the Record the cal-lee voice as Audio file.
    2. Auto Answer the call and play Pre-defined Audio files based on the cal-lee Touch tones/DTMF tones.

    Can you help me? Thanks in Advance.

  5. sayam October 7, 2015 / 3:29 pm

    I have download your code but in need to know how to detect call has been canceled by user

  6. sanjay December 9, 2015 / 5:39 pm

    I am using custom ringtone during outgoing call’s in my application.
    How to make outgoing call ringtone in earphone (without speaker ) in android.
    Please share your suggestions and views into this.

    I have written AudioManager for speaker off-
    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    and my custom ringtone code here-
    public void startRing(Context context) {

    AssetFileDescriptor afd;
    try {
    afd = context.getAssets().openFd(“test1.mp3”);
    afd.getStartOffset(), afd.getLength());
    } catch (Exception e) {
    // TODO: handle exception


  7. JR April 1, 2016 / 6:23 am

    Hi Krishna,

    I have developed an app which receive messages via WFI and alert the user by audio sound and vibration. It works fine. What I want to achieve is that when there a is voice call active my app to sound only vibration alert. Voice call can be VOIP call or Mobile voice calls.
    Is there way to use PHONE_STATE to detect when voice call is on progress???

    Thank you Jo

Leave a Reply

Your email address will not be published. Required fields are marked *