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.

5 Comments

  1. 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 http://www.anddev.org/accessing_call_log_on_incoming_call_and_outgoing_calls-t4408.html

    Any idea?
    Pinakin

    Reply

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

    Reply

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

    Reply

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

    Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>