'How obtain calling event from my app Android?

I want to capture incoming and outgoing calls from my App, that is, while it is running, if a phone call is made or comes in that can be captured, I have implemented the BroadcastReceiver class and the PhoneStateListener class, but it is not capturing it, or perhaps the way to invoke her. I have added the permissions in the Manifest, I also invoke the Receiver with the name of the BroadcastReceiver class called "Ena_Llamadas_Manager"

This is the class below:

@SuppressLint("SimpleDateFormat") public class Ena_Llamadas_Manager  extends BroadcastReceiver {

private static Context contexto;

@Override
public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();
    if(extras != null){

        call(context,extras);
    }

    if ( extras.getString("state").equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        //codigo
    }
}

public static void call(Context context, Bundle extras) {
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    contexto = context;
    //String phoneNumber = extras.getString("incoming_number");

}

private static class PhoneCallListener extends PhoneStateListener {
    public boolean isPhoneCalling = false;
    Boolean wasRinging = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        //Bundle extras = intent.getExtras();

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            //Aquí ya detectas que el teléfono esta recibiendo una llamada entrante

        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            isPhoneCalling = true;

            AlertDialog.Builder builder = new AlertDialog.Builder(contexto);
            builder.setMessage("Llamada saliente...")
                    .setCancelable(false)
                    .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {

            isPhoneCalling = false;

        }

    }
}

}

And this is the Manifest permisson:

 <receiver android:name="etapa.webServices.inec.go.cr.Ena_Llamadas_Manager" >
        <intent-filter
            android:priority="1">
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>

Finally this is the way tha invoke the class from the Main Activity

Ena_Llamadas_Manager.call(LoginActivity.this, getIntent().getExtras());


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source