Search code examples
androidgoogle-playandroid-package-managersinstalled-applications

Android get list of non Play Store apps


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.


Solution

  • 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;
    }