?
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<ResolveInfo> list =? ? ? ? ? ? packageManager.queryIntentActivities(intent,? ? ? ? ? ? ? ? ? ? PackageManager.MATCH_DEFAULT_ONLY);? ? return list.size() > 0;}Here is how I use it: