Discussion
The Intent
constructor takes two arguments: the action to take and the entity to act on. Think of the first as the verb and the second as the object of the verb. The most common action is Intent.ACTION_VIEW
, for which the string representation is android.intent.action.VIEW
. The second will typically be a URL or, in Android, a URI (uniform resource identifier). URI objects can be created using the static parse()
method in the Uri
class (note the two lowercase letters in the class name do not use the URI
class from java.net
). Assuming that the string variable data
contains the location we want to view, the code to create an Intent
for it might be something like the following:
Intent
intent
=
new
Intent
(
Intent
.
ACTION_VIEW
,
Uri
.
parse
(
data
));
That’s all! The beauty of Android is shown here—we don’t know or care if data
contains a web page URL with http:
, a phone number with tel:
, or even something we’ve never seen. As long as there is an application registered to process this type of Intent, Android will find it for us, after we invoke it. How do we invoke the Intent? Remember that Android will start a new Activity to run the Intent. Assuming the code is in an Activity, we just call the inherited startIntent()
method. For example:
startActivity
(
intent
);
If all goes well, the user will see the web browser, phone dialer, maps application, or whatever.
Google defines many other actions, such as ACTION_OPEN
(which tries to open the named object). In some cases VIEW
and OPEN
will have the same effect, but in other cases the former may display data and the latter may allow the user to edit or update the data.
If the request fails because your particular device doesn’t have a single Activity in all its applications that has said it can handle this particular Intent, the user will not see another Activity, but instead the startActivity()
call will throw the unchecked ActivityNotFoundException
.
And even if things do work, we won’t find out about it.
That’s because we basically told Android that we don’t care whether the Intent succeeds or fails.
To get feedback, we would instead call startActivityForResult()
:
startActivityForResult
(
intent
,
requestCode
);
The requestCode
is an arbitrary number used to keep track of multiple Intent requests; you should generally pick a unique number for each Intent you start, and keep track of these numbers to track the results later (if you only have one Intent whose results you care about, just use the number 1
).
Just making this change will have no effect, however, unless we also override an important method in Activity
:
@Override
public
void
onActivityResult
(
int
requestCode
,
int
resultCode
,
Intent
data
)
{
// Do something with the results...
}
It may be obvious, but it is important to note that you cannot know the result of an Intent until the Activity that was processing it is finished, which may be an arbitrary time later. However, onActivityResult()
will eventually be called.
The resultCode
is, of course, used to indicate success or failure. There are defined constants for these, notably Activity.RESULT_OK
and Activity.RESULT_CANCELED
. Some Intents provide their own, more specific result codes; for one example, see Recipe 9.7. For information on use of the passed Intent, please refer to recipes on passing extra data, such as Recipe 4.4.
The sample program attached to this recipe allows you to type in a URL and either OPEN
or VIEW
it, using the actions defined previously. Some example URLs that you might try are shown in the following table.
URL |
Meaning |
Note |
http://www.google.com/
|
Web page |
|
content://contacts/people/
|
List of contacts |
|
content://contacts/people/1
|
Contact details for one person |
|
geo:50.123,7.1434?z=10
|
Location and zoom |
Need Google API |
geo:39.0997,-94.5783
|
Location |
Need Google API |