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 Responses to “Detecting incoming and outgoing calls in Android”

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

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

    Reply
  3. P C Varma

    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

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