一些有关Intent异常的处理方法(no activity matches)
来源:网络 责任编辑:admin 发表时间:2013-07-01 10:23 点击:次
	   
	通常系统中的应用程序会借助于抛出Intent的方法,调用程序以外的资源来处理当前需求。这种方式可以提高系统资源利用率,而且当存在多个解决方法时,可以供用户作出最优化选择。然而,有一种特殊的情况却不容忽视,当前Intent无法在系统中获得任何可利用资源。这是一个必须要面对的客观情况,这里给出两种针对这种情况的解决方法。
第一种方法是提前预判,预判特定的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 applications 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 list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}
第二种方式是错误补救,利用ActivityNotFoundException实时跟踪捕捉startActivity()或者startActivityForResult()执行过程中由于无法找到与其相匹配的Activities而抛出的异常,从而给出针对这种异常的解决办法。
查看更加详细的内容:Can I use this Intent?
相关新闻>>
- 发表评论
 - 
				
 
- 最新评论 进入详细评论页>>
 








