You need to implement Phonecall state aside from your activity.
//// Processing phone states
private PhoneStateListener phoneListener = new PhoneStateListener() { public void onCallStateChanged(int state, String incomingNumber) { try { switch (state) { case TelephonyManager.CALL_STATE_RINGING: Toast.makeText(activity.this, "CALL_STATE_RINGING", Toast.LENGTH_SHORT).show(); mediaPlayer.pause(); break; case TelephonyManager.CALL_STATE_OFFHOOK: Toast.makeText(activity.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show(); mediaPlayer.pause(); break; case TelephonyManager.CALL_STATE_IDLE: Toast.makeText(activity.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show(); mediaPlayer.start(); break; default: Toast.makeText(activity.this, "default", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } } };
}
source share