简体   繁体   English

如何获取用户在 Android 设备上安装的应用列表?

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

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?如何限制它以便只返回我安装的应用程序?

// 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 ApplicationInfo还设置了标志FLAG_UPDATED_SYSTEM_APP

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.在我的智能手机上,此类应用程序包括 Amazone Kindle、Adobe Reader、Slacker Radio 等。 These applications did not come with the phone and were installed from Google Play Store.这些应用程序不是手机自带的,而是从 Google Play 商店安装的。 Thus, they can be considered as third-party apps.因此,它们可以被视为第三方应用程序。

So, you may also want to check FLAG_UPDATED_SYSTEM_APP flag.因此,您可能还想检查FLAG_UPDATED_SYSTEM_APP标志。

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.如果我执行 pkgAppsList.get(0),它会返回一个 ResolveInfo 对象。 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);

Answering this for android 11/API 30为 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">

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.如果您想知道如何在 Kotlin 中执行此操作,如下所示,尽管正如 Ketan sangle 之前提到的,您需要添加<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission"/>在您的 AndroidManifest.xml 文件中。

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在这种情况下,我使用系统标志来排除系统应用程序,您可以在此处找到其他标志

user用户

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

Android PackageManager class is used to retrieve information on the application packages that are currently installed on the device. Android PackageManager 类用于检索有关当前安装在设备上的应用程序包的信息。 You can get an instance of PackageManager class by calling getPackageManager().您可以通过调用 getPackageManager() 获取 PackageManager 类的实例。 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 提供了查询和操作已安装包和相关权限等的方法。在这个Android 示例中,我们获取了Android 中已安装应用程序的列表。

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

packageManager.getInstalledApplications() return a List of all application packages that are installed on the device. packageManager.getInstalledApplications() 返回设备上安装的所有应用程序包的列表。 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.如果我们设置标志 GET_UNINSTALLED_PACKAGES 已设置,将返回所有应用程序的列表,包括使用 DONT_DELETE_DATA 删除的应用程序(部分安装的带有数据目录的应用程序)。

Full info here.完整信息在这里。

Another good read here.另一个很好的阅读在这里。

There is a very useful library Called AndroidUtils, which will help to get it just write one line code.有一个非常有用的库,叫做 AndroidUtils,它可以帮助你只写一行代码。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM