@ohos.app.ability.application (Application)(系统接口)

开发者可以通过该模块创建Context

说明:

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 本模块接口仅可在Stage模型下使用。

导入模块

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

application.createModuleContext12+

createModuleContext(context: Context, bundleName: string, moduleName: string): Promise<Context>

根据入参Context创建相应模块的Context。使用Promise异步回调。

说明:

  • 从API version 18开始,Context支持获取当前应用的进程名processName。createModuleContext创建的Context中的processName属性与入参Context中的processName属性一致,其他属性根据入参Context、bundleName和moduleName获得相应的属性值。

  • 由于创建模块上下文的过程涉及资源查询与初始化,耗时相对较长,在对应用流畅性要求较高的场景下,不建议频繁或多次调用createModuleContext接口创建多个Context实例,以免影响用户体验。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

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

参数

参数名 类型 必填 说明
context Context 表示应用上下文。
bundleName string 表示应用包名。取值为空字符串时,默认为当前应用。
moduleName string 表示应用模块名。

返回值:

类型 说明
Promise<Context> Promise对象。返回创建的Context。

错误码:

以下错误码详细介绍请参考元能力子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Permission denied, non-system app called system api.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.

示例:

import { UIAbility, application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate() {
    let moduleContext: common.Context;
    try {
      application.createModuleContext(this.context, 'bundlename', 'entry').then((data: Context)=>{
        moduleContext = data;
        console.info('createModuleContext success!');
      }).catch((error : BusinessError)=>{
        console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
      })
    } catch (error) {
      console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
    }
  }
}

application.createBundleContext12+

createBundleContext(context: Context, bundleName: string): Promise<Context>

根据入参Context创建相应应用的Context。使用Promise异步回调。

说明:

从API version 18开始,Context支持获取当前应用的进程名processName。createBundleContext创建的Context中的processName属性与入参Context中的processName属性一致,其他属性根据入参Context、bundleName和moduleName获得相应的属性值。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

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

参数

参数名 类型 必填 说明
context Context 表示应用上下文。
bundleName string 表示应用包名。

返回值:

类型 说明
Promise<Context> Promise对象。返回创建的Context。

错误码:

以下错误码详细介绍请参考元能力子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Permission denied, non-system app called system api.
401 Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.

示例:

import { UIAbility, application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate() {
    let moduleContext: common.Context;
    try {
      application.createBundleContext(this.context, 'bundlename').then((data: Context)=>{
        moduleContext = data;
        console.info('createBundleContext success!');
      }).catch((error : BusinessError)=>{
        console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
      })
    } catch (error) {
      console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
    }
  }
}

application.createPluginModuleContextForHostBundle20+

createPluginModuleContextForHostBundle(context: Context, pluginBundleName: string, pluginModuleName: string, hostBundleName: string): Promise<Context>

根据入参Context、插件包名和插件模块名和应用包名,创建对应插件的Context,用于获取插件的基本信息。使用Promise异步回调。

需要权限:ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

系统能力:SystemCapability.Ability.AbilityRuntime.Core

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

参数

参数名 类型 必填 说明
context Context 表示应用上下文。
pluginBundleName string 表示应用的插件包名。
pluginModuleName string 表示应用的插件模块名。
hostBundleName string 表示安装插件的应用包名。

返回值:

类型 说明
Promise<Context> Promise对象。返回创建的Context,返回的Context中的属性processName和config与入参Context中的属性processName和config的值相同。

错误码

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

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

示例:

import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    let moduleContext: common.Context;
    try {
      application.createPluginModuleContextForHostBundle(this.context, 'com.example.pluginBundleName', 'pluginModuleName', 'com.example.hostBundleName')
        .then((data: Context) => {
          moduleContext = data;
          console.info('createPluginModuleContextForHostBundle success!');
        })
        .catch((error: BusinessError) => {
          let code: number = (error as BusinessError).code;
          let message: string = (error as BusinessError).message;
          console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`);
        });
    } catch (error) {
      let code: number = (error as BusinessError).code;
      let message: string = (error as BusinessError).message;
      console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`);
    }
  }
}