HarmonyOS鸿蒙Next中如何在设备上批量查询已安装的应用列表及其版本信息?
HarmonyOS鸿蒙Next中如何在设备上批量查询已安装的应用列表及其版本信息? MDM 系统需要定期上报设备上安装的 App 清单(包括包名、版本号、安装来源),用于合规审计,怎么才能批量查呢?
通过 HMS Device Management Kit 获取详细信息(含安装来源)
华为提供了更强大的 企业设备管理接口,可通过 MDM Server ↔ HMS ↔ 设备 的架构远程获取应用信息。
步骤:
-
集成 HMS Device Management Kit 到 MDM 客户端
- 在
build.gradle中添加依赖:implementation 'com.huawei.hms:device-management:6.x.x'
- 在
-
调用
ApplicationPolicy.getInstalledApplications()ApplicationPolicy appPolicy = new ApplicationPolicy(context); List<ApplicationInfo> apps = appPolicy.getInstalledApplications(); for (ApplicationInfo app : apps) { String packageName = app.getPackageName(); String versionName = app.getVersionName(); int versionCode = app.getVersionCode(); String installer = app.getInstallerPackageName(); // 安装来源(如 com.huawei.appmarket) }getInstallerPackageName()返回安装器包名:com.huawei.appmarket→ 华为应用市场com.android.shell→ ADB 安装null→ 用户手动安装(如文件管理器)
-
定期上报到 MDM 服务器
- 将上述列表序列化为 JSON,通过 HTTPS 上报至你的合规审计平台。
- 建议使用 JobScheduler 或 WorkManager 定期执行(如每天一次)。
更多关于HarmonyOS鸿蒙Next中如何在设备上批量查询已安装的应用列表及其版本信息?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
【解决方案】
开发者您好,MDM应用的话,开发者可以参考这个接口文档:getInstalledBundleList,该接口可以获取设备指定用户下已安装应用列表信息,开发者可以尝试下是否可以满足您的诉求,不过开发者需要注意这个接口使用前需要通过调用@ohos.account.osAccount中的getOsAccountLocalId接口来获取getInstalledBundleList接口所需要的accountId参数。
这个好像不行, 提工单问问
在HarmonyOS Next中,可通过bundleManager.getBundleInfo接口批量查询应用信息。使用bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION标志获取应用列表,遍历列表并提取versionName或versionCode获取版本信息。需声明ohos.permission.GET_BUNDLE_INFO权限。
在HarmonyOS Next中,批量查询已安装应用列表及其版本信息,可以通过@ohos.bundle.bundleManager和@ohos.bundle.installer等API实现。以下是核心实现方案:
1. 获取所有已安装应用
使用bundleManager.getBundleInfos()接口,可以一次性获取设备上所有应用的BundleInfo信息数组。
import bundleManager from '@ohos.bundle.bundleManager';
import { BusinessError } from '@ohos.base';
try {
// 指定获取所有应用信息,并包含版本信息
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION |
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE;
let bundleInfos: Array<bundleManager.BundleInfo> = await bundleManager.getBundleInfos(bundleFlags);
// 遍历处理每个应用信息
for (let bundleInfo of bundleInfos) {
let appName = bundleInfo.name; // 包名
let versionName = bundleInfo.versionName; // 版本名称
let versionCode = bundleInfo.versionCode; // 版本号
// 获取安装来源等信息需要进一步查询
// ...
}
} catch (error) {
const err: BusinessError = error as BusinessError;
console.error(`Failed to get bundle infos. Code: ${err.code}, message: ${err.message}`);
}
2. 获取应用安装来源
安装来源信息需要通过installer模块查询。对于每个应用包名,可以调用getBundleInstaller()获取安装器信息。
import installer from '@ohos.bundle.installer';
async function getInstallSource(bundleName: string): Promise<string> {
try {
let installerInfo: installer.InstallerInfo = await installer.getBundleInstaller(bundleName);
return installerInfo.installerBundleName; // 返回安装来源包名
} catch (error) {
console.error(`Failed to get installer for ${bundleName}`);
return "Unknown";
}
}
3. 完整上报数据组装
将上述信息组合,形成完整的应用清单数据:
interface AppInventory {
packageName: string; // 包名
versionName: string; // 版本名称
versionCode: number; // 版本号
installSource: string; // 安装来源
}
async function collectAppInventory(): Promise<AppInventory[]> {
let inventory: AppInventory[] = [];
let bundleInfos = await bundleManager.getBundleInfos(
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION |
bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE
);
for (let bundleInfo of bundleInfos) {
let installSource = await getInstallSource(bundleInfo.name);
inventory.push({
packageName: bundleInfo.name,
versionName: bundleInfo.versionName,
versionCode: bundleInfo.versionCode,
installSource: installSource
});
}
return inventory;
}
4. 权限配置
在module.json5配置文件中声明必要权限:
{
"module": {
"requestPermissions": [
{
"name": "ohos.permission.GET_BUNDLE_INFO",
"reason": "MDM compliance audit"
},
{
"name": "ohos.permission.GET_INSTALLED_BUNDLE_LIST",
"reason": "MDM compliance audit"
}
]
}
}
关键点说明:
- 批量获取:
getBundleInfos()接口直接返回所有应用信息数组,无需逐个查询。 - 版本信息:
BundleInfo中的versionName和versionCode字段可直接获取。 - 安装来源:需要通过
installer模块单独查询每个应用的安装来源。 - 性能考虑:批量查询大量应用时,建议在后台任务中执行,避免阻塞主线程。
此方案可直接集成到MDM客户端,定期执行collectAppInventory()函数并上报数据到服务器,满足合规审计要求。

