Monday, November 26, 2012

How To Handle Incoming Calls in Android

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

Handle Incoming Calls in Android

In this Post I will describe how to Handle incoming Calls on Android and Reacting to them i.e. receiving, rejecting etc.

We  can do this by Activity and by Broadcast Receiver, here we will do using Activity.

As I mentioned in my earlier posts we require to declare permission in manifest file.
For listening and reacting to Incoming calls we need following permissions

android.permission.READ_PHONE_STATE   to listen to InComing Calls
"android.permission.MODIFY_PHONE_STATE"   to receive Incoming Calls

You can learn more about permission  Here.

Add these permissions in your manifest file.
Your Manifest file should look like

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

    <uses-sdk android:minSdkVersion="8"
              android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
       <uses-permission android:name="android.permission.READ_PHONE_STATE" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


How to Get IMEI number of the Phone

Some More Good  Android Topics
Customizing Toast In Android 
 Showing Toast for Longer Time
Customizing Checkboxes In Android 
Customizing Progress Bar
To learn Basic of Android Animation  go to  Android Animation Basics


We need to use TelephonyManager class and  TELEPHONY_SERVICE.

The onCallStateChanged handler receives the phone number associated with incoming calls, and the
state parameter represents the current call state as one of the following three values:

➤ TelephonyManager.CALL_STATE_RINGING When the phone is ringing








➤ TelephonyManager.CALL_STATE_OFFHOOK When the phone is currently in a call means call is recieved






➤ TelephonyManager.CALL_STATE_IDLE When the phone is neither ringing nor in a call













How to Monitor Incoming Calls:

We need to have a listener to listen the InComing Calls

PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
// TODO React to incoming call.


public class GetCallerInfoActivity extends Activity

            public void onCreate(Bundle savedInstanceState)
                    TelephonyManager telephonyManager =
               // register PhoneStateListener
               PhoneStateListener callStateListener = new PhoneStateListener() {
                    public void onCallStateChanged(int state, String incomingNumber)
                            //  React to incoming call.

                             // If phone ringing
                                    Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();

                              // If incoming call received
                                Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
                                Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();


How To Launch the Dialer to Initiate Phone Calls:

Use an Intent action to start a dialer activity; you should specify the number to dial using the tel: schema as the data component of the Intent.

Use the Intent.ACTION_DIAL Activity action to launch a dialer rather than dial the number immediately.
This action starts a dialer Activity, passing in the specified number but allowing the dialer application
to manage the call initialization (the default dialer asks the user to explicitly initiate the call). This
action doesn’t require any permissions and is the standard way applications should initiate calls.

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567"));


New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views

Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time

TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android

Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog


  1. do you know how to handle call forwarding in android programming ?

    1. In Android an Incoming Call can be forwarded with below Code

      String url = "tel:"+"**21*"+ phoneNumberToForward+Uri.encode("#");
      Intent intent1 = (new Intent(Intent.ACTION_CALL, Uri.parse(url)));

      This will activate the Call Forwarding to Given Number(phoneNumberToForward)

      To Reset/Cancel The Call Forwarding:

      String url = "tel:"+"##21#"+ phoneNumberToForward+Uri.encode("#");
      Intent intent1 = (new Intent(Intent.ACTION_CALL, Uri.parse(url)));

    2. can you help me please, by ur way in call forwarding , the cost of calling will be for my android phone not for the caller ?!
      i tried it but it doesn't work with me

    3. What help you need pls clarify, the cost of call forwarding will be borne by you and not by the caller

  2. where you are calling "GetCallerInfoActivity",i mean where you used intent to call it? if i want when any incoming call appears a toast should be displayed then how can i do it?

    1. GetCallerInfoActivity is launcher activity, it automatically gets called whenever there ia an incoming call.

      We can also add a Incoming Call broadcast reciever, that will receive and handle all the incoming calls.

    2. could you explain this a little more?
      I cant figure out, the difference of using and not usingthe broadcast recvr class.

  3. hiiiiiiiiiii
    please tell me the purpose of
    and the methods in it and where it implemented

    1. ITelephony interface is an AIDL interface, which are used for IPC between different Android processes. It is also used for rejecting/blocking a call and for many other purposes as well.

  4. I would like to ask, what's better, using the broadcast receiver or this.?

  5. I want to know does this app records the call when the application is not open in UI

  6. does this app will record call when this is not in UI if not please tell me how to create an application which will record call and runs always in background.

  7. Hi,
    I'm getting error for the following line in manifest file.

    Errors says that only system apps have the above permission.
    How to solve this. Any help is appreciated.
    Thanks in advance.

    1. HI Pavan
      It is not true that only system apps can have the above permission, User apps can also have those permissions.
      There could be some other problem.
      Please paste the code in which you are getting error. I will try to solve your problem.

  8. how can i get the call duration

    1. To get the Call Duration you need query to Call Logs table through proper URI.

  9. please post the toturial how we can reject the incomming call and how to import

  10. can anybody share with me code to tape a call in android

  11. Great!! Got clear explanation to Handle Incoming Calls in Android..Android Training

  12. Great site. I have read your article. really your have shared your Idea. I think it is the best phone calls site. Everyone can understand everything easily. For more info click this site about telephone number search.

  13. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. 카지노사이트

  14. Hi,
    Thanks for sharing the information with us it was very informative.