This example code shows a listener to a outgoing and incoming calls to device. I wanted to handle something on both incoming and outgoing of calls and wanted to revert to normal state when call was dropped.
This uses a PhoneStateListener : A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.
public class CallListener extends PhoneStateListener { @Override public void onCallStateChanged (int state, String incomingNumber) { if(TelephonyManager.CALL_STATE_RINGING == state) { //Incoming call handling } if(TelephonyManager.CALL_STATE_OFFHOOK == state) { //Outgoing call handling } if(TelephonyManager.CALL_STATE_IDLE == state) { //Device back to normal state (not in a call) } } }
Do not forget to add following permission(s) in your manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
External References : http://developer.android.com/reference/android/telephony/PhoneStateListener.html