HarmonyOS 鸿蒙Next中@ohos.bundle.bundleManager (应用程序包管理模块)
【解决方案】
设备上的已安装应用列表属于用户私隐数据,出于安全隐私考虑,HarmonyOS对外不提供接口获取设备上的已安装的应用列表。应用外获取设备应用列表可以使用hdc命令实现,设备进入shell环境执行hdc shell bm dump -a命令可展示设备已安装包名,更多详细内容可参考社区分享帖如何查看手机里面安装的应用有哪些。
更多关于HarmonyOS 鸿蒙Next中@ohos.bundle.bundleManager (应用程序包管理模块)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
从API version 9开始不再维护,替代接口仅向系统应用开放。
没办法这种接口开发给第三方会带来一些隐私问题
我是系统应用,什么替代接口能获取所有应用列表?getAllBundleInfo这个接口是以前的,要获取ohos.permission.GET_INSTALLED_BUNDLE_LIST这个权限,我申请 了一个调试证书,但是还是用不了这个权限 data permissions:ohos.permission.GET_INSTALLED_BUNDLE_LIST data authResults:2 data dialogShownResults:true data errorReasons:0
小伙伴你好,鸿蒙系统以安全为核心之一,获取设备所有应用列表这个API被废弃是很正常的。
目前想要获取的话只有通过 HDC 命令才能实现:
使用 hdc shell bm dump -a 获取设备上已安装的所有应用包名。
希望HarmonyOS能加强与其他品牌设备的兼容性,让更多人受益。
我是系统应用,没有接口可以使用了嘛?,
@ohos.bundle.bundleManager是HarmonyOS Next中用于管理应用程序包的模块。该模块提供应用包信息的查询、安装、卸载、更新等核心管理能力。开发者可通过此接口获取指定应用的BundleInfo、ApplicationInfo等详细信息,支持应用静默安装与卸载操作。模块遵循HarmonyOS API规范,调用需申请相应权限。
在HarmonyOS Next中,@ohos.bundle.bundleManager 模块是管理应用包信息的核心。由于你拥有 ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 权限,可以获取设备上所有应用的信息。
以下是获取所有应用列表(包含bundleName和ability信息)的核心步骤和代码示例:
-
导入模块
import bundleManager from '@ohos.bundle.bundleManager'; import { BusinessError } from '@ohos.base'; -
获取所有应用的BundleInfo 使用
getAllBundleInfo接口,并指定bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION标志来获取基础信息。为了获取ability信息,需要进一步查询每个应用的详细信息。try { let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; let allBundleInfo: Array<bundleManager.BundleInfo> = await bundleManager.getAllBundleInfo(bundleFlags); // 现在 allBundleInfo 是一个包含所有应用基础信息的数组 } catch (error) { const err: BusinessError = error as BusinessError; console.error(`Failed to get all bundle info. Code: ${err.code}, message: ${err.message}`); } -
遍历并获取每个应用的详细信息(包含Ability信息) 遍历上一步得到的
allBundleInfo数组,对每个应用使用getBundleInfo接口,并指定bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY标志来获取包含Ability的详细信息。// 假设已经从 getAllBundleInfo 获取了 allBundleInfo for (let i = 0; i < allBundleInfo.length; i++) { let bundleName = allBundleInfo[i].name; // 获取 bundleName try { let detailBundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY; let detailBundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfo(bundleName, detailBundleFlags); // 现在 detailBundleInfo 包含了该应用的详细信息 console.log(`BundleName: ${detailBundleInfo.name}`); // 提取并打印 ability 信息 if (detailBundleInfo.abilities) { for (let ability of detailBundleInfo.abilities) { console.log(` Ability Name: ${ability.name}`); console.log(` Ability Label: ${ability.label}`); // 可以访问 ability 的其他属性,如 type, orientation 等 } } } catch (error) { const err: BusinessError = error as BusinessError; console.error(`Failed to get bundle info for ${bundleName}. Code: ${err.code}, message: ${err.message}`); } }
关键点说明:
- 权限:确保在应用的
module.json5配置文件中声明了ohos.permission.GET_BUNDLE_INFO_PRIVILEGED权限,并且该权限的授权方式 (grantMode) 为system_grant。 - 标志位 (BundleFlag):
GET_BUNDLE_INFO_WITH_APPLICATION:用于获取包含应用基本信息(如bundleName、versionCode等)的列表,效率较高。GET_BUNDLE_INFO_WITH_ABILITY:用于获取包含Ability详细信息的BundleInfo。由于你需要ability信息,因此对每个应用都需要使用此标志进行二次查询。
- 性能考虑:
getAllBundleInfo获取基础列表,再循环调用getBundleInfo获取每个应用的详情(含Ability)。如果设备上应用数量非常多,请注意性能开销。
通过以上步骤,你可以组合出设备上所有应用的bundleName及其对应的ability信息列表。

