Click here to Skip to main content
Click here to Skip to main content

Handling Phone Events in Android Programs

By , 19 Sep 2012
Rate this:
Please Sign up or sign in to vote.


In this article, we will learn to write code which responds to various phone events such as: phone ring, start conversation, start outgoing call, etc.
The implementation of this code is done by a class which extends the BroadcastReceiver class, and overrides the onReceive method:

public void onReceive(Context context, Intent intent)

This function is called when a certain phone event occurs.

Define Permissions

The Android operating system requires to explicitly specify permission for every action that involves operating various core events and components in the Android operating systems such as: handling phone events, accessing internet connectivity, operating the inner camera of the phone and so forth. We define these permissions in the AndroidManifest.xml file (in the root directory):

<!-- permission to read and respond to an event such as phone ring, hungup Etc.  -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- permission to read and respond to an event such as outgoing calls  -->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<!-- permission to write SMS messages from your own android programs  -->
<uses-permission android:name="android.permission.WRITE_SMS" />
<!-- permission to read SMS messages from your own android programs  -->
<uses-permission android:name="android.permission.READ_SMS" />
<!-- permission to receive SMS messages from your own android programs  -->
<uses-permission android:name="android.permission.RECEIVE_SMS" />

Another thing we have to define in the AndroidManifest.xml file is an intent filter. An intent filter defines two things:

  1. That a specific class is defined as a BroadCastReciever class.
  2. That this class will respond to certain phone events such as phone call, SMS receive.

This intent filter is defined in the AndroidManifest.xml inside the <Application> element:

<!-- Here we define that the MyPhoneReciever class is a broadcastreciever -->
<receiver android:name=".MyPhoneReciever"/>
        <!-- Here we define to what events the broadcastreciever will respond to -->
        <action android:name="android.intent.action.PHONE_STATE" />
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />

The BroadcastReciever Derived Class

As I explained before, we have to build a class which is derived from the BroadcastReciever class. In that class, we override the onReceive method:

public class MyPhoneReciever extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        // gets an outgoing phone number
        String outgoingPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        // gets an incoming phone number
        String incomingPhoneNumber = 

        if (intent.getStringExtra
            // code to be executed when the phone user initiates an 
            // outgoing call or when you answer an incoming call.

        if (intent.getStringExtra
            // code to be executed when the call has ended

        if (intent.getStringExtra
            // code to be executed when the phone starts to ring

        // Get received SMS array
        Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );


What I have described here is only a sample code which explained only the fundamentals of handling various phone events. To see a full featured program which records your phone calls (incoming and outgoing), click here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Israel Israel
No Biography provided

Comments and Discussions

GeneralGreat article Pinmember| B | A | N | D | I |15-Feb-13 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 19 Sep 2012
Article Copyright 2012 by krasnoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid