Intents and Data URIs

So far we've covered the simplest of the intents, where all we need is the name of an action. The ACTION_DIAL activity in Listing 3-32 is one of these. So to invoke the dialer, all we need is the dialer's action and nothing else:

public static void dial(Activity activity) {

Intent intent = new Intent(Intent.ACTION_DIAL); activity.startActivity(intent);

Unlike ACTION_DIAL, the intent ACTION_CALL that is used to make a call to a given phone number takes an additional parameter called Data. This parameter points to a URI, which in turn points to the phone number:

public static void call(Activity activity) {

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:555-555-5555"));

activity.startActivity(intent);

The action portion of an intent is a string or a string constant, usually prefixed by the Java package name. The data portion is always a string representing a URI. The format of this URI could be specific to each activity that is invoked by that action. In this case, the CALL action decides what kind of data URI it would expect. From the URI it extracts the telephone number.

0 0

Post a comment