How to get notified during an incoming call and get that number inside your program in ANDROID?

By | April 24, 2011

Hi all…….

In today’s tutorial I will show you how to get the incoming phone number inside your application. In many situation we may need this.
For this we need to have BoradCastReceivers. Inside this class we create an object of the TelephonyManager class ang register with the sytem service.Now we have to make another class which extends PhoneStateListener class which implements the onCallStateChange class which listens to incoming calls.The onReceive method inside the BroadcastReceiver class gets the incoming number from the intent using this line

Bundle bundle = intent.getExtras();
String phone_number = bundle.getString(“incoming_number”);

Note that this program doesnot have an UI ,since it is a service.

Now we go to the code part.
Create a new project and copy this code to it.

package pack.coderzheaven;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class MyBroadcastReceiver extends BroadcastReceiver {

	public void onReceive(Context context, Intent intent) {

	    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
	    MyPhoneStateListener customPhoneListener = new MyPhoneStateListener();

	    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

	    Bundle bundle = intent.getExtras();
	    String phone_number = bundle.getString("incoming_number");
	    System.out.println("Phone Number : " + phone_number);


Now create another class and name it and copy following code to it.

package pack.coderzheaven;

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class MyPhoneStateListener extends PhoneStateListener {

	public void onCallStateChange(int state, String incomingNumber){

	       System.out.println("Icoming Number inside onCallStateChange : "  + incomingNumber);
	                case TelephonyManager.CALL_STATE_RINGING:
	                		System.out.println("PHONE RINGING.........TAKE IT.........");
	                case TelephonyManager.CALL_STATE_OFFHOOK:

Note that all receivers have to be declared in the androidmanifest file inorder to get events.
So the androidmanifest.xml is given below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.WRITE_CONTACTS" />
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />

      <application android:icon="@drawable/icon" android:label="@string/app_name">
    	        <receiver android:name=".MyBroadcastReceiver">
		                <action android:name="android.intent.action.PHONE_STATE" />


Now what you have to do is to simulate a call to your emulator. Check these links for this

1. Simulating a call or SMS in your ANDROID Emulator

2. How to simulate an incoming call to an emulator OR Send an SMS to an emulator in ANDROID ?

Check your console for the output.

Please leave your comments if this post was useful.

4 thoughts on “How to get notified during an incoming call and get that number inside your program in ANDROID?

  1. Swati


    I tried your example. Everything seems working fine but the code does not gets into It does not print ” Icoming Number inside onCallStateChange”. I wanted to ask if there is some tag this should be given in the maifest file. I am sorry if the solution is trivial but am very new to android.
    Any help would be great 🙂

  2. Karan

    public void onCallStateChange is not a overrided method….

    public void onCallStateChange()


    public void onCallStateChanged()

  3. Dhuna

    your example is work perfact but app is not in background at that time receiver not work in marshmallow
    please help for solve this problem

    1. James Post author

      Dhuna, I believe Marshmallow has more restrictions and need more permission. I need to check that.



Leave a Reply

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