如何获取用户在 Android 设备上安装的应用程序列表?
我目前正在使用以下代码:
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
但它返回设备制造商和我都安装的应用程序。如何限制它只返回我安装的应用程序?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
标志:
Flags:
泽利米尔的回答是正确的。但在某些情况下,它不会为您提供所有已安装的第三方应用程序。
ApplicationInfo
还设置了标记FLAG_UPDATED_SYSTEM_APP
在我的智能手机上,此类应用程序包括 Amazone Kindle、Adobe Reader、Slacker Radio 等。这些应用程序不随手机一起提供,而是从 Google Play 商店安装的。因此,它们可以被视为第三方应用程序。
因此,您可能还想检查
FLAG_UPDATED_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 flagFLAG_UPDATED_SYSTEM_APP
which is setOn 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.只需这样做:
Just do this:
尼古拉的答案是正确的,但可以使用迭代器进行优化。这就是我想出的:
Nikolai's answer is correct, but could be optimized using an iterator. This is what I came up with:
针对 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">
如果您想知道如何在 Kotlin 中执行此操作,如下所示,但正如 Ketan sangle 之前提到的,您需要添加
。在本例中,我使用系统标志来排除系统应用程序,您可以找到其他标志
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.In this case I used the system flag to exclude system apps and you can find other flags here
用户
pkgAppsList.get(i).activityInfo.packageName
获取packageName
pkgAppsList.get(i).activityInfo.applicationInfo.loadLabel(getPackageManager()).toString ()
获取应用级别名称
user
pkgAppsList.get(i).activityInfo.packageName
to fetchpackageName
pkgAppsList.get(i).activityInfo.applicationInfo.loadLabel(getPackageManager()).toString()
to
fetch app level name
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.
有一个非常有用的库,名为 AndroidUtils,只需编写一行代码即可实现它。
参考:https://github.com/xihadulislam/androidUtils
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