14

I am using the following piece of code at the moment:

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);

but it returns Apps that have been installed by the both device manufacturer and me. How to limit it so that only the apps that I installed are returned?

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Raunak
  • 6,427
  • 9
  • 40
  • 52
  • If it is Android 11 take refer https://stackoverflow.com/questions/62345805/namenotfoundexception-when-calling-getpackageinfo-on-android-11 – user3509903 Nov 10 '21 at 09:28

10 Answers10

41
// Flags: See below
int flags = PackageManager.GET_META_DATA | 
            PackageManager.GET_SHARED_LIBRARY_FILES |     
            PackageManager.GET_UNINSTALLED_PACKAGES;

PackageManager pm = getPackageManager();
List<ApplicationInfo> applications = pm.getInstalledApplications(flags);
for (ApplicationInfo appInfo : applications) {
    if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
        // System application
    } else {
        // Installed by user
    }
}

Flags:

Mark Renouf
  • 30,697
  • 19
  • 94
  • 123
Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • what does 0 in getInstalledApplications(0) stands for. The doco is not stating the int 0. http://developer.android.com/reference/android/test/mock/MockPackageManager.html#getInstalledPackages%28int%29 – GSree Jan 04 '11 at 23:24
  • 1
    Your if condition is incorrect. It should be applications.get(n).applicationInfo.flags :) – Raunak Jan 04 '11 at 23:48
  • @Raunak - My if condition is correct. Try it on the phone and you will see that. Also, try your suggestion and you will see that it does not compile. – Zelimir Jan 05 '11 at 09:50
  • @GSree - agreed. Documentation does not mention explicitly 0, although it works and means all applications. Alternatively, instead of 0 I could use PackageManager.GET_UNINSTALLED_PACKAGES. I used that to follow original approach described in question itself. – Zelimir Jan 05 '11 at 09:56
  • @Jared Burrows - If you edit, please take care to format the source code correctly. Thanks. – Zelimir Aug 07 '12 at 08:19
  • @Zelimir - Sorry just trying to fix a logic error because this code helped me :) – Jared Burrows Aug 09 '12 at 14:48
  • @Jared Burrows - It is all fine. I just cannot remember that code snapshot alignment looked so strange. – Zelimir Aug 10 '12 at 07:34
  • I don't understand: In the condition line it says that "applicationInfo cannot be resolved or is a field" and thats right because i can't find it in the docs? – seb Aug 18 '12 at 10:12
  • Sorry, but it has been edited since my original post, and I cannot check if the changes someone else made are correct or not. The code I originally posted was working correctly for me. – Zelimir Aug 18 '12 at 13:32
  • You can view the edits but clicking "Edit" and click the drop-down bar. – Jared Burrows Aug 27 '12 at 01:22
  • 1
    I've fixed the example! – Mark Renouf Mar 26 '14 at 20:20
  • @GSree For all cases of int flags: 0 means no flags are set. – FrankKrumnow Sep 10 '21 at 12:32
  • It's taking a too long time to get all installed app – Abhi S Jun 30 '22 at 13:26
14

Zelimir's answer is correct. But in some cases it won't give you all the installed third-party applications. ApplicationInfo also has flag FLAG_UPDATED_SYSTEM_APP which is set

If this application has been install as an update to a built-in system application

On my smart phone such applications include Amazone Kindle, Adobe Reader, Slacker Radio and others. These applications did not come with the phone and were installed from Google Play Store. Thus, they can be considered as third-party apps.

So, you may also want to check FLAG_UPDATED_SYSTEM_APP flag.

final PackageManager packageManager = _context.getPackageManager();
List<ApplicationInfo> installedApplications = 
    packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo appInfo : installedApplications)
{
    if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
    {
        // IS A SYSTEM APP
    }

    if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)
    {
        // APP WAS INSTALL AS AN UPDATE TO A BUILD-IN SYSTEM APP
    }
}
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
5

If I do pkgAppsList.get(0), it returns an ResolveInfo Object. How do I get information such as the icon, and packageName?

Just do this:

ResolveInfo info = pkgAppsList.get(0);
ApplicationInfo appInfo = info.activityInfo.applicationInfo;

PackageManager packageManager = = getPackageManager();
//And then you retrieve all needed data:
Drawable packageIcon = packageManager.getApplicationIcon(applicationInfo); //Icon
String packageName = applicationInfo.packageName; //Package name
String packageLabel = String.valueOf(packageManager.getApplicationLabel(applicationInfo)) //Package label(app name)
Anton Cherkashyn
  • 5,719
  • 6
  • 43
  • 80
2

Nikolai's answer is correct, but could be optimized using an iterator. This is what I came up with:

/**
 * Return list of installed user applications
 */
static List<ApplicationInfo> getUserInstalledApplications(Context context) {
    // Get installed applications
    final PackageManager packageManager = context.getPackageManager();
    List<ApplicationInfo> installedApplications =
            packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

    // Remove system apps
    Iterator<ApplicationInfo> it = installedApplications.iterator();
    while (it.hasNext()) {
        ApplicationInfo appInfo = it.next();
        if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            it.remove();
        }
    }

    // Return installed applications
    return installedApplications;
}
Ofir
  • 171
  • 1
  • 5
1
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
user547995
  • 2,036
  • 8
  • 33
  • 62
  • If I do pkgAppsList.get(0), it returns an ResolveInfo Object. How do I get information such as the icon, and packageName? – Raunak Jan 04 '11 at 22:11
1

Answering this for android 11/API 30

context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA); The above code returns the list of system apps as user apps are not visible by default, you need to add below permission in the manifest to get list of user apps

<uses-permission android:name"android.permission.QUERY_ALL_PACKAGES">

Ketan sangle
  • 376
  • 5
  • 6
1

In case you want to know how to do this in Kotlin it is shown below though as mentioned previously by Ketan sangle you will need to add <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission"/> in your AndroidManifest.xml file.

val packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

for (packageInfo in packages) {
            if (packageInfo.flags and ApplicationInfo.FLAG_SYSTEM != 1) {
                 //enter what you want to do here
            }
        }

In this case I used the system flag to exclude system apps and you can find other flags here

Eric
  • 11
  • 1
0

user

  1. pkgAppsList.get(i).activityInfo.packageName to fetch packageName
  2. pkgAppsList.get(i).activityInfo.applicationInfo.loadLabel(getPackageManager()).toString() to fetch app level name
0

Android PackageManager class is used to retrieve information on the application packages that are currently installed on the device. You can get an instance of PackageManager class by calling getPackageManager(). PackageManager provides methods for querying and manipulating installed packages and related permissions, etc. In this Android example, we we get list of installed apps in Android.

PackageManager packageManager = getPackageManager(); List list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA)

packageManager.getInstalledApplications() return a List of all application packages that are installed on the device. If we set the flag GET_UNINSTALLED_PACKAGES has been set, a list of all applications including those deleted with DONT_DELETE_DATA (partially installed apps with data directory) will be returned.

Full info here.

Another good read here.

AD Progress
  • 4,190
  • 1
  • 14
  • 33
0

There is a very useful library Called AndroidUtils, which will help to get it just write one line code.

Ref: https://github.com/xihadulislam/androidUtils

val installApplications : List<ApplicationInfo> = ApplicationUtil.getInstallApplications(context)