Get Started

Twilio Client SDK for Android Quickstart

Passing Parameters to Your Application

So you've created a Twilio Application and connected to it from your Android device -- frickin' sweet! Go get yourself a drink and celebrate!

Now, let's make a Twilio Application that can actually do something useful by passing data from your Android app to your TwiML script. The most useful thing we could think of was... dialing 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 the hello-client-monkey-twiml.php to serve a TwiML that contains the <Dial> verb with the phone number you passed in from your Android device.

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 PhoneNumber. Then pass that parameter Map when calling Device.connect():
import java.util.HashMap;
import java.util.Map;

/* ... other imports... */

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

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

    public void connect(String phoneNumber)
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("PhoneNumber", phoneNumber);
        connection = device.connect(parameters, null /* ConnectionListener */);
        if (connection == null)
            Log.w(TAG, "Failed to create new connection");

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

And finally, in HelloMonkeyActivity, pass the phone number from the text field to the updated MonkeyPhone.connect() method.
public class HelloMonkeyActivity extends Activity implements View.OnClickListener
    private MonkeyPhone phone;
    private EditText numberField;

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

    public void onClick(View view)
        if (view.getId() ==
        else if (view.getId() ==

Now, the hello-client-monkey-twiml.php will receive a PhoneNumber parameter in the request when it's invoked from your app. Change hello-client-monkey-twiml.php to read the PhoneNumber parameter and print that value into the <Dial> verb. Note that <Dial> requires a callerId attribute when calling out to regular ol' phone numbers. You'll need to provide a number that you've verified with Twilio.

header('Content-type: text/xml');

// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "XXXXXXXXXX";
    <Dial callerId="<?php echo $callerId ?>"> 
        <Number><?php echo $_REQUEST["PhoneNumber"]; ?></Number>

So, go ahead and build your app, run it, enter your cell phone number in the box, and hit dial. Your phone will ring and you can talk to yourself. Huzzah!

Now, it's about time apps started calling YOU. Keep reading.

Next: Allowing Incoming Connections »