1

Mi aplicación por medio de un Intent

Intent intent = new Intent(Intent.ACTION_CALL);
intent.setPackage("com.android.server.telecom");
intent.setData(Uri.parse("tel:" + mobile));
context.startActivity(intent);
Toast.makeText(context, "Llamando a: "+nombre, Toast.LENGTH_LONG).show();

Realizo una llamada, pero necesito saber cuando la llamada ya termino para mostrar una alerta (Específicamente para calificar la llamada), Existe alguna manera, bueno creo que si debe existir ya que

Truecaller realiza algo simimilar a lo que yo estoy necesitando

1
  • Hola Wilson, no se puede detectar esto mediante un Intent, en este caso debes usar un BroadcastReceiver, @WilsonCajisaca revisa la respuesta, saludos.
    – Jorgesys
    Commented el 1 ago. 2019 a las 19:52

1 respuesta 1

4

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>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.