Next   Previous section   Next section

Mobile Phone Handheld Hardware Hardware Rick Rogers John Lombardo O'Reilly Media, Inc. O'Reilly Media Android Application Development, 1st Edition

14.1. 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:9785551212"));
        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.

14.1.1. Creating an Example Application to Run the call Method

To test the method in Example 14-1, create a new Android project in Eclipse by selecting File New Project Other.... When the "Select a Wizard" dialog appears, select Android Android Project. When you see the new project dialog, fill it in as shown in Figure 14-1.

Press Finish to create a project named dialing-example in your Eclipse workspace. (The complete code for this example is also on the book's website.) You will see this project in the Package Explorer pane of your Eclipse IDE. Expand the project to see a set of folders, including one named src. Expand this folder to see a package named example.dialing. Expand that package and you will see two Java source files, one of which is named dialing.java. This file contains the code in Example 14-2.

Example 14-2. Setting up an application to make phone calls

package example.dialing;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class dialing extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

This is where you will put the code that invokes our call method.

Figure 14-1. Creating a phone project in Eclipse


14.1.2. Embedding the Code Snippet in a Simple Application

Now that you have created a simple Android application, you can use it to isolate and observe operations, such as starting a phone call.

Copy the method we created in Section 14.1.1 to the dialing class in the dialing.java file. Then, add a line to the onCreate method that calls the call method. The results should look something like Example 14-3.

Example 14-3. The dialing class with call method and its invocation

package example.dialing;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class dialing extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        call();
    }

private void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:9785551212"));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
        Log.e("dialing-example", "Call failed", activityException);
    }
}
      

Make sure your program compiles and runs. To run the program, select the Run Run command. When the Run As dialog appears, select Android Application. If you have followed the steps in this chapter, the result should be displayed in the Android emulator window (Figure 14-2).

Figure 14-2. Dialer starts


You can use the red "end" button on the phone depicted in the emulator to let the user end the simulated phone call.

          
    Next   Previous section   Next section
     


    интернет магазин инструментов в москве