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和多个String值进行比较思路分析 2013-12-24

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

  • vector char* 和 string和C-Style的string的效率 2012-03-26

    大部分人使用vector构造字符串数组,都是用vector<string>,但是也不乏人使用vector<char*> ,用vctor<char*>不会遇到任何问题,但是既然用了C的东西,很多东西需要自己来控制,不像C++的string那样: std::string is a string class, encapsulating all the required data that makes up a string, along with allocation an

  • Map String,List String 转为List Map String,String 2014-07-25

    /** * * @param map 输入 * @param list 输出 * @param idx 次序 * @param pathMap 已选 */ void map2List(Map<String, List<String>> map, List<Map<String, String>> list, int idx, HashMap<String,String> pathMap){ if(null == pathMap){ pathMap

  • java中String s="abc"及String s=new String("abc")详解 2014-12-04

    1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. 2. 栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器.但缺点是,存在栈中的数据大小与生存期必须是确定的,缺乏灵活性.另外,栈数据可以共 享,详见第3点.堆的优势是可以动态地分配内存大小,生存期也不必事先告诉编译器,Java的垃圾收集器会自动收走这些不再使用的数据.但缺点是,由于要 在运行时动态分配内存,存取速度较慢. ==是判断两个对象

  • StringUtils类:isEmpty(String str) VS isBlank(String 2015-05-05

    org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String str). StringUtils.isEmpty(String str) 功能: 判断某字符串是否为"空或长度为0",str==null 或 str.length()==0 示例: public static void main(String[] args) { System.out.pri

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

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