Android: Can I use this Intent?

Android offers a very powerful and yet easy to use tool called intents. An intent can be use to turn applications into high-level libraries and make code re-use something even better than before. The Android Home screen and AnyCut use intents extensively to create shortcuts for instance. While it is nice to be able to make use of a loosely coupled API, there is no guarantee that the intent you send will be received by another application. This happens in particular with 3rd party apps, like Panoramio and its RADAR intent.

While working on a new application, I came up with a very simple way to find out whether the system contains any application capable of responding to the intent you want to use. I implemented this technique in my application to gray out the menu item that the user would normally click to trigger the intent. The code is pretty simple and easy to follow:

 * Indicates whether the specified action can be used as an intent. This
 * method queries the package manager for installed packages that can
 * respond to an intent with the specified action. If no suitable package is
 * found, this method returns false.
 * @param context The application's environment.
 * @param action The Intent action to check for availability.
 * @return True if an Intent with the specified action can be sent and
 *         responded to, false otherwise.
public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List list =
    return list.size() > 0;

Here is how I use it:

public boolean onPrepareOptionsMenu(Menu menu) {
    final boolean scanAvailable = isIntentAvailable(this,

    MenuItem item;
    item = menu.findItem(;

    return super.onPrepareOptionsMenu(menu);

In this example, the menu is grayed out if the Barcode Scanner application is not installed. Another, simpler, way to do this is to catch the ActivityNotFoundException when calling startActivity() but it only lets you react to the problem, you cannot predict it and update the UI accordingly to prevent the user from doing something that won’t work. The technique described here can also be used at startup time to ask the user whether he’d like to install the missing package, you can then simply redirect him to the Android Market by using the appropriate URI.

28 Responses to “Android: Can I use this Intent?”

  1. Pop says:

    Rescanning all your books ISBN are you? ;-)

  2. jcl says:

    Indeed, for the people that went to devoxx,
    the ‘click’ is easily made.
    Can’t wait for the final version ;-)

    Anyway, great to see you pick up a bit of your older blogging style.

  3. luok says:

    Handy suggestion. -1 for greying things out though (its often hard to tell just why something is disabled).

  4. Romain Guy says:

    I debated against myself over this many times. My first solution was not greying out the menu but letting the user click it and then showing a message explaining what went wrong. I however find this even more frustrating: if I cannot do something, why does the UI let me do it in the first place? I could also remove the menu items, but it’s even worse, since the menu would change depending on unknown factors (kinda like greying out but at least you don’t change the layout.)

    When the app is done, it will include a brief tutorial explaining the why and how of these items. Hopefully it will be clear enough.

  5. Luke says:

    Why not instead change the text of the menu item? Keep the menu item in the same position, but instead of using text like “Scan…”, use “Install scanner…” or “Learn about scanner…”.

  6. maxemaos says:

    welcome to maxemaos

  7. Ouroborus says:

    This technique is used in Download Manager (one of the built in applications) to determine if it should bother downloading a requested link.

  8. Hi Romain, although i think this check is better than nothing – i still have some concerns about using intents. As an example ( you might already read this on Dev forum ) – i want to share text via email and SMS. This should work with standard Messaging app as soon as cupcake is out
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, Body + emailTagLine);
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, Title);
    sendIntent.putExtra(“sms_body”, smsBody);
    Well as you can see , i had to specify different extras for sms and email. Developer has a flexibility , even using “standard” action to put any extras. And not necessarily default one. And therefore to “choose and use” intents not only i need to know if intent is available but also set of extras to pass. And currently i don’t think i can get this info , unless source is open or developer shared api.

  9. I follow your blog for quite a long time and should tell that your posts are always valuable to readers.

  10. with we leave it to you to decide.

  11. Hello, I found this article while searching for help with JavaScript. I’ve recently changed browsers from Opera to Internet Explorer 7. Now I seem to have a problem with loading JavaScript. Everytime I browse website that needs Javascript, the site doesn’t load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any help is greatly appreciated! Thanks

  12. i like your style of writing

  13. says:

    Hi, nice blog. I am inviting you to join our monthly blogging tournament.

  14. Awesome blog, you got a new follower this very minute

  15. mx123 says:

    Where can a person find a list of intents available foe use?

  16. css server says:

    Hallo, ich würde gerne wissen ob mir einer helfen könnte wie man einen Counter Strike Source Server einrichten kann ? Und ob das etwas kosten tut ? Oder funktioniert das mit z.B. Sponsoren ??? Währe toll wenn ich per e-mail oder hier in den Kommis eine Antwort für mein Problem bekommen würde. 2l3oigf7

  17. Will Hou says:

    this is useful, thanks for sharing!

  18. Denham says:

    Good day could I use some of the perception discovered on this webpage if I provide a hyperlink back to your site?

  19. really good information thanks admin

  20. Ailin says:

    what if what I want is to change the highlight color of the focus and when I click on the item?