openharmony 鸿蒙 js-apis-freeInstall-sys

2025-06-12 浏览 (1)

@ohos.bundle.freeInstall (freeInstall模块)(系统接口)

本模块提供免安装相关的设置和查询能力,支持BundlePackInfo、DispatchInfo等信息的查询。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块为系统接口。

导入模块

import { freeInstall } from '@kit.AbilityKit';

权限列表

权限权限等级描述
ohos.permission.GET_BUNDLE_INFO_PRIVILEGEDsystem_basic可查询所有应用信息。
ohos.permission.INSTALL_BUNDLEsystem_core允许应用安装、卸载其他应用(除了企业相关应用,目前有企业InHouse应用,企业MDM应用和企业normal应用)。

权限等级参考权限APL等级说明

UpgradeFlag

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

名称说明
NOT_UPGRADE0模块无需升级。
SINGLE_UPGRADE1单个模块需要升级。
RELATION_UPGRADE2关系模块需要升级。

BundlePackFlag

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

名称说明
GET_PACK_INFO_ALL0x00000000获取应用包pack.info的所有信息。
GET_PACKAGES0x00000001获取应用包pack.info的package信息。
GET_BUNDLE_SUMMARY0x00000002获取应用包pack.info的bundle摘要信息。
GET_MODULE_SUMMARY0x00000004获取应用包pack.info的module摘要信息。

freeInstall.setHapModuleUpgradeFlag

setHapModuleUpgradeFlag(bundleName: string, moduleName: string, upgradeFlag: UpgradeFlag, callback: AsyncCallback<void>):void

设置指定模块是否升级。使用callback异步回调。

需要权限: ohos.permission.INSTALL_BUNDLE

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用Bundle名称。
moduleNamestring应用程序模块名称。
upgradeFlagUpgradeFlag仅供内部系统使用标志位。
callbackAsyncCallback<void>回调函数。当函数调用成功,err为null,否则为错误对象。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.
17700002The specified module name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
let bundleName = 'com.example.myapplication';
let moduleName = 'entry';
let upgradeFlag = freeInstall.UpgradeFlag.SINGLE_UPGRADE;
try {
    freeInstall.setHapModuleUpgradeFlag(bundleName, moduleName, upgradeFlag, err => {
        if (err) {
            console.error('Operation failed:' + JSON.stringify(err));
        } else {
            console.info('Operation succeed');
        }
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

setHapModuleUpgradeFlag

setHapModuleUpgradeFlag(bundleName: string, moduleName: string, upgradeFlag: UpgradeFlag): Promise<void>

设置指定模块是否升级。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.INSTALL_BUNDLE

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用Bundle名称。
moduleNamestring应用程序模块名称。
upgradeFlagUpgradeFlag仅供内部系统使用标志位。

返回值:

类型说明
Promise<void>Promise对象。无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.
17700002The specified module name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';
let bundleName = 'com.example.myapplication';
let moduleName = 'entry';
let upgradeFlag = freeInstall.UpgradeFlag.SINGLE_UPGRADE;
try {
    freeInstall.setHapModuleUpgradeFlag(bundleName, moduleName, upgradeFlag).then(() => {
        console.info('Operation succeed')
    }).catch((err: BusinessError) => {
        console.error('Operation failed:' + JSON.stringify(err));
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

isHapModuleRemovable

isHapModuleRemovable(bundleName: string, moduleName: string, callback: AsyncCallback<boolean>): void

查询指定模块是否可以被移除。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用Bundle名称。
moduleNamestring应用程序模块名称。
callbackAsyncCallback<boolean>回调函数。返回true表示可以移除;返回false表示不可移除。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.
17700002The specified module name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
let bundleName = 'com.example.myapplication';
let moduleName = 'entry';
try {
    freeInstall.isHapModuleRemovable(bundleName, moduleName, (err, data) => {
        if (err) {
            console.error('Operation failed:' + JSON.stringify(err));
        } else {
            console.info('Operation succeed:' + JSON.stringify(data));
        }
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

isHapModuleRemovable

isHapModuleRemovable(bundleName: string, moduleName: string): Promise<boolean>

查询指定模块是否可以被移除。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用Bundle名称。
moduleNamestring应用程序模块名称。

返回值:

类型说明
Promise<boolean>Promise对象。返回true表示可以移除;返回false表示不可移除。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.
17700002The specified module name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';
let bundleName = 'com.example.myapplication';
let moduleName = 'entry';
try {
    freeInstall.isHapModuleRemovable(bundleName, moduleName).then(data => {
        console.info('Operation succeed:' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
        console.error('Operation failed:' + JSON.stringify(err));
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

getBundlePackInfo

getBundlePackInfo(bundleName: string, bundlePackFlag : BundlePackFlag, callback: AsyncCallback<BundlePackInfo>): void

基于bundleName和bundlePackFlag来获取bundlePackInfo。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用Bundle名称。
bundlePackFlagBundlePackFlag指示要查询的应用包标志。
callbackAsyncCallback<BundlePackInfo>回调函数。当函数调用成功,err为null,data为获取到的BundlePackInfo信息。否则为错误对象。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
let bundleName = 'com.example.myapplication';
let bundlePackFlag = freeInstall.BundlePackFlag.GET_PACK_INFO_ALL;
try {
    freeInstall.getBundlePackInfo(bundleName, bundlePackFlag, (err, data) => {
        if (err) {
            console.error('Operation failed:' + JSON.stringify(err));
        } else {
            console.info('Operation succeed:' + JSON.stringify(data));
        }
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

getBundlePackInfo

getBundlePackInfo(bundleName: string, bundlePackFlag : BundlePackFlag): Promise<BundlePackInfo>

基于bundleName和BundlePackFlag来获取bundlePackInfo。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
bundleNamestring应用程序Bundle名称。
bundlePackFlagBundlePackFlag指示要查询的应用包标志。

返回值:

类型说明
Promise<BundlePackInfo>Promise对象,返回BundlePackInfo信息。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
801Capability not supported.
17700001The specified bundle name is not found.

示例:

import { freeInstall } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';
let bundleName = 'com.example.myapplication';
let bundlePackFlag = freeInstall.BundlePackFlag.GET_PACK_INFO_ALL;
try {
    freeInstall.getBundlePackInfo(bundleName, bundlePackFlag).then(data => {
        console.info('Operation succeed:' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
        console.error('Operation failed:' + JSON.stringify(err));
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

getDispatchInfo

getDispatchInfo(callback: AsyncCallback<DispatchInfo>): void

获取有关dispatch版本的信息。使用callback异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

参数:

参数名类型必填说明
callbackAsyncCallback<DispatchInfo>回调函数。当函数调用成功,err为null,data为获取到的DispatchInfo信息。否则为错误对象。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
801Capability not supported.

示例:

import { freeInstall } from '@kit.AbilityKit';
try {
    freeInstall.getDispatchInfo((err, data) => {
        if (err) {
            console.error('Operation failed:' + JSON.stringify(err));
        } else {
            console.info('Operation succeed:' + JSON.stringify(data));
        }
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

getDispatchInfo

getDispatchInfo(): Promise<DispatchInfo>

获取有关dispatch版本的信息。使用Promise异步回调。

系统接口: 此接口为系统接口。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力: SystemCapability.BundleManager.BundleFramework.FreeInstall

返回值:

类型说明
Promise<DispatchInfo>Promise对象,返回DispatchInfo信息。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID错误信息
201Permission denied.
202Permission denied, non-system app called system api.
801Capability not supported.

示例:

import { freeInstall } from '@kit.AbilityKit';
import { BusinessError } from '@ohos.base';
try {
    freeInstall.getDispatchInfo().then(data => {
        console.info('Operation succeed:' + JSON.stringify(data));
    }).catch((err: BusinessError) => {
        console.error('Operation failed:' + JSON.stringify(err));
    });
} catch (err) {
    console.error('Operation failed:' + JSON.stringify(err));
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit(程序框架服务)

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_base_common.h

harmony 鸿蒙ability_runtime_common.h

harmony 鸿蒙application_context.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/VmAfWn