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

 

 

 posted on 2013-12-08 14:46  wlrhnh  阅读(978)  评论(0编辑  收藏  举报