Social Icons

вторник, 22 января 2013 г.

Detecting incoming and outgoing phone calls on Android.

In this article i'll show you how to detect incoming and outgoing phone calls on Android platform.
It can be useful, if you need to perform some action when call is made. For example, to block it, to log it, or send call info to a server.

Article gives the step-by-step instructions, how to create the simple demo app, that will detect incoming and outgoing phone calls, show "toast" message with phone number. You can extends and use this code for your own needs.

Incoming calls.

For incoming calls we need to use TelephonyManager class, and it's method listen, to register a listener, that will receive call state, data connection, network, sim and other events, related to telephony. We are interested only in the call state notifications.

This requires android.permission.READ_PHONE_STATE permission.

So, create our listener class, derived from PhoneStateListener, and override onCallStateChanged method, as follows:
  * Listener to detect incoming calls. 
 private class CallStateListener extends PhoneStateListener {
  public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {
          case TelephonyManager.CALL_STATE_RINGING:
          // called when someone is ringing to this phone
                  "Incoming: "+incomingNumber, 

Now i'll explain onCallStateChanged method.

First argument - state is call state, it can be CALL_STATE_RINGING, CALL_STATE_OFFHOOK or CALL_STATE_IDLE. Ringing is state when someone is calling us, offhook is when there is active or on hold call,  and idle - is when nobody is calling us and there is no active call. We are interested in ringing state.

Second argument - incomingNumber, it's number who is calling us.

As shown in code above, listener show the "toast" message with phone number, when incoming call is ringing.

Next, get instance of TelephonyManager and register listener:

  tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
  tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

When app is no longer need to receive notifications, it must unregister a listener by call:
  tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);

Outgoing calls.

For outgoing calls, system sends broadcast action android.intent.action.NEW_OUTGOING_CALL. We need to make broadcast receiver, that will receive intent with this action.

To receive this broadcast the android.permission.PROCESS_OUTGOING_CALLS permission is required.

Create broadcast receiver class:
  * Broadcast receiver to detect the outgoing calls.
 public class OutgoingReceiver extends BroadcastReceiver {
     public OutgoingReceiver() {

     public void onReceive(Context context, Intent intent) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
           "Outgoing: "+number, 

As with incoming calls, this code will show "toast" message, with phone number, when there is outgoing call.

Register the broadcast receiver:
  IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
  ctx.registerReceiver(outgoingReceiver, intentFilter);

We finished with the calls detection code. And how need to create an activity, that will enable/disable calls detection. It will be activity with simple UI, with textview showing detection status, button to enable/disable detection, and exit button.

But, here is the another issue - when our activity losses focus, calls detection is disabled. To prevent this, we have to make service, that will run  that will enable detection on start, and disable on stop.

Create the service:

 * Call detect service. 
 * This service is needed, because MainActivity can lost it's focus,
 * and calls will not be detected.
 * @author Moskvichev Andrey V.
public class CallDetectService extends Service {
    private CallHelper callHelper;
    public CallDetectService() {

    public int onStartCommand(Intent intent, int flags, int startId) {
        callHelper = new CallHelper(this);
        int res = super.onStartCommand(intent, flags, startId);
        return res;
    public void onDestroy() {

    public IBinder onBind(Intent intent) {
        // not supporting binding
        return null;

Create the activity.

Get UI elements and set onclick button handlers:
    public void onCreate(Bundle savedInstanceState) {
        textViewDetectState = (TextView) findViewById(;
        buttonToggleDetect = (Button) findViewById(;
        buttonToggleDetect.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
        buttonExit = (Button) findViewById(;
        buttonExit.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {

Create setDetectEnabled method, that will toggle calls detection:
    private void setDetectEnabled(boolean enable) {
        detectEnabled = enable;
        Intent intent = new Intent(this, CallDetectService.class);
        if (enable) {
              // start detect service 
              buttonToggleDetect.setText("Turn off");
        else {
              // stop detect service
              buttonToggleDetect.setText("Turn on");
              textViewDetectState.setText("Not detecting");


<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="15" />
         Permissions required for calls detection.
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        android:theme="@style/AppTheme" >
            android:label="@string/title_activity_main" >
                <action android:name="android.intent.action.MAIN" />

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

            android:exported="false" >


I shown you how to detect incoming and outgoing phone calls on Android. It a quite simple. Please, send me your comments and suggestions.

In the next articles, i'll show you other aspects of Android platform programming (networking, threads, multimedia processing, integration with websites and webservices, etc).

You can download source code for this article at:

23 комментария:

  1. Thanks for the post,

    In addition you can use BroadcastReceiver for incoming calls the same way as OutgoingReceiver, but using android.intent.action.PHONE_STATE

  2. Hello,
    Thank you for this tuto.
    Please, I would like to detect the time between detection of outgoing call and the listenning of dial tone.
    Thank you.

  3. Hi! I'm trying to override the incoming call screen activity. I found a solution but it seems that is not longer working on android versions api level 17+. Can you help me with a solution? Thank you! George

  4. Thanks for a nice article.

    Is it possible to know that I have received incoming call?


    Asif Iqbal

    1. Yes, you can use sources from this article to perform some actions, when incoming call is received.

  5. Thank you very much,

    Please how can I open incoming call ??

  6. how can i block a incoming call in this code?

  7. Could you please provide code sample for enabling both services using checkboxpreference? I tried without success.

  8. hy i m trying to apply password on incoming call as soon as incoming call arrives i want to show password screen then if password successfull then my login activity should hide how can i achieve this ?

  9. Hi sir . thx for nice tuto. i have same problem. that is , i want to do auto reply by sms app but when i use call log to access record then it fetch second record for outgoing but not access actually first record. is any help how to access first outgoing record...

  10. not working after close the app from recent app broadcast receiver never call until re launch the app

  11. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    Android Training institute in chennai with placement | Android Training in chennai

  12. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this development course fees in chennai | android app development training in chennai|

  13. We share this information is very impressive to me. I'm very inspired our post to writing this content style & how will be continuous topic describe for some level. Selenium Training in Chennai | Software Testing in Chennai | Hadoop Training Institute in Chennai | Best Java Training in Chennai

  14. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.