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);

intent.setData(Uri.parse("http://www.google.com"));

activity.startActivity(intent);

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 >

<intent-filter>

<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 http://code.google.com/android/reference/android/R.styleable. 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:

<intent-filter>

<action android:name="android.intent.action.VIEW" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </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:

<intent-filter>

<action android:name="android.intent.action.VIEW" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter>

0 0

Post a comment