Search code examples
androidbroadcastreceivertelephonymanager

How to know when call is made and connected?


hello im using a broadcast reciever to try to find out when a call is "made" and after the call is "made" also when the made call is "connected" i searched around and came up with this code but this works when recievng a call and making a call, all i really need is when the call is made and connected, any help in guiding me in this is appreciated, thank you for your time

receiver = new BroadcastReceiver() {
               @Override
               public void onReceive(Context context, Intent intent) {
                  String action = intent.getAction();
                  TelephonyManager telephonyManager = (TelephonyManager) context
                          .getSystemService(Context.TELEPHONY_SERVICE);

                  if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                       //action for phone state changed
                      Toast.makeText(context, "Receiver call status", Toast.LENGTH_SHORT).show();
                      Log.d("reciever","entered keep going");
                      if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
                          Toast.makeText(context, "Receiver call connected", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(context, "Receiver call not connected", Toast.LENGTH_SHORT).show();  
                        } 
                  }     
               }
            };
            IntentFilter filter = new IntentFilter();

            filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);

            registerReceiver(receiver,filter);

Solution

  • Here you can find all states of call, just make handle this states, like i did:

    public class PhoneState extends PhoneStateListener {
    
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                //Some Action
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                //Some Action
                break;
            //case TelephonyManager.ANOTHER_STATE: ...
        }
        }
    }