In the manifest file you should have the following permissions to check the outgoing call:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
and the code of broadcast receiver:
public class PhoneStateChecking extends BroadcastReceiver {
boolean hasCallStateRinging = false;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Intent", intent.toString());
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
Log.e("CAll-STATUS", "out going Call");
}
// Checking for the call status
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
if (hasCallStateRinging)
return;
else {
hasCallStateRinging = true;
Log.e("CAll-STATUS", "CALL_STATE_RINGING");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(!hasCallStateRinging){
hasCallStateRinging = true;
Log.e("CAll-STATUS", "CALL_STATE_OFFHOOK");
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (hasCallStateRinging) {
hasCallStateRinging = false;
Log.e("CAll-STATUS", "CALL_STATE_IDLE");
}
break;
}
}
}
}