Get Started

Twilio Client for Android Quickstart Tutorial

Making Calls in/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 do this, we'll need to log in as two different users. We'll change the login 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 add some conditional-compilation code:

MonkeyPhone.java
@Override
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("http://companyfoo.com/token?clientName=" + clientName);
        device = Twilio.createDevice(capabilityToken, this);

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

The Python script has some simple call routing rules:

  • If the call is coming from PSTN, the app tells to Twilio to dial Client jenny.
  • Otherwise, the application tells Twilio to dial whatever number is passed in the To parameter: If the To parameter is a PSTN number, it writes that out within the <Number> noun (which then will call a regular phone). If the To parameter is another client name, as indicated by client: prefix, it writes out a <Client> noun, 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 "client: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! And of course, you can call regular phone numbers as we demonstrated above.

Wrapping up

That's it for this Quickstart! If you want to learn more about how to customize the telephony experience of your application, check out the TwiML documentation.