如何判断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;
}

 

 

相关文章:

  • 2022-12-23
  • 2021-08-18
  • 2021-10-12
  • 2022-02-13
  • 2022-12-23
  • 2019-07-27
猜你喜欢
  • 2022-12-23
  • 2021-04-15
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2021-11-18
  • 2021-06-18
相关资源
相似解决方案