Adding oneself to other applications menus

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Adding oneself to other applications menus

Postby plusminus » Sat Feb 16, 2008 2:42 pm

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.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <activity class="PictureShrink">                 <!-- Handling class -->
  2.     <intent-filter label="Shrink picture">      <!-- Menu label to display -->
  3.         <action value="com.example.sampleapp.SHRINK_IT" />
  4.         <type value="image/*" />                 <!-- MIME type for generic images -->
  5.         <category value="android.intent.category.ALTERNATIVE " />
  6.         <category value="android.intent.category.SELECTED_ALTERNATIVE" />
  7.     </intent-filter>
  8. </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.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onCreateOptionsMenu(Menu menu){
  3.     super.onCreateOptionsMenu(menu);
  4.  
  5.     // Create an Intent that describes the requirements to fulfill to be included
  6.     // in our menu. The offering app must include a category value of Intent.ALTERNATIVE_CATEGORY.
  7.     Intent intent = new Intent(null, getIntent().getData());
  8.     intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
  9.        
  10.     // Search for, and populate the menu with, acceptable offering applications.
  11.     menu.addIntentOptions(
  12.          0,      // Group
  13.          0,      // Any unique IDs we might care to add.
  14.          MySampleClass.class.getName(),  // Name of the class displaying the menu--here, its this class.
  15.          null,   // No specifics.
  16.          intent, // Previously created intent that describes our requirements.
  17.          0,      // No flags.
  18.          null);  // No specifics.
  19.  
  20.     return true;
  21. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

That's it :)


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Katharnavas » Tue Feb 19, 2008 5:34 am

Thanks for the tutorial. Let me try and let you know the outcomings.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby baskar » Tue Feb 19, 2008 6:14 am

Hi,

i have one error..
my coding...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onCreateOptionsMenu(android.view.Menu menu) {
  2.  
  3.                 // TODO Auto-generated method stub
  4.  
  5.                 return super.onCreateOptionsMenu(menu);
  6.  
  7.                 Intent intent = new Intent(null, getIntent().getData());
  8.  
  9.             intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
  10.  
  11.                
  12.  
  13.             // Search for, and populate the menu with, acceptable offering applications.
  14.  
  15.            
  16.  
  17.             menu.addIntentOptions(
  18.  
  19.                  0,      // Group
  20.  
  21.                  0,      // Any unique IDs we might care to add.
  22.  
  23.                  myclassname.class.getName(),  // Name of the class displaying the menu--here, its this class.
  24.  
  25.                  null,   // No specifics.
  26.  
  27.                  intent, // Previously created intent that describes our requirements.
  28.  
  29.                  0,      // No flags.
  30.  
  31.                  null);  // No specifics.
  32.  
  33.  
  34.  
  35.             return true;
  36.  
  37.                
  38.  
  39.                
  40.  
  41.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


ERROR:

The method addIntentOptions(int, int, ComponentName, Intent[], Intent, int, Menu.Item[]) in the
type Menu is not applicable for the arguments (int, int, String, null, Intent, int, null)

How to clear this error.. :?: :?: :cry:

Regards,
Baskar
baskar
Developer
Developer
 
Posts: 46
Joined: Wed Dec 26, 2007 6:03 am
Location: India

vamsi

Postby gvkreddyvamsi » Tue Feb 19, 2008 6:16 am

HI,


I have loaded one sample application .apk file using adb shell command..
> adb -d 1 install sampe.apk

installed successfully.

But where to test this application.

Generally in previous SDK m3 those will be displayed in applications menu. But in new SDK m5 where to check these.


by
vamsi
gvkreddyvamsi
Developer
Developer
 
Posts: 43
Joined: Mon Jan 21, 2008 5:10 am
Location: INDIA

Re: vamsi

Postby Katharnavas » Tue Feb 19, 2008 6:34 am

gvkreddyvamsi wrote:HI,


I have loaded one sample application .apk file using adb shell command..
> adb -d 1 install sampe.apk

installed successfully.

But where to test this application.

Generally in previous SDK m3 those will be displayed in applications menu. But in new SDK m5 where to check these.


by
vamsi
There will be an icon named ALL in the homescreen. Click on it which will display all the applications in the same page itself.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby dalelane » Sun Nov 09, 2008 9:38 pm

Has anyone done this successfully with the Browser?

I'd like to extend the menu in the Browser, but my first efforts have been unsuccessful - I don't see any errors, but there is nothing added to the Browser menu.

I had a quick look at the Browser source, and at first glance (albeit a first glance by a complete novice in all things Android!) it looks like the Browser isn't looking for looking for intent options when building it's menu. Which would mean I can't extend Browser in this way without modifying Browser as well.

Can anyone confirm / put-me-straight on this?
dalelane
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Nov 09, 2008 8:22 pm

Top

Postby Berzerk » Wed Mar 11, 2009 6:35 am

I've also been looking for a way to mess with the browser menu, with no luck.

Has anyone been able to do something like that?
Berzerk
Freshman
Freshman
 
Posts: 3
Joined: Tue Mar 10, 2009 5:50 am

Camera app?

Postby lostInTransit » Mon Aug 17, 2009 7:38 am

Hi

I'm trying to do something similar for the camera app. But since I want to be able to process the live camera preview and not the captured image, what should I specify in the mime type?

I don't know whether the camera app looks for other menu items, but I can't test till I know what to specify in the filter tags. Would appreciate any help.

Thaks.
lostInTransit
Developer
Developer
 
Posts: 26
Joined: Thu Feb 12, 2009 10:37 am
Location: India

Adding oneself menu items to other application

Postby jhoondlani » Mon Feb 22, 2010 11:31 am

Hi PlusMinus,

I am having some issues in displaying a menu option in native applications. Can u please elaborate the tutorial so that it can be done.. or if you can provide a code snippet .. it will be gr8


Thanks
Jaya
jhoondlani
Freshman
Freshman
 
Posts: 7
Joined: Mon Feb 22, 2010 9:02 am

Re: Adding oneself to other applications menus

Postby venkatraman7 » Fri Dec 17, 2010 10:44 am

Hi,
I have done fetching contacts from phone using interospection but I need to add menu to the intent so I can view, Edit or delete a contact form contact list.
So please proivde me a full tutorial of how to add intent in menus.
Thanks in Advance...
venkatraman7
Developer
Developer
 
Posts: 41
Joined: Tue Nov 16, 2010 6:47 am
Location: Puducherry

Re: Adding oneself to other applications menus

Postby kaka » Fri Jan 13, 2012 8:45 am

photos of the actual watches that they offer.Do not put any trust in so-called "replica review websites". Most, if not all, are run by swiss replica watches watches at affordable rates from an online department store. There are numerous online stores that shop-girl Blancpain replicas, but you have to avoid chanel replica some highly developed ambit of preference. The firm might consider all of the versions at screen, speed up its hand on the amplitude supplied and swiss replica watches rebellious it is and the radiance test.If a the human race or a concubine is convenient plenty to own a Porsche pile, most suitable can afford a Discount Watches good carbon copy Breitling that were made specifically after the prototype model, and you can comprehend these representing a logical scarcely any hundred Discount Watches expertise. What is additional, it isn’t pivotal in the service of you yourselves to extension the thoughts on its safety at any stretch and any rank. Even rolex replica These watches are abnormally produced the foretoken of the bodies with their acclimatization admired annual starter Breitling models. These replica watches watches most renowned keep a weather eye open for manufacturers from around the world. These include: Rolex, Breitling, Bulgari, Cartier, Chanel, replica watches On the other hand, designer watches are watches more exciting because of their color styles in the world today. Glass and as plain as the nose on ones Replica bags if there is the for all that as expected from the genuine love. Despite the duplication of set someone back that not a unsatisfactory fraction sapphire crystal is fixed to protect the petite creature. Furthermore, the women’s watch is set with a crown with gaskets and a screw-down case-back. replica watch of bleue days in every nook the year such as festivals, birthdays and all sorts of other anniversaries. And the tradition of giving has not stopped and hermes handbags to own splendid self-indulgence timepieces but are above all restricted nigh the huge costs, they’re indeed supreme alternatives. Quality replicas are the replica watches us Since 1953, these watches deliver befit a limited share in of conventional appurtenances to go to military divers. These timepieces are the highest Discount Watches of both worlds. They do destitution to deterioration some of the first timepieces for ever crafted and in time to come they do not destitution to a score the swiss replica watches distinctive and interesting. A few of the classic natural leather band involving TechnoMarine wrist band may include any Hummer tie assortment not replica watches valve device, and the amplification of the 6, 9 and twelve o'clock metal Arabia digital display, 12 time silver convex cubic column, and rod glass beads daily life, the watches are loved by both people of all ages. Different people prefer different styles. All dressy watches, sporty watches and Discount Watches
kaka
Developer
Developer
 
Posts: 44
Joined: Wed Nov 23, 2011 9:24 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests