android - 如何从应用程序列表视图中过滤掉系统应用程序

时间:2013-11-05 07:57:58

标签: android filter categories launcher

我正在使用一段Android代码来学习如何启动应用程序并制作一个启动器,但我无法弄清楚如何过滤出像faceunlock和facebok这样的应用程序,用于htc sense和类似的应用程序

public static List<ApplicationInfo> getInstalledApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
    Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
    return apps;
}

我知道我可能是一个简单的错误,但我似乎无法找到它,请帮助并解释答案,以便我可以从中学习:如果需要,可以发布更多代码

3 个答案:

答案 0 :(得分:0)

见下面的代码:

List<ApplicationInfo> applications = getPackageManager().getInstalledApplications(0);
for (int n=0; n < applications.size(); n++)
{
    if ((applications.get(n).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
    {
        //This is System application
    }
    else
    {
       //This app is installed by user
    }
}

答案 1 :(得分:0)

没关系,经过长时间的搜索,我找到了答案,所以我决定把它用来帮助其他人解决同样的问题

public static List<ApplicationInfo> getInstalledApplication(Context context) {
    PackageManager packageManager = context.getPackageManager();
    List<ApplicationInfo> apps = packageManager.getInstalledApplications(0);
    Collections.sort(apps, new ApplicationInfo.DisplayNameComparator(packageManager));
    Iterator<ApplicationInfo> it = apps.iterator();
    while (it.hasNext()) {
        ApplicationInfo ai = it.next();
        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            it.remove();
        }
    }
    return apps;
}

所有我最终做的就是替换我原来为此进行的代码并导入Iterator并过滤掉所有不需要的应用程序:)希望这有助于其他人

答案 2 :(得分:-1)

您可以使用程序包管理器中提供的标志来区分系统应用程序和已安装的应用程序。

PackageManager manager = getPackageManager();
List<PackageInfo> availableActivities = manager.getInstalledPackages(0);

for(PackageInfo packageInfo : availableActivities) {

    if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
        continue;
    }
    else {
        //Installed Apps
    }
}