Can I Use this Intent? Android中不要发送没有接收应用的Intent.

Android中Intent如果没有接收对象,就不要发送了, 因为可能导致系统Crash,所以我们在发送前检查有没有应用的Intent,下面是Android的解释:我们直接

Android offers a very powerful and yet easy-to-use message type called an intents. You can use intents to turn applications into high-level libraries and make code modular and reusable. The Android Home screen and AnyCut applications, for instance, use intents extensively to create shortcuts.

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 third-party apps, like Panoramio and its RADAR intent.

This article describes a technique you can use to find out whether the system contains any application capable of responding to the intent you want to use. The example below shows a helper method that queries the system package manager to determine whether there's an app that can respond to a specified intent. Your application can pass an intent to the method and then, for example, show or hide user options that the user would normally use to trigger the intent.

/**

 * 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);
                    
            //queryIntentServices(Intent intent, int flags)


    return list.size() > 0;


}

Here is how you could use the helper method:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {


    final boolean scanAvailable = isIntentAvailable(this,


        "com.google.zxing.client.android.SCAN");


    MenuItem item;

    item = menu.findItem(R.id.menu_item_add);


    item.setEnabled(scanAvailable);



    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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值