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( ):


Notice that you pass to startActivity( ) your Intent. The Intent is then passed to Android, and the action is resolved. The full code for should look like this:

package android_programmers_guide.AndroidPhoneDialer; import; import android.content.Intent; import android.os.Bundle; import;

public class AndroidPhoneDialer extends Activity {

/** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); /** Create our Intent to call the 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 */


You should now compile AndroidPhoneDialer and run it on your Emulator. The process for compiling and running applications was covered in previous chapters, so you should be familiar with that process. Once you run your application, the Emulator should launch. After the lengthy boot process, your Activity will launch.

0 0

Post a comment