Show alert dialog when a call is received

1

I'm making an app that when receiving a call gets the number of the incoming call, looks for it in a BD, and shows a message with the data associated with that number. So far, I notified it with a Toast, I want it to show something like an alert dialog but it does not work, how can I do this? ...

with this class I get the incoming call number:

public class MyCallReceiver extends BroadcastReceiver {
    String incomingNumber;


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

        //verfica que esta entrando una llamada y gruada el número...
        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            MiBaseDatos MDB = new MiBaseDatos(context);
            String numero = null;
            if (incomingNumber.charAt(0) == '+') {
                numero = incomingNumber.substring(3);
            }
            if (incomingNumber.charAt(0) == '9') {
                numero = incomingNumber.substring(4, 12);
            }
        Toast.makeText(context, "LLama de: " + MDB.recuperarPERSONA(numero).getName() + " " + incomingNumber, Toast.LENGTH_LONG).show();


//            AlertDialog.Builder builder = new AlertDialog.Builder(context);
//
//                    builder.setTitle("LLamada Entrante de:")
//                    .setMessage(MDB.recuperarPERSONA(incomingNumber).getName() + " " + incomingNumber)
//                    .setIcon(R.mipmap.ic_launcher)
//                    .show();


        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            Toast.makeText(context, "LLamada finalizada", Toast.LENGTH_LONG).show();
        }   

    }

Here I show the message correctly with the class tosat but when I replace it with the alert dialg that this comment does not work ....

    
asked by Felix A Marrero Pentón 25.03.2017 в 22:41
source

2 answers

0

Try this:

                String msj2="¿DESEA CONTINUAR?\n";
                AlertDialog.Builder dialogK2= new AlertDialog.Builder(micontexto);
                dialogK2.setTitle("ALERTA");
                dialogK2.setMessage(msj2);
                dialogK2.setCancelable(true);
                dialogK2.setNegativeButton(R.string.cancel, null);
                dialogK2.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //ejecuto mi codigo
                    }
                });
                dialogK2.show();
    
answered by 26.03.2017 в 06:20
0

I think that in some operating systems you will have to see the screen that shows the incoming call, since it has a higher priority.

Something important for the AlertDialog to be shown is to ensure that the context is that of Activity .

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context);

// Configura el titulo.
alertDialogBuilder.setTitle("LLamada Entrante de:");

// Configura el mensaje.
    alertDialogBuilder
            .setMessage(MDB.recuperarPERSONA(incomingNumber).getName() + " " + incomingNumber)
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // Acción boton 
            }
        })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // Cancela dialogo
                dialog.cancel();
            }
        }).create().show();
    
answered by 26.03.2017 в 06:39