Http://developer.android.com/guide/topics/ui/menus.html? # Intents
Add the menu component based on the imported intent object
In some cases, you need to use the menu component and intent to start an activity (whether the activity is in the current application or not ). When you know the details of the intent you are using and prepare a menu component to start this intent, you can select the callback function corresponding to this component (for exampleOnoptionsitemselected ()).Startactivity ()Method.
However, when no application in the local machine can process this intent, the menu control you created to send this intent will eventually be invalid because no activity can correspond to this intent. To solve this problem, the android system provides the following mechanism that allows android to dynamically add this menu component only when it finds an activity that can process the intent locally.
To add a dynamic menu component, follow these steps:
1. Define a categoryCategory_alternativeOrCategory_selected_alternativeAnd intent objects of other properties to be used
2. CallMenu. addintentoptions ()Android system first searches for the activity that can process the intent locally. If the activity can be found, Android system automatically adds this component to the menu; otherwise, no.
Note: BecauseCategory_selected_alternativeIs used to select the intent of the entry in the interface. Therefore, this type attribute can only be used inOncreatecontextmenu ()Method.
For example:
@ Override
Publicboolean oncreateoptionsmenu (menu
Menu ){
Super. oncreateoptionsmenu (menu );
// Createan intent that describes the requirements to fulfill, to be encoded
// In ourmenu. The offering app must include a category value ofintent. category_alternative.
Intent intent = newintent (null,
Datauri );
Intent. addcategory (intent. category_alternative );
// Searchand populate the menu with acceptable offering applications.
Menu. addintentoptions (
R. Id. intent_group ,//
Menugroup to which new items will be added
0, // uniqueitem ID (none)
0, // orderfor the items (none)
This. getcomponentname (),//
Thecurrent activity name
Null, // specificitems to place first (none)
Intent, // intent created above thatdescribes our requirements
0, // additional flags to control items (none)
Null); // array ofmenuitems that correlate to specific
Items (none)
Returntrue;
}
For each activity found in the Android system that meets the requirements, a corresponding menu component is created, and the intent corresponding to this intent can be used in this activity.
The Android: label of filter serves as the title of the menu component and the application icon of the activity as the icon.Addintentoptions ()Use the total number of successfully created menu components as the return value.
Note:When you callAddintentoptions (),
It overrides any and all menu items by themenu group specified in the first argument.
Add your activity to the menus of other applications.
To achieve the purpose of the title, your activity must use one of the following two values in the type attribute of intent filter:Category_alternativeOrCategory_selected_alternative.
Example:
<Intent-filterlabel = "@ string/resize_image">
...
<Categoryandroid: Name = "android. Intent. Category. Alternative"/>
<Categoryandroid: Name = "android. Intent. Category. selected_alternative"/>
...
</Intent-filter>