【问题标题】:Get user accessible apps?获取用户可访问的应用程序?
【发布时间】:2023-04-04 11:30:01
【问题描述】:

我正在尝试从用户设备获取所有应用。 并且有很多示例如何做到这一点。 但是 - 我只想显示在 Launcher 和 souch 中显示的应用程序。所以安装的应用程序和系统应用程序(如 com.android.calendar)。但是,我很难过滤像“com.android.certinstaller”这样的东西。通常是操作系统使用的应用程序,但一般用户找不到图标。

我有两个实现。一个只显示用户安装的应用程序(因此没有系统应用程序,如日历或拨号器)

    PackageManager pm = getActivity().getPackageManager();
    List<ApplicationInfo> apps = pm.getInstalledApplications(0);

    List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();

    for(ApplicationInfo app : apps) {
        //checks for flags; if flagged, check if updated system app
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
            installedApps.add(app);
            //it's a system app, not interested
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            //Discard this one
            //in this case, it should be a user-installed app
            continue;
        } else {
            installedApps.add(app);
        }
    }

和其他收集所有应用程序的:

    ArrayList<ApplicationItem> res = new ArrayList<ApplicationItem>();
    List<PackageInfo> packs = getActivity().getPackageManager().getInstalledPackages(0);
    for(int i=0;i<packs.size();i++) {
        PackageInfo p = packs.get(i);
        if ((!getSysPackages) && (p.versionName == null)) {
            continue ;
        }
        ApplicationItem newInfo = new ApplicationItem();
        newInfo.appname = p.applicationInfo.loadLabel(getActivity().getPackageManager()).toString();
        newInfo.pname = p.packageName;
        newInfo.versionName = p.versionName;
        newInfo.versionCode = p.versionCode;
        newInfo.icon = p.applicationInfo.loadIcon(getActivity().getPackageManager());
        res.add(newInfo);
    }
    return res;

所以。任何想法如何做到这一点?

【问题讨论】:

    标签: android android-package-managers


    【解决方案1】:

    顺便说一句,您可以通过意图获取它,只需添加 category_launcher 以确定该应用是否有要启动的活动

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
    

    没有重复将使用 HashSet

        //using hashset so that there will be no duplicate packages, 
        //if no duplicate packages then there will be no duplicate apps
        HashSet<String> packageNames = new HashSet<String>(0);
        List<ApplicationInfo> appInfos = new ArrayList<ApplicationInfo>(0);
    
        //getting package names and adding them to the hashset
        for(ResolveInfo resolveInfo : pkgAppsList) {
            packageNames.add(resolveInfo.activityInfo.packageName);
        }
    
        //now we have unique packages in the hashset, so get their application infos
        //and add them to the arraylist
        for(String packageName : packageNames) {
            try {
                appInfos.add(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
            } catch (NameNotFoundException e) {
                //Do Nothing
            }
        }
    
        //to sort the list of apps by their names
        Collections.sort(appInfos, new ApplicationInfo.DisplayNameComparator(packageManager));ApplicationInfo.DisplayNameComparator(packageManager));
    

    【讨论】:

    • 这很好用!但是,某些应用程序不止一次列出。我记得我看到了解决方案,但我现在找不到...
    • 谢谢!我认为有更“聪明”的方法可以做到这一点,但 HashSet 也很好。再次感谢您!
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2011-07-21
    • 2016-01-18
    相关资源
    最近更新 更多