get Installed apps from package name

不问归期 提交于 2019-12-12 05:29:39

问题


i have tried the code i pasted below ..but that gives me every application installed but then i need to check each of the app is mine or not by checking with the package name...Is there a code to where i can pass the package name to the phone system so that it gives me all the apps installed based on that package name.

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:


Theres a cleaner way to do that.

 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;

Copied shamelessly from this link



来源:https://stackoverflow.com/questions/14955414/get-installed-apps-from-package-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!