I'm looking through Android programming to know when a call is rejected or when a call is sent to voicemail.
I am also looking for how I can know when the call is connected to the telephone service provider
I'm looking through Android programming to know when a call is rejected or when a call is sent to voicemail.
I am also looking for how I can know when the call is connected to the telephone service provider
What we can do is get when the phone is ringing and it is not answered, when you are dialing a number or when you cancel the call, and to get the number that sent the voice message you could use getVoiceMailNumber()
of TelephonyManager (may not work for all providers)
This requires READ_PHONE_STATE
permission in your manifest, as detailed by HERE
To implement the first and know when you miss a call, when it is ringing or when you are dialing we can do the following
We create a class that is a listener that listens when one of these things happens, called missed, call in progress, or answering a call
int prev_state=0;
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomPhoneStateListener";
@Override
public void onCallStateChanged(int state, String incomingNumber){
if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
NumberDatabase database=new NumberDatabase(mContext);
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){
prev_state=state;
//Llamada contestada que termino
}
if((prev_state==TelephonyManager.CALL_STATE_RINGING)){
prev_state=state;
//Llamada cancelada o perdida
}
break;
}
}
}
CALL_STATE_RINGING
Called when the phone is ringing
CALL_STATE_OFFHOOK
It is called when we are dialing some number
CALL_STATE_IDLE
Called when a call is rejected or not answered
Then you must make a class that extends to BrodcastReceiver
to get this data with a method onReceive()
public class IncomingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager objecto
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Registramos nuestro listener con TelephonyManager
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
mContext=context;
}
I hope you serve