如何判断Intent有没有对应的Activity去处理?至少有以下两种方法,最近使用过,随笔记下来,以供查阅。
第一种, 如下:
1 public boolean isIntentResolvable(Intent intent) { 2 return intent.resolveActivity(this.getPackageManager()) != null; 3 }
第二种,比较复杂,但是能够获得更多信息,有时候更有用:
public static boolean isIntentAvailable(String action) { final PackageManager packageManager = getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); Log.d("David", "list.size() = " + list.size()); for (ResolveInfo res : list) { Log.d("David", "res.resolvePackageName = " + res.activityInfo.packageName); } return list.size() > 0; }