Set an intent for a single menu item

If you want to offer a specific menu item that launches a new Activity, then you can specifically define an Intent for the menu item with the setIntent() method.

// in our menu. The offering app must include a category value of Intent.CATEGORY_ALTERNATIVE.

Intent intent = new Intent(null, getIntent().getData());

intent.addCategory(Intent.CATEGORY_ALTERNATIVE);

// Search for, and populate the menu with, acceptable offering applications. menu.addIntentOptions(

thisClass.INTENT_OPTIONS, // Menu group 0, // Unique item ID (none)

this.getComponentName(), // The current Activity name null, // Specific items to place first (none)

intent, // Intent created above that describes our requirements 0, // Additional flags to control items (none)

null); // Array of MenuItems that corrolate to specific items (none) return true;

For each Activity found that provides an Intent Filter matching the Intent defined, a menu item will be added, using the android:label value of the intent filter as the text for the menu item. The addIntentOptions() method will also return the number of menu items added.

Also be aware that, when addIntentOptions() is called, it will override any and all menu items in the menu group specified in the first argument.

If you wish to offer the services of your Activity to other application menus, then you only need to define an intent filter as usual. Just be sure to include the ALTERNATIVE and/or SELECTED_ALTERNATIVE values in the name attribute of a <category> element in the intent filter.

For example:

<intent-filter label="

Resize Image">

<category android:

name="android.intent

category

ALTERNATIVE" />

<category android:

name="android.intent

category

SELECTED ALTERNATIVE" />

</intent-filter>

read more about writing intent filters in the Intents and Intent Filters document. For a sample application using this technique, see the Note Pad sample code.

0 0

Post a comment