【问题标题】:android - how to filter out system apps from listview of applicationsandroid - 如何从应用程序列表视图中过滤掉系统应用程序
【发布时间】:2013-11-05 07:57:57
【问题描述】:

我正在使用 android 代码的 sn-p 来学习如何启动应用程序和制作启动器,但我不知道如何过滤掉诸如 faceunlock 和 facebok 之类的应用程序以实现 htc 感知和类似的应用程序

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;
}

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

【问题讨论】:

    标签: android filter categories launcher


    【解决方案1】:

    见下面的代码:

    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
        }
    }
    

    【讨论】:

    • 每次我尝试实现这个方法都会给我一个错误
    • applicationInfo.flags 其他一切似乎都很好,但是由于某种原因,一段代码没有通过
    【解决方案2】:

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

    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;
    }
    

    我最终所做的只是替换了我最初为此使用的代码并导入了迭代器并过滤了所有不需要的应用程序:) 希望这对其他人有所帮助

    【讨论】:

      【解决方案3】:

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

      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
          }
      }
      

      【讨论】:

      • 您好,欢迎来到 SO!尽管代码本身可能会说话,但提供一些细节将有助于提高您的答案质量!
      猜你喜欢
      • 2013-02-28
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 2014-03-07
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多