Can I use this Intent? (Switched android developer)

Can I use this Intent?

Posted by Romain Guy on 05 January 2009 at 6:00 AM

Android offers a Powerful and yet very Easy to use tool called intents . An intent can be use to Turn applications into High-level LIBRARIES Re-use code and make 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 Application Will be received by another. 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<ResolveInfo> 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.

分类:Mobile 时间:2010-11-16 人气:128
blog comments powered by Disqus


  • setClassName (String, String) and setClassName (Context, String) the distinction 2010-11-29

    public Intent setClassName (Context packageContext, String className) This describes you want to jump in activity or service is out of your current activity is the same applicaition (apk) inside. The public Intent setClassName (String packageName, St

  • Developing Detail Explain Struts2 application - 6.Action the value of the property into 2010-09-21

    Struts2 Action attribute to provide dependency injection functionality. Program code with the configuration file as follows: Code fragment: public class HelloWorldAction ( private String message = null; public String getMessage () ( return message; )

  • String Manipulation - split a string by string 2010-03-30

    /* * Note the string decomposition function. strtok The more complex the application to properly. ! * You can also not strtok function , But to achieve string by splitting a string into difficult ! * Note the str to space must be readable and writabl

  • js in English and Chinese in English and Chinese string length of the string js intercept (change) 2010-03-30

    js in English and Chinese in English and Chinese string length of the string js intercept (change) Keywords: javascript / Interception Abstract: We have procedures for dealing with the process of the text, often encounter problems in English and Chin ...

  • Check string is a valid string 2010-04-17

    Check string is a valid string, the string is valid provided 0 - 9 numbers, lowercase letters, and underscores _ and hyphens -

  • String transcoding [String.getBytes () and new String ()] 2010-11-09

    In Java,, String.getBytes (String decode) method will return the specified decode a string encoded under the encoding byte array representation, such as byte [] b_gbk = "in". getBytes ("GBK"); byte [] b_utf8 = "in". getB

  • 如何将一个String和多个String值进行比较思路分析 2013-12-24

    开发中我们经常需要将一个String和多个String值进行比较。直觉反应是使用||符号连接多个===完成,感兴趣的朋友可以了解下哈 开发中我们经常需要将一个String和多个String值进行比较。直觉反应是使用||符号连接多个===完成,比如: if (string === 'banana' || string === 'pineapple') { fruitColor = 'yellow'; } 这样能够很好的完成需求,但总觉得有点笨,并且对扩展不友好,当我们的水果种类变多时: 复制代码

  • Struts2 action returns the string 2010-12-30

    Struts2 action in the method actually does not return a string. So let bad mood. The following is a solution: <action name="uploadFiles" method="uploadFiles"> <result name="xmlMessage" type="plaintext"></result> </action> The

  • Ubuntu add application shortcuts to the application 2011-05-07

    Eclipse wants to add to the system such as the "application" menu, as follows: sudo gedit / usr / share / applications / Eclipse.desktop / / file name are free from Then add the following inside: [Desktop Entry] Name = Eclipse Comment = Ecl

  • じ ☆ ve string-AS online games in the Socket Data Analysis and Countermeasures dip package 2010-04-15

    Excerpt: ActionScript3 + Java (including any back-end server language) gaming data synchronization has been the large number of developers often encountered in the development of a headache, some experience now posted for your information. Stick on t ...

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

processed in 0.291 (s). 12 q(s)