Android get list of non Play Store apps

2019-07-30 03:52发布

问题:

As a safety measure I would like to get the list of apps that aren't installed from the Play Store. Is there a way to do this?

The packageManager contains a method getInstalledApplications but I don't know which flags to add to get the list. Any help would be appreciated.

Edit: Here is an code example of v4_adi's answer.

public static List<String> getAppsFromUnknownSources(Context context)
{
  List<String> apps = new ArrayList<>();
  PackageManager packageManager = context.getPackageManager();
  List<PackageInfo> packList = packageManager.getInstalledPackages(0);
  for (int i = 0; i < packList.size(); i++)
  {
     PackageInfo packInfo = packList.get(i);
     if (packageManager.getInstallerPackageName(packInfo.packageName) == null)
     {
        apps.add(packInfo.packageName);
     }
  }

  return apps;
}

This is a good start, however this also returns a lot off pre-installed Android and Samsung apps. Is there anyway to remove them from the list? I only want user installed apps from unknown sources.

回答1:

The following link has answer to your question The PackageManager class supplies the getInstallerPackageName method that will tell you the package name of whatever installed the package you specify. Side-loaded apps will not contain a value.

How to know an application is installed from google play or side-load?



回答2:

Originally I thought it would be enough to retrieve the apps that weren't installed via the Google Play Store. Later I found that I also needed to filter out the pre-installed system applications. I found the last part of the puzzle in another post: Get list of Non System Applications

public static List<String> getAppsFromUnknownSources(Context context)
{
  List<String> apps = new ArrayList<>();
  PackageManager packageManager = context.getPackageManager();
  List<PackageInfo> packList = packageManager.getInstalledPackages(0);
  for (int i = 0; i < packList.size(); i++)
  {
     PackageInfo packInfo = packList.get(i);
     boolean hasEmptyInstallerPackageName = packageManager
           .getInstallerPackageName(packageInfo.packageName) == null;
     boolean isUserInstalledApp = (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0;

     if (hasEmptyInstallerPackageName && isUserInstalledApp)
     {
        apps.add(packInfo.packageName);
     }
  }

  return apps;
}