In this guide we'll show you how to manipulate live phone calls using Java. We'll also cover how to retrieve information about in progress and completed calls from your Twilio account.
We'll use Twilio's Java SDK in these examples to interact with the Twilio REST API's Calls endpoint.
Modifying Twilio phone calls in progress with Java
The simplest way to control the flow of a Twilio phone call is with TwiML itself.
You can use the "action" parameters of verbs like <Gather> and <Record> to tell Twilio to get new instructions from your applications during a call. You can also use the <Redirect> verb to explicitly tell Twilio to fetch new TwiML.
But sometimes you need to change a live phone call outside of Twilio's normal request-response cycle. For those cases you can update the Call to tell Twilio to immediately change the TwiML it's using in a phone call.
Update an in progress Call with new TwiML instructions
Java
_22
// Install the Java helper library from twilio.com/docs/java/install
_22
_22
import com.twilio.Twilio;
_22
import com.twilio.rest.api.v2010.account.Call;
_22
import com.twilio.type.Twiml;
_22
_22
public class Example {
_22
// Find your Account SID and Auth Token at twilio.com/console
_22
// and set the environment variables. See http://twil.io/secure
_22
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_22
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
In order to update the call you will need to use the CallSid. Twilio returns this when you initiate an outgoing call, and also includes the CallSid in its request to your application for an incoming call's initial TwiML.
As an alternative to updating your call with TwiML directly, you may also redirect a Call to a new URL that responds with your requested TwiML.
Redirect an in progress Call to a new URL
Java
_24
// Install the Java helper library from twilio.com/docs/java/install
_24
_24
import com.twilio.Twilio;
_24
import com.twilio.http.HttpMethod;
_24
import com.twilio.rest.api.v2010.account.Call;
_24
_24
import java.net.URI;
_24
_24
public class Example {
_24
// Find your Account SID and Auth Token at twilio.com/console
_24
// and set the environment variables. See http://twil.io/secure
_24
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_24
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
Twilio will end phone calls for you when it encounters a <Hangup> verb or when it runs out of TwiML to process. But you can also end phone calls whenever you like by passing a "completed" status to a CallSid in progress.
Terminate a Running Call
Java
_19
// Install the Java helper library from twilio.com/docs/java/install
_19
_19
import com.twilio.Twilio;
_19
import com.twilio.rest.api.v2010.account.Call;
_19
_19
public class Example {
_19
// Find your Account SID and Auth Token at twilio.com/console
_19
// and set the environment variables. See http://twil.io/secure
_19
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_19
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");