安卓(Android) 怎么从包管理器(PackageManager)中查找匹配的解析信息

2019年10月19日12:04:09安卓(Android) 怎么从包管理器(PackageManager)中查找匹配的解析信息已关闭评论 385 893字阅读2分58秒

1.简介

安卓(Android) 怎么从包管理器(PackageManager)中查找匹配的解析信息?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import java.util.Collection;
import java.util.HashSet;

public class Main {
    static Collection<ResolveInfo> findMatchingResolveInfo(
            PackageManager pm, Intent messageIntent,
            String... lookupPackages) {
        Collection<ResolveInfo> resInfo = findAllMatching(pm, messageIntent);
        Collection<ResolveInfo> matchingResInfo = new HashSet<>(
                resInfo.size());
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            for (String lookupPackage : lookupPackages) {
                if (packageName.contains(lookupPackage)) {
                    matchingResInfo.add(resolveInfo);
                    break;
                }
            }
        }
        return matchingResInfo;
    }

    public static Collection<ResolveInfo> findAllMatching(
            PackageManager pm, Intent messageIntent) {
        return pm.queryIntentActivities(messageIntent, 0);
    }
}

 
继续阅读