Twilio Client for Android Quickstart Tutorial

Passing Parameters to your Application

Now, let's make a Twilio Application that can actually do something useful by passing data from your Android app to your TwiML script. Let's dial out to an arbitrary phone number. It'll be easy, all we need to do is:

  1. Grab the phone number that you can enter in the text field.
  2. Pass that phone number along to the Device.connect() method.
  3. Update your TwiML Application to point to the back-end endpoint that receives this input and dials the number entered by the user.

Let's go do it.

The text field in the app is wired up to the member variable numberField in HelloMonkeyActivity. We'll now add some code to pass this through to your script when creating the connection.

First, change the MonkeyPhone.connect() method to take in a String for the phone number in, and add the number to a Map using the key To. Then pass that parameter Map when calling Device.connect():
public void connect(String phoneNumber)
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("To", phoneNumber);
    connection = device.connect(parameters, null /* ConnectionListener */);
    if (connection == null)
        Log.w(TAG, "Failed to create new connection");

And finally, in HelloMonkeyActivity, pass the phone number from the text field to the updated MonkeyPhone.connect() method.
public void onClick(View view)
    if (view.getId() == {
    } else if (view.getId() == {

Now, let's change our TwiML Application to point to our app's /call endpoint. Go to the TwiML apps page in the account portal, change your application's Voice URL to point to your server's /call endpoint: http://{your server url}/call, and save your changes.

Now, go ahead and compile your app for the Android Emulator. Run it, enter another phone number in the box and hit dial. Your phone will ring and you can talk to yourself! Huzzah!

Here's how it works. When your application connects to Twilio, your TwiML App's Voice URL points to the /call endpoint on your python server, which runs the following code:

@app.route('/call', methods=['GET', 'POST'])
def call():
  resp = twilio.twiml.Response()
  from_value = request.values.get('From')
  to = request.values.get('To')

  if not (from_value and to):
    return str(resp.say("Invalid request"))

  from_client = from_value.startswith('client')
  caller_id = os.environ.get("CALLER_ID", CALLER_ID)

  if not from_client:
    # PSTN -> client
  elif to.startswith("client:"):
    # client -> client
    # client -> PSTN
    resp.dial(to, callerId=caller_id)

  return str(resp)

This code looks at the From parameter in the request from Twilio. If the connection is coming from your Android application, it grabs the To parameter passed by your application and dials the number.

Things work a bit differently if the call is coming from a PSTN phone number. Let's dig into that now: Now, it's time to get your app ringing.

Next: Receiving Incoming Calls »