Home | History | Annotate | Download | only in articles
      1 page.title=Can I Use this Intent?
      2 parent.title=Articles
      3 parent.link=../browser.html?tag=article
      4 @jd:body
      5 
      6 <p>Android offers a very powerful and yet easy-to-use message type called 
      7 an <a href="{@docRoot}guide/topics/intents/intents-filters.html">intents</a>.
      8 You can use intents to turn applications into high-level libraries and
      9 make code modular and reusable. The Android Home screen and AnyCut 
     10 applications, for instance, use intents extensively to create shortcuts. </p>
     11 
     12 <p>While it is nice to be able to make use of a loosely coupled
     13 API, there is no guarantee that the intent you send will be received by
     14 another application. This happens in particular with third-party apps, like 
     15 <a href="http://code.google.com/p/apps-for-android/source/browse/trunk/Panoramio">Panoramio</a> 
     16 and its RADAR intent.</p>
     17 
     18 <p>This article describes a technique you can use to find out whether the system
     19 contains any application capable of responding to the intent you want to use.
     20 The example below shows a helper method that queries the system package manager
     21 to determine whether there's an app that can respond to a specified intent. Your
     22 application can pass an intent to the method and then, for example, show or hide
     23 user options that the user would normally use to trigger the intent. </p>
     24 
     25 <pre class="prettyprint">/**
     26  * Indicates whether the specified action can be used as an intent. This
     27  * method queries the package manager for installed packages that can
     28  * respond to an intent with the specified action. If no suitable package is
     29  * found, this method returns false.
     30  *
     31  * @param context The application's environment.
     32  * @param action The Intent action to check for availability.
     33  *
     34  * @return True if an Intent with the specified action can be sent and
     35  *         responded to, false otherwise.
     36  */
     37 public static boolean isIntentAvailable(Context context, String action) {
     38     final PackageManager packageManager = context.getPackageManager();
     39     final Intent intent = new Intent(action);
     40     List&lt;ResolveInfo&gt; list =
     41             packageManager.queryIntentActivities(intent,
     42                     PackageManager.MATCH_DEFAULT_ONLY);
     43     return list.size() &gt; 0;
     44 }
     45 </pre>
     46 
     47 <p>Here is how you could use the helper method:</p>
     48 
     49 <pre class="prettyprint">@Override
     50 public boolean onPrepareOptionsMenu(Menu menu) {
     51     final boolean scanAvailable = isIntentAvailable(this,
     52         "com.google.zxing.client.android.SCAN");
     53 
     54     MenuItem item;
     55     item = menu.findItem(R.id.menu_item_add);
     56     item.setEnabled(scanAvailable);
     57 
     58     return super.onPrepareOptionsMenu(menu);
     59 }
     60 </pre>
     61 
     62 <p>In this example, the menu is grayed out if the <em>Barcode Scanner</em> 
     63 application is not installed. </p>
     64 
     65 <p>Another, simpler, way to do this is to catch the
     66 <code>ActivityNotFoundException</code> when calling <code>startActivity()</code>
     67 but it only lets you react to the problem, you cannot predict it and update the
     68 UI accordingly to prevent the user from doing something that won't work. The
     69 technique described here can also be used at startup time to ask the user
     70 whether he'd like to install the missing package, you can then simply redirect
     71 him to the Android Market by using the appropriate URI.</p>