从包名称获取已安装的应用程序

时间:2013-02-19 10:40:31

标签: android android-activity package-managers

我已经尝试了下面粘贴的代码..但是这给了我安装的每个应用程序但是我需要通过检查包名来检查每个应用程序是我的...是否有代码到哪里我可以将软件包名称传递给电话系统,以便根据软件包名称为我提供所有应用程序。

PackageManager manager = getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
        final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
        Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
        ArrayList<String> oArrInstalledHomeApps = new ArrayList<String>();
        if (apps != null) 
        {
            final int count = apps.size();

            for (int i = 0; i < count; i++) 
            {
                ResolveInfo info = apps.get(i);
                String strpackageName = info.activityInfo.applicationInfo.packageName;
if(strpackageName.contains ("com.ZZ."))
{
                oArrInstalledHomeApps.add(strpackageName);
}
            }
            return oArrInstalledHomeApps;
        }

1 个答案:

答案 0 :(得分:2)

Theres是一种更清洁的方法。

 final PackageManager pm = getPackageManager();
        ArrayList<String> oArrInstalledHomeApps = new ArrayList<String>();
        // get a list of installed apps.
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        for (ApplicationInfo packageInfo : packages)
        {
            Log.d(TAG, "Installed package :" + packageInfo.packageName);
            Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
            if (packageInfo.packageName.contains("com.ZZ."))
            {
                oArrInstalledHomeApps.add(packageInfo.packageName);
            }
        }
        return oArrInstalledHomeApps;

从这个link

中无耻地复制