that you will use to do the job. Android uses Intents to do specific jobs within applications. Once you
master the use of Intents, a whole new world of application development will be open to you. This section
defines what an Intent is and how it is used. An Intent is Android’s’ll notice, in most cases, the
name of the Intent does a good job of describing what that Intent does.
The Intent is only one-third of the picture. An Intent is really just that, an intent to do something;
an Intent cannot actually do anything by itself. You need Intent Filters and Intent Receivers to listen for,
and interpret, the Intents. An Intent Receiver is like the mailbox of an Activity. The Intent Receiver is
used to allow an Activity to receive the specified Intent. Using the previous web browser example, the
Web Browser Activity is set up to receive web browser Intents. A system like this allows unrelated
Activities to ignore Intents that they would not be able to process. It also allows Activities that need the
assistance of another Activity to utilize that Activity without needing to know how to call it.
With Intents and Intent Receivers, one Activity can send out an Intent and another can receive it.
However, there needs to be something that governs the type of information that can be sent between the
two Activities. This is where Intent Filters come in. Intent Filters are used by Activities to describe the
types of Intents they want to receive. More importantly, they outline the type of data that should be passed
with the Intent. Therefore, in our example scenario, we want the web browser to open a web page. The
Intent Filter would state that the data passed with the WEB_SEARCH_ACTION Intent should be in the
form of a URL.
In the next section, you will begin to use Intents to open and utilize the phone’s dialer. Using the
Dialer Now that you know what an Intent is, it is time to see one in action. This section shows you how to
use the DIAL_ACTION Intent to open the phone dialer. You will pass a telephone number with your
Intent. If your application works correctly, you should see displayed in the dialer the number you pass
with your Intent.
The first step is to create a new project for this Activity.
Name the project AndroidPhoneDialer. The following illustration shows the New Android Project
wizard for this project.
With your new application open in Eclipse, the first order of business is to remove the TextView from
main.xml that contains that Hello World statement. The main.xml file should look like this after you
remove the TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
android:orientation="vertical"
android:layout_width="fill_parent"
android:
</LinearLayout>
You need to add two new packages to your project to utilize the DIAL_ACTION Intent, as follows. The
first package allows you to set up Intents and the second allows you to parse URIs.
import android.content.Intent;
import android.net.Uri;
NOTE There are several different Intent Filters on the DIAL_ACTION Intent that you can use. You are
using the Filter that lets you pass a phone number as a URI. The next step is to create your Intent. The
syntax for creating an Intent is as follows:
Intent <intent_name> = new Intent(<Android_Intent>,<data>)
For your application, replace the first parameter, <intent_name>, with DialIntent. To get the value for the
second parameter, <Android_Intent>, refer to the list of Activity Actions in Table 7-1. You’ll find that, to
call the dialer, you need to use the DIAL_ACTION Intent. To call the Intent properly, use the format
Intent.DIAL_ACTION. The last parameter, <data>, is the phone number. The DIAL_ACTION, "tel:5551212" in this example.
The final call to create an Intent for your project should look like this:
Intent DialIntent = new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));
TIP
You use the notation tel:<phone_number> to dial a specific phone number. You can also use voicemail:
instead of tel: to dial the phone’s voicemail shortcut. With the Intent created, you now have to tell
Android that you want the dialer to be launched in a new Activity. To do this, you use the
setLaunchFlags( ) method of the Intent. You must pass setLaunchFlags( ) the appropriate parameter for
launching. The following is a list of the possible launch flags that you can set:
NOTE
In some cases, more than one of the following flags may be set to accomplish the desired outcome.
- NO_HISTORY_LAUNCH Launches the Activity without recording it in the system’s launch history
- SINGLE_TOP_LAUNCH Tells the system not to launch the Activity if it is already running
- NEW_TASK_LAUNCH Launches the Activity
- MULTIPLE_TASK_LAUNCH Launches the Activity even if it is already running
- FORWARD_RESULT_LAUNCH Allows the new Activity to receive results that would normally be
forwarded to the existing Activity In this case, you want to use Intent.NEW_TASK_LAUNCH, which
simply lets you open a new instance of the dialer Activity:
DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH );
The last step to creating your dialer Intent is to actually launch the Activity. (More accurately, you are
telling Android that you have an intent to launch the dialer as a New Task. It is ultimately up to Android
to launch the Dialer Activity.) To tell Android that you want to start the dialer, you need to use
startActivity( ):
startActivity(DialIntent);
Notice that you pass to startActivity( ) your Intent. The Intent is then passed to Android, and the action is
resolved. The full code for AndroidPhoneDialer.java should look like this:
package sk Dialer */
/** Pass the Dialer the number 5551212 */
Intent DialIntent = new
Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));
/** Use NEW_TASK_LAUNCH to launch the Dialer Activity */
DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH );
/** Finally start the Activity */
startActivity(DialIntent);
}
}
Placing a Call from Your Activity In this section you will learn what Intent to add to your Activity when
calling the dialer. You will also learn where to add your chosen Intent in the Activity’s code. Further, you
will learn how to parse the intended phone number as a URI. You need to make a few changes to your
code to move from the Dialer Activity to the Call Activity. In this section, you are going to edit your
AndroidPhoneDialer Activity to place a call after opening the dialer.
Adding the Intent to Your Activity You still need the Intent and Uri packages—shown here—so leave
those in place at the header of your AndroidPhoneDialer.java file.
import android.content.Intent;
import android.net.Uri;
These packages will enable you to not only instantiate the Intent that you need, but also pass the needed
telephone number data to the Intent (with the Uri package).
TIP
If you are flipping through the chapters out of order, and did not work on the project in the previous
section, simply create a new project, name it AndroidPhoneDialer, and add the previous two packages to
- That will catch you up to speed. True to its name, the Intent that you need in your Activity is
CALL_ACTION. In much the same way that DIAL_ACTION opened the Android dialer,
CALL_ACTION will launch the phone’s calling process and initiate a call to the supplied number.
To create the Intent, use the same procedure as you did for the dialer, only this time call CALL_ACTION:
Intent CallIntent = new Intent(Intent.CALL_ACTION,Uri.parse("tel:5551212"));
Notice that you use Uri.parse to pass a correctly parsed telephone number to the Activity. The next step is
to tell Android that you want to set this Activity to launch, and then launch it. This is accomplished using
the following two lines of code.
CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH );
startActivity(CallIntent);
In the first line, you sent the launch flag to NEW_TASK_LAUNCH. This launches a new instance of the
Call Activity. Finally, you tell Android to start the Activity using your Intent. When finished, your
AndroidPhoneDialer.java file should look like this.
package android_programmers_guide device's Call Activity */
/** Pass the Call the number 5551212 */
Intent CallIntent = new
Intent(Intent.CALL_ACTION,Uri.parse("tel:5551212"));
/** Use NEW_TASK_LAUNCH to launch the Call Activity */
CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH );
/** Finally start the Activity */
startActivity(CallIntent);
}
}
Editing Activity Permissions Most Activity Action Intents fall into the category of requiring that the
proper permission be set before Android will allow the action. As with many systems, Android just needs
to make sure that only Activities with the correct credentials be allowed to perform actions with Activities
that are outside of their base. Here are the available permissions:
Building the Activities
Up to now, you have created only single-Activity applications. This is to say, you have created rather simplistic applications that encompass only one “screen” of data. Take a minute, and think of the last few applications you have used. Chances are, they used more than one “window.” Most applications use multiple windows to gather, display, and save data. Your Android applications should be no different.
Although you have not yet learned how to create multiple-Activity applications that run on Android, you got a hint about how to leverage multiple Activities in the last chapter. You used a new concept called Intents to call—and run—a core Android Activity. While the concept still holds true in this chapter, the execution is slightly different when you want to call Activities that you have created, as opposed to calling core Android Activities.
The first thing you need to do is build the Activities. Then you can create the Intents that will call them.
When building the Activities, you need to follow a three-step process.
- Intent code for the .xml file
- Intent code for the .java file
- Calling Activities using an Intent
Once you create your first additional Activity, the rest should come very easily.
NOTE
These steps are not bound to each other. You can perform them in any order. Intent Code for the .xml File Remember that all Android Activities comprise three main parts: the .java file that contains the code, the .xml file that holds the layout, and the package’s Manifest. To this point in the book, you have only used main.xml to control the layout of a single Activity. However, to take advantage of having multiple Activities, you must have multiple .xml layout files.
To create a new .xml file, open your Eclipse project and navigate to the Package Explorer. Open the res directory, right-click the layout folder, and choose New | File.
The layout file is created, but it is empty. To get the Activity off on the right foot, add the following code to test.xml. This code will provide a base for your layout. If you need to, you can simply copy this code from the existing main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:
</LinearLayout>
Intent Code for the .java File
Using the Package Explorer again, navigate to the src directory, open it, and right-click the android_programmers_guide.AndroidViews package, as shown in the following illustration. Once again, you are going to add a new file to the folder. After you right-click the AndroidViews package, select New | File from the context menu. This file will hold all the code for the second Activity in this project. Name the file test.java. You should now have a nice, new (but empty) .java file. You just need to add a few lines of code to the file to make it usable:
package testPackage.test;
import android.app.Activity;
import android.os.Bundle;
public class test extends Activity {
/** Called when the Activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.test);
/** This is our Test Activity
All code goes below */
}
}
Modifying the AndroidManifest.xml
Open your AndroidManifest.xml file in Eclipse. AndroidManifest.xml contains the global settings for your project. More importantly, AndroidManifest.xml also contains the Intent Filters for your project.
The information that facilitates this process is kept in AndroidManifest.xml.
NOTE
There is only one AndroidManifest.xml file per project.
If your AndroidManifest.xml file is currently open, it should appear as follows:
<activity android:
<intent-filter>
<action android:
<category android:
</intent-filter>
</activity>
What you are looking at here is the Intent Filter for the AndroidViews Activity, the main Activity that was created with the project. To this file you can add any other Intent Filters that you want your project to handle. In this case, you want to add an Intent Filter that will handle the new Test Activity that you created.
The following is the code for the Intent Filter that you need to add to the
AndroidManifest.xml file:
<activity android:
<intent-filter>
<action android:
<category android:
</intent-filter>
</activity>
Adding this code to AndroidManifest.xml enables Android to pass Intents for the Test Activity to the correct place. The full AndroidManifest.xml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
<application android:
<activity android:
<intent-filter>
<action android:
<category android:
</intent-filter>
</activity>
<activity android:
<intent-filter>
<action android:
<category android:
</intent-filter>
</activity>
</application>
</manifest>