android 获取所有包名,非系统包名,还有 指定包名对应的启动activity

    //  不过滤,所有包名
    public static List getAllPackageNames() {
        List packageNames = new ArrayList<>();
        List resolveInfos = loadApps();
        for (ResolveInfo resolveInfo : resolveInfos) {
            String packageName = resolveInfo.activityInfo.packageName;
            packageNames.add(packageName);
        }
        return packageNames;
    }
//取第三方应用,过滤系统应用
    public static List getNullSystemPackageNames() {
        List packageNames = new ArrayList<>();
        List resolveInfos = loadApps();
        for (ResolveInfo resolveInfo : resolveInfos) {
            String packageName = resolveInfo.activityInfo.packageName;
            try {
                PackageManager pm = ServiceApplication.getContext().getPackageManager();
                PackageInfo pi = pm.getPackageInfo(packageName, 0);
                if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
                    //第三方应用
                    packageNames.add(packageName);
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }

        }
        return packageNames;
    }
/**
     * 获取指定包名应用的启动activity
     * @param packageName
     * @return
     */
    public static String getActivityName(String packageName) {
        String activityName = "";
        List resolveInfos = loadApps();
        for (ResolveInfo resolveInfo : resolveInfos) {
            if (packageName.equals(resolveInfo.activityInfo.packageName)) {
                activityName = resolveInfo.activityInfo.name;
            }
        }
        return activityName;
    }

 

你可能感兴趣的:(android1)