Detectar cuando una llamada recibida termino en Android.
Tienes que realizarlo mediante un BroadcastReceiver
, define una clase que va a ser llamada mediante el intent-filter :
<receiver android:name=".PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
La clase extiende de BroadcastReceiver
e implementa el método onIncomingCallEnded()
, el método onIncomingCallEnded()
cual determina cuando una llamada recibida termino:
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
}
Si deseas detectar cuando una llamada que tu realizas termino, usa el método onOutgoingCallEnded()
:
protected void onOutgoingCallEnded(Context ctx, String savedNumber, Date callStartTime, Date end) {
}
Esta sería la clase completa:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.Date;
public class CallReceiver extends BroadcastReceiver {
private static String TAG = "myCallReceiver";
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
public CallReceiver() { }
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
Log.d("onIncomingCallStarted()",number);
}
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
Log.d(TAG, "onOutgoingCallStarted() number: " + number);
}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
Log.i(TAG, "onIncomingCallEnded() : (savedNumber: "+ number + ", callStartTime: " +start.toString());
}
protected void onOutgoingCallEnded(Context ctx, String savedNumber, Date callStartTime, Date end) {
Log.i(TAG, "onOutgoingCallEnded() : (savedNumber: "+ savedNumber + ", callStartTime: " +callStartTime.toString());
}
protected void onMissedCall(Context ctx, String incomingNumber, Date start) {
Log.i(TAG, "onMissedCall() : (savedNumber: "+ incomingNumber + ", callStartTime: " +start.toString());
}
public void onCallStateChanged(Context context, int state, String number) {
if(lastState == state){
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(lastState == TelephonyManager.CALL_STATE_RINGING){
onMissedCall(context, savedNumber, callStartTime);
}
else if(isIncoming){
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
}
else{
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}
else{
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
Log.i(TAG, "onReceive() : (savedNumber: "+ number + ", stateStr: EXTRA_STATE_IDLE)");
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
Log.i(TAG, "onReceive() : (savedNumber: "+ number + ", stateStr: CALL_STATE_OFFHOOK)");
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
Log.i(TAG, "onReceive() : (savedNumber: "+ number + ", stateStr: CALL_STATE_RINGING)");
}
onCallStateChanged(context, state, number);
}
}
}
Para que sea llamado el BroadCastReceiver
debes configurarlo en tu AndroidManifest.xml
definiendo los permisos:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
Ejemplo:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
...
...
...
<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>