Generic Actions

The actions Intent.ACTION_CALL and Intent.ACTION_DIAL could easily lead us to the wrong assumption that there is a one-to-one relationship between an action and what it invokes. To disprove this, let us extract a counterexample from the IntentUtils code in Listing 3-32:

public static void invokeWebBrowser(Activity activity) {

Intent intent = new Intent(Intent.ACTION_VIEW);



Note that the action is simply stated as ACTION_VIEW. How does Android know which activity to invoke in response to such a generic action name? In these cases, Android relies more heavily on the nature of the URI. Android looks at the scheme of the URI, which happens to be http, and questions all the registered activities to see which ones understand this scheme. Out of these, it inquires which ones can handle the VIEW and then invokes that activity. For this to work, the browser activity should have registered a VIEW intent against the data scheme of http. That intent declaration might look like this in the manifest file:

<activity >


<action android:name="android.intent.action.VIEW" /> <data android:scheme="http"/> <data android:scheme="https"/> </intent-filter> </activity>

You can learn more about the data options by looking at the XML definition for the data element at html#AndroidManifestData. The child elements or attributes of data include these:

host mimeType path pathPattern pathPrefix port scheme mimeType is one attribute you'll see used often. For example, the following intent-filter for the activity that displays a list of notes indicates the MIME type as a directory of notes:


<action android:name="android.intent.action.VIEW" /> <data android:mimeType="" /> </intent-filter>

The screen that displays a single note, on the other hand, declares its intent-filter using a MIME type indicating a single note item:


<action android:name="android.intent.action.VIEW" /> <data android:mimeType="" /> </intent-filter>

0 0

Post a comment