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