How to make a phone call in android and come back to my activity when the call is done?

Viewed 224753

I am launching an activity to make a phone call, but when I pressed the 'end call' button, it does not go back to my activity. Can you please tell me how can I launch a call activity which comes back to me when 'End call' button is pressed? This is how I'm making the phone call:

    String url = "tel:3334444";
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
21 Answers

To call from app use simple intent for call, after that if you want to listen the call status then use below code

1] implement this in your class -

implements AudioManager.OnAudioFocusChangeListener

2] Add below code with overridden method

    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); //public static final String AUDIO_SERVICE = "audio";
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    assert audioManager != null;
    audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);

@Override
public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS: {
                //here you can pause your playing audio
                break;
            }
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: {
                 //here you can pause your playing audio
                break;
            }
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
                break;
            }
            case AudioManager.AUDIOFOCUS_GAIN: {
                 //here you will return back to the activity when your call is finsihed
               //resume your audio here
                break;
            }
        }
    }

3]Add this

@Override public void onDestroy() { audioManager.abandonAudioFocus(this); }

Related