该接口的bundleFlags参数传入GET_BUNDLE_INFO_WITH_APPLICATION可以获取返回的BundleInfo中的ApplicationInfo,其中systemApp字段标识应用是否为系统应用,取值为true表示系统应用,取值为false表示非系统应用。
注意:调用该接口需要ohos.permission.GET_BUNDLE_INFO_PRIVILEGED权限,该权限为系统级别权限,API 7-13该权限仅向系统应用开放;从API 14开始,开放范围从系统应用变更为企业普通应用。
关键代码如下:
import { bundleManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct IsSystemApp {
build() {
Column({ space: 10 }) {
Button('华为钱包是系统应用吗')
.onClick(() => {
let bundleName = 'com.huawei.hmos.wallet'; // 应用包名
let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION; // 用于获取包含appInfo的bundleInfo
let userId = 100;
try {
bundleManager.getBundleInfo(bundleName, bundleFlags, userId, (err, data) => {
if (err) {
console.error(`getBundleInfo failed: ${err.message}`);
} else {
console.info(`getBundleInfo successfully, isSystemApp: ${data.appInfo.systemApp}`);
}
});
} catch (err) {
let message = (err as BusinessError).message;
console.error(`getBundleInfo failed: ${message}`);
}
});
}
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
} 根据此命令返回的applicationInfo中的isSystemApp字段判断是否为系统应用,isSystemApp为true,则为系统应用。