Adding oneself to other applications menus
You can also advertise your Activity's services so that other Activities can add your activity to their own option menu. For example, suppose you implement a new image handling tool that shrinks an image to a smaller size and you would like to offer this as a menu option to any other Activity that handles pictures. To do this, you would exposes your capabilities inside an intent filter in your manifest. If another application that handles photos asks Android for any Activities that can perform actions on pictures, Android will perform intent resolution, find your Activity, and add it to the other Activity's options menu.
The offering application
The application offering the service must include an <intent-filter> element in the manifest, inside the <activity> tag of the offering Activity. The intent filter includes all the details describing what it can do, such as a <type> element that describes the MIME type of data that it can handle, a custom <action> value that describes what your handling application can do (this is so that when it receives the Intent on opening it knows what it is expected to do), and most important, include a <category> filter with the value android.intent.category.ALTERNATIVE and/or android.intent.category.SELECTED_ALTERNATIVE (SELECTED_ALTERNATIVE is used to handle only the currently selected element on the screen, rather than the whole Activity intent.
Here's an example of a snip of a manifest that advertises picture shrinking technology for both selected items and the whole screen.
Using xml Syntax Highlighting
- <activity class="PictureShrink"> <!-- Handling class -->
- <intent-filter label="Shrink picture"> <!-- Menu label to display -->
- <action value="com.example.sampleapp.SHRINK_IT" />
- <type value="image/*" /> <!-- MIME type for generic images -->
- <category value="android.intent.category.ALTERNATIVE " />
- <category value="android.intent.category.SELECTED_ALTERNATIVE" />
- </intent-filter>
- </activity>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
The menu-displaying application
An application that wants to display a menu that includes any additional external services must, first of all, handle its menu creation callback. As part of that callback it creates an intent with the category Intent.ALTERNATIVE_CATEGORY and/or Intent.SELECTED_ALTERNATIVE, the MIME type currently selected, and any other requirements, the same way as it would satisfy an intent filter to open a new Activity. It then calls menu.addIntentOptions() to have Android search for and add any services meeting those requirements. It can optionally add additional custom menu items of its own.
You should implement SELECTED_ALTERNATIVE in onPrepareOptionsMenu() rather than onCreateOptionsMenu(), because the user's selection can change after the application is launched.
Here's a code snippet demonstrating how a picture application would search for additional services to display on its menu.
Using java Syntax Highlighting
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- super.onCreateOptionsMenu(menu);
- // Create an Intent that describes the requirements to fulfill to be included
- // in our menu. The offering app must include a category value of Intent.ALTERNATIVE_CATEGORY.
- Intent intent = new Intent(null, getIntent().getData());
- intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
- // Search for, and populate the menu with, acceptable offering applications.
- menu.addIntentOptions(
- 0, // Group
- 0, // Any unique IDs we might care to add.
- MySampleClass.class.getName(), // Name of the class displaying the menu--here, its this class.
- null, // No specifics.
- intent, // Previously created intent that describes our requirements.
- 0, // No flags.
- null); // No specifics.
- return true;
- }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
That's it 

Regards,
plusminus






