Dynamically add intents

If there are potentially multiple activities that are relevant to your current Activity or selected item, then the application can dynamically add menu items that execute other services.

During menu creation, define an Intent with the category Intent.ALTERNATIVE_CATEGORYand/or Intent.SELECTED_ALTERNATIVE, the MIME type currently selected (if any), and any other requirements, the same way as you would satisfy an intent filter to open a new Activity. Then call addIntentOptions() to have Android search for any services meeting those requirements and add them to the menu for you. If there are no applications installed that satisfy the Intent, then no additional menu items are added.

Note: SELECTED_ALTERNATIVE is used to handle the currently selected element on the screen. So, it should only be used when creating a Menu in onCreateContextMenu() or onPrepareOptionsMenu() , which is called every time the Options Menu is opened.

Here's an example demonstrating how an application would search for additional services to display on its menu.

public boolean onCreateOptionsMenu(Menu menu){

super.onCreateOptionsMenu(menu); // Create an Intent that describes the requirements to fulfill, to be included

For more information on creating Intents and providing your application's services to other applications, read the Intents and Intent Filters document.

0 0

Post a comment