Get the user's installed APP list and APK installation package

Codewords work hard! Please indicate the source!

 

· Wrong way:

public static List<PackageInfo> getAllApplication(Context context, boolean needActivities) {
        PackageManager packageManager = context.getPackageManager();
        return packageManager.getInstalledPackages(needActivities ? PackageManager.GET_ACTIVITIES : 0);
    }

This is currently the most popular method on the Internet, and it seems that the semantics is the most practical method. However, after a large number of tests on blogger projects, it has been found that this method has three fatal problems:

1. Obtained a lot of system services : these services have no Activity and cannot be filtered out by removing the system APP

2. Unable to get a complete list of APPs : In the test of bloggers, it is often found that the bugs of APPs such as certain QQ and certain video cannot be scanned

3. Unable to get the startup activity of the APP : This method can only get all the activities of the APP. As for which LauncherActivity is, there is no way to know. Although the first item in the Manifest file of most apps is LauncherActivity, there are still many applications that do the opposite, such as a QQ

In summary, the landlord highly does not recommend using packageManager.getInstalledPackages to get the application list!

· Recommended method:

//获取用户安装的APP
    public static List<ResolveInfo> getInstalledApplication(Context context, boolean needSysAPP) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
        if (!needSysAPP) {
            List<ResolveInfo> resolveInfosWithoutSystem = new ArrayList<>();
            for (int i = 0; i < resolveInfos.size(); i++) {
                ResolveInfo resolveInfo = resolveInfos.get(i);
                try {
                    if (!isSysApp(context, resolveInfo.activityInfo.packageName)) {
                        resolveInfosWithoutSystem.add(resolveInfo);
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return resolveInfosWithoutSystem;
        }
        return resolveInfos;
    }

    //判断是否系统应用
    public static boolean isSysApp(Context context, String packageName) throws PackageManager.NameNotFoundException {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        return (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1;
    }

· Acquisition of some common data:

//包名
resolveInfo.activityInfo.packageName

//启动Activity
resolveInfo.activityInfo.name 

//APP名
resolveInfo.activityInfo.applicationInfo.loadLabel(getPackageManager())

//Icon
resolveInfo.activityInfo.applicationInfo.loadIcon(getPackageManager())

//APK安装包路径
resolveInfo.activityInfo.applicationInfo.sourceDir

Finally, the universal convention: If the blogger’s help makes you grateful, it is better to give the blogger a small red envelope~

Guess you like

Origin blog.csdn.net/u014653815/article/details/85773304