Quick and Easy Phone Calls

Android includes an application called PhoneApp that embodies the functions of a mobile phone. Through the use of Intent objects, Android enables applications to tell other applications to perform certain operations, such as initiating a phone call. To enable your application to initiate a phone call, a method like the one in Example 14-1 will do the job.

Example 14-1. How to make a phone call private void call() { try {

Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:978555l212")); startActivity(callIntent); } catch (ActivityNotFoundException activityException) {

Log.e("dialing-example", "Call failed", activityException);

What happens when you start a phone call depends, in part, on the telephone network. The number may be incorrect. The network may be busy or otherwise unavailable. The call can be interrupted. Here, however, you see no error-handling logic, except for catching and logging exceptions that can be thrown if Android's system encounters a problem when finding applications that can process Intent objects. Instead, the PhoneApp application, which already has code for interpreting and remediating errors, handles the job from the time the phone call is started.

When an application just wants to start phone calls, making it handle all these contingencies is a large burden. Systems that provide a telephony API place that burden on application authors when, in most cases, all an application needs is to start a phone call—not to manage the lifecycle of a phone call.

Starting a phone call is a multistep operation. Here we'll take a detailed look at each step in the execution of the call method shown in Example 14-1. Along the way, we'll see how it uses Android's system of Intent objects and Intent filters.

0 0

Post a comment