35

I have a requirement wherein I want to detect two kind of events related to Calls in Android

  1. Whenever an outgoing call is made, my application should get to know this along with the called number
  2. When the call is hanged up(due to success/failure), my application should get to know this along with the reason of hangup

Is this possible in Android?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user669231
  • 1,371
  • 3
  • 18
  • 27

3 Answers3

34

You should create a BroadcastReceiver:

public class CallReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

                // Phone number 
                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

                // Ringing state
                // This code will execute when the phone has an incoming call
        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            // This code will execute when the call is answered or disconnected
        }

    }
}

You should register you application to listen to these intents in the manifest:

<receiver android:name=".CallReciever" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
 </receiver>
Hardik
  • 17,179
  • 2
  • 35
  • 40
vendor
  • 1,854
  • 13
  • 8
  • Thanks for the sample code Is there a way for capturing the Outgoing call as well similar to the way Incoming call is captured? – user669231 Mar 31 '11 at 10:41
  • @user669231 Yes, you should change the check in the if-state. You can see this topic: http://stackoverflow.com/questions/2668445/detect-outgoing-calls-and-problem-with-a-real-device – vendor Mar 31 '11 at 10:59
  • 1
    Works like a charm. Additionally you have to declare the following permission in the manifest.xml file: otherwise this won't work. See API of TelephonyManager class: http://developer.android.com/reference/android/telephony/TelephonyManager.html – qupera Oct 08 '14 at 09:19
  • 1
    The code is executing twice that is written in `else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) { }` block. In this block I am getting details of outgoing calls. Please suggest why is this happening ?? – Ashish Tiwari Jan 05 '15 at 11:08
  • 1
    the same problem described in the above comment is happened to me. Somebody help us to solve the problem of getting the same phonestate in multiple times. – ARUNBALAN NV Oct 09 '15 at 11:49
  • but still not able to detect that call is received by recipient. – Dhanraj Mar 07 '21 at 18:30
9

There is a simpler solution using only TelephonyManager and PhoneStateListener.You don´t even have to register a BroadcastReceiver.

public class MyPhoneStateListener extends PhoneStateListener {

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            //Hangup
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            //Outgoing
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            //Incoming
            case TelephonyManager.CALL_STATE_RINGING:
                break;
        }
    }
}

And to register it:

public static void registerListener(Context context) {
    ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).listen(new MyPhoneStateListener(),
            PhoneStateListener.LISTEN_CALL_STATE);
}
rickythefox
  • 6,601
  • 6
  • 40
  • 62
Marian Klühspies
  • 15,824
  • 16
  • 93
  • 136
4

You need to create a receiver for the following intent actions:

  1. Outgoing call - ACTION_NEW_OUTGOING_CALL
  2. Call hangup - ACTION_PHONE_STATE_CHANGED
Nic Strong
  • 6,532
  • 4
  • 35
  • 50
  • Can you give me a example for that, My need for outgoing call – Ashish Dwivedi Dec 24 '12 at 11:47
  • The above described solution worked perfectly for me.But the receiver is receiving the same state for multiple times. How can we solve this?. Is there anyway to detect the id of receiving intent or some other ways? – ARUNBALAN NV Oct 09 '15 at 11:47