Get Started

Twilio Client SDK for Android Quickstart

Hanging Up Calls

Congrats on making your first audio connection from your Android device to Twilio!

You should have heard a welcome message, but as you probably noticed, you had no way to close the connection, as the Hangup button doesn't do anything. So, let's add some functionality to do that. That'll be two steps:

  1. Making MonkeyPhone able to disconnect an active connection, and
  2. Hooking up our second button in the UI

Since MonkeyPhone is the object talking to the Twilio Client API, let's add a disconnect() method there:

MonkeyPhone.java
public class MonkeyPhone implements Twilio.InitListener
{
    private Device device;
    private Connection connection;

    /* ... other methods ... */

    public void disconnect()
    {
        if (connection != null) {
            connection.disconnect();
            connection = null;
        }
    }

    /* ... other methods ... */
}

And add code in HelloMonkeyActivity to call this new method when the hangup button is pressed:

HelloMonkeyActivity.java
public class HelloMonkeyActivity extends Activity implements View.OnClickListener
{
    private MonkeyPhone phone;

    /* ... other methods ... */

    @Override
    public void onClick(View view)
    {
        if (view.getId() == R.id.dialButton)
            phone.connect();
        else if (view.getId() == R.id.hangupButton)
            phone.disconnect();
    }
}

That's everything! Go ahead and make another call. You can now press the hangup button at any time and your connection will close.


Next: Set Up Your Twilio Application »