How to get the list of apps that have been installed by a user on an Android device?

独自空忆成欢 提交于 2019-11-27 18:37:02
Zelimir
// 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:

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

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)

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;
}
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
user4558855

user

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

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.

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