Get Started

Twilio Client SDK for Android Quickstart

Making Calls In and Out of Your App

Time to put it all together... using your Quickstart skills, you've already initiated an outgoing connection, and received an incoming connection. Now, let's call from one client to another.

For this example, we'll make a call between the Android Emulator and your device. To accomplish this, we'll need to:

  1. Assign a different client name to the Emulator and your device,
  2. Pass the correct client name to the auth PHP script, and
  3. Modify our server-side PHP to use the correct noun inside the <Dial> verb

First we'll change the auth code to register as "tommy" for the app in the Android Emulator, and "jenny" for the app on your Android device.

In MonkeyPhone's onInitialized() method, add some code to figure out if we're running in the emulator or not:
public class MonkeyPhone implements Twilio.InitListener, DeviceListener
    /* ... other methods ... */

    /* Twilio.InitListener method */
    public void onInitialized()
        Log.d(TAG, "Twilio SDK is ready");

        try {
            // the Emulator has a somewhat unique "product" name
            boolean isEmulator = Build.PRODUCT.equals("sdk") || Build.PRODUCT.equals("google_sdk");
            String clientName = isEmulator ? "tommy" : "jenny";
            String capabilityToken = HttpHelper.httpGet("" + clientName);
            device = Twilio.createDevice(capabilityToken, this);

            Intent intent = new Intent(context, HelloMonkeyActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        } catch (Exception e) {
            Log.e(TAG, "Failed to obtain capability token: " + e.getLocalizedMessage());

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

Next we'll change the TwiML PHP file to connect your two clients together. So, change the file hello-client-monkey-twiml.php to:

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

$number = $_REQUEST["PhoneNumber"];

// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "XXXXXXXXXX";
    <Dial callerId="<?php echo $callerId ?>">
    <?php if (preg_match("/^[\d\(\)\-\+ ]+$/", $number)) { ?> 
        <Number><?php echo $number;?></Number>
    <?php } else { ?> 
        <Client><?php echo $number;?></Client>
    <?php } ?>

What the above PHP program does is simply to check whether the PhoneNumber is only digits, and if so, write that out within the <Number> noun (which then will call a regular phone), otherwise write out a <Client> noun instead, which will try and connect to a named Twilio Client app (a browser or another mobile client).

So now build, install, and run your app on both your Android device and the Android Emulator. Then have one of the platforms call the other (e.g. by typing "tommy" into the Android device's app). Once the connection has been established you should be able to talk between your phone and your laptop!

You can also dial regular phone numbers from your Android app as long as you replace the $callerId value in the PHP above with a phone number that's verified with your Twilio Account. See the "Verified Numbers" section under the Numbers tab in your Twilio Account.

That's it for this Quickstart Guide!