Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Opening a Web Page, Phone Number, or Anything Else with an Intent

Author: Ian Darwin -- Published? true -- FormatLanguage: W


The Intent mechanism is fundamental to Android; it allows one application to have some entity processed by another application without knowing or caring what that application is.


Invoke the Intent constructor; invoke startActivity on the constructed Intent


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 as Android likes it less precisely (more generally) a URI. URIs can be created using the static parse() method in the URI class. 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, just call the inherited startIntent method, e.g.,


If all goes well, the user will see the web browser, phone dialer, maps application, or whatever.

XXX Discuss other actions such as ACTION_OPEN

However, if things fail, the user will not see anything. Why not? We basically told Android that we don't care whether the intent succeeds or fails. To get feedback, we have to 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, that is:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // do something with the results...

It may be obvious, but is important to note, that you cannot know the result of an Intent until the entire application that was processing it is finished, which may be an arbitrary time later. However, the onActivityResult will eventually be called,

XXX Cover resultCode

XXX cover use of the passed intent - refer to recipes on passing Extra Data


The source code for this project can be downloaded from http://projects.darwinsys.com/IntentsDemo-src.zip.


The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory IntentsDemo.
FlinxSYS 2011-12-08 04:53:42.266 Although when unzipped and copied over into a new Android Project the app fails. At least for this noob. Please help? I have error in ExampleActivity.java stating 'R is not a defined variable'.... Please advise?
dipen.ptl1 2011-11-02 01:42:24.401 How can we see the code,given link is not working.