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

发布于 2024-10-10 12:10:03 字数 168 浏览 3 评论 0 原文

我目前正在使用以下代码:

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

但它返回设备制造商和我都安装的应用程序。如何限制它只返回我安装的应用程序?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

染墨丶若流云 2024-10-17 12:10:03
// 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: 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:

酒中人 2024-10-17 12:10:03

泽利米尔的回答是正确的。但在某些情况下,它不会为您提供所有已安装的第三方应用程序。 ApplicationInfo 还设置了标记 FLAG_UPDATED_SYSTEM_APP

如果此应用程序已作为内置系统的更新安装
应用

在我的智能手机上,此类应用程序包括 Amazone Kindle、Adobe Reader、Slacker Radio 等。这些应用程序不随手机一起提供,而是从 Google Play 商店安装的。因此,它们可以被视为第三方应用程序。

因此,您可能还想检查 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
    }
}

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
    }
}
温折酒 2024-10-17 12:10:03

如果我执行 pkgAppsList.get(0),它会返回一个 ResolveInfo 对象。如何获取图标和包名称等信息?

只需这样做:

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)

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)
·深蓝 2024-10-17 12:10:03

尼古拉的答案是正确的,但可以使用迭代器进行优化。这就是我想出的:

/**
 * 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;
}

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;
}
绝情姑娘 2024-10-17 12:10:03
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
关于从前 2024-10-17 12:10:03

针对 android 11/API 30 回答这个问题

context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
上面的代码返回系统应用程序列表,因为用户应用程序默认不可见,您需要在清单中添加以下权限才能获取用户应用程序列表

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

最初的梦 2024-10-17 12:10:03

如果您想知道如何在 Kotlin 中执行此操作,如下所示,但正如 Ketan sangle 之前提到的,您需要添加

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

与风相奔跑 2024-10-17 12:10:03

用户

  1. pkgAppsList.get(i).activityInfo.packageName 获取 packageName
  2. pkgAppsList.get(i).activityInfo.applicationInfo.loadLabel(getPackageManager()).toString ()
    获取应用级别名称

user

  1. pkgAppsList.get(i).activityInfo.packageName to fetch packageName
  2. pkgAppsList.get(i).activityInfo.applicationInfo.loadLabel(getPackageManager()).toString()
    to fetch app level name
南风起 2024-10-17 12:10:03

Android PackageManager 类用于检索设备上当前安装的应用程序包的信息。您可以通过调用 getPackageManager() 来获取 PackageManager 类的实例。 PackageManager提供了查询和操作已安装的包以及相关权限等的方法。在这个Android示例中,我们获取Android中已安装的应用程序的列表。

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

packageManager.getInstalledApplications() 返回设备上安装的所有应用程序包的列表。如果我们设置了 GET_UNINSTALLED_PACKAGES 标志,则将返回所有应用程序的列表,包括使用 DONT_DELETE_DATA 删除的应用程序(带有数据目录的部分安装的应用程序)。

完整信息此处。

另一个好< a href="https://inducesmile.com/android/android-list-installed-apps-in-device-programmatically/" rel="nofollow noreferrer">请阅读此处。

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.

幼儿园老大 2024-10-17 12:10:03

有一个非常有用的库,名为 AndroidUtils,只需编写一行代码即可实现它。

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

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文