harmony 鸿蒙Context
Context
Context模块提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。
说明:
- 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
- 本模块接口仅可在Stage模型下使用。
导入模块
import common from '@ohos.app.ability.common';
属性
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
| 名称 | 类型 | 可读 | 可写 | 说明 | 
|---|---|---|---|---|
| resourceManager | resmgr.ResourceManager | 是 | 否 | 资源管理对象。 | 
| applicationInfo | ApplicationInfo | 是 | 否 | 当前应用程序的信息。 | 
| cacheDir | string | 是 | 否 | 缓存目录。 | 
| tempDir | string | 是 | 否 | 临时目录。 | 
| filesDir | string | 是 | 否 | 文件目录。 | 
| databaseDir | string | 是 | 否 | 数据库目录。 | 
| preferencesDir | string | 是 | 否 | preferences目录。 | 
| bundleCodeDir | string | 是 | 否 | 安装包目录。不能拼接路径访问资源文件,请使用资源管理接口访问资源。 | 
| distributedFilesDir | string | 是 | 否 | 分布式文件目录。 | 
| eventHub | EventHub | 是 | 否 | 事件中心,提供订阅、取消订阅、触发事件对象。 | 
| area | contextConstant.AreaMode | 是 | 否 | 文件分区信息。 | 
Context.createBundleContext
createBundleContext(bundleName: string): Context;
根据Bundle名称创建安装包的上下文。
需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
系统能力:SystemCapability.Ability.AbilityRuntime.Core
系统API:该接口为系统接口,三方应用不支持调用。
参数:
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| bundleName | string | 是 | Bundle名称。 | 
返回值:
| 类型 | 说明 | 
|---|---|
| Context | 安装包的上下文。 | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let bundleContext: common.Context;
    try {
      bundleContext = this.context.createBundleContext('com.example.test');
    } catch (error) {
      console.error(`createBundleContext failed, error.code: ${error.code}, error.message: ${error.message}`);
    }
  }
}
Context.createModuleContext
createModuleContext(moduleName: string): Context;
根据模块名创建上下文。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| moduleName | string | 是 | 模块名。 | 
返回值:
| 类型 | 说明 | 
|---|---|
| Context | 模块的上下文。 | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let moduleContext: common.Context;
    try {
      moduleContext = this.context.createModuleContext('entry');
    } catch (error) {
      console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
    }
  }
}
说明:仅支持获取本应用中其他Module的Context和应用内HSP的Context,不支持获取其他应用的Context。
Context.createModuleContext
createModuleContext(bundleName: string, moduleName: string): Context;
根据Bundle名称和模块名称创建上下文。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
系统API:该接口为系统接口,三方应用不支持调用。
参数:
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| bundleName | string | 是 | Bundle名称。 | 
| moduleName | string | 是 | 模块名。 | 
返回值:
| 类型 | 说明 | 
|---|---|
| Context | 模块的上下文。 | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let moduleContext: common.Context;
    try {
      moduleContext = this.context.createModuleContext('com.example.test', 'entry');
    } catch (error) {
      console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
    }
  }
}
Context.getApplicationContext
getApplicationContext(): ApplicationContext;
获取本应用的应用上下文。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
返回值:
| 类型 | 说明 | 
|---|---|
| ApplicationContext | 应用上下文Context。 | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let applicationContext: common.Context;
    try {
      applicationContext = this.context.getApplicationContext();
    } catch (error) {
      console.error('getApplicationContext failed, error.code: ${error.code}, error.message: ${error.message}');
    }
  }
}
Context.getGroupDir10+
getGroupDir(dataGroupID: string): Promise<string>;
通过使用元服务应用中的Group ID获取对应的共享目录,使用Promise异步回调。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| dataGroupID | string | 是 | 元服务应用项目创建时,系统会指定分配唯一Group ID。 | 
返回值:
| 类型 | 说明 | 
|---|---|
| Promise<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。 | 
错误码:
以下错误码详细介绍请参考errcode-ability。
| 错误码ID | 错误信息 | 
|---|---|
| 16000011 | The context does not exist. | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let groupId = "1";
    let getGroupDirContext: common.Context = this.context;
    try {
      getGroupDirContext.getGroupDir(groupId).then(data => {
        console.log("getGroupDir result:" + data);
      })
    } catch (error) {
      console.error('getGroupDirContext failed, error.code: ${error.code}, error.message: ${error.message}');
    }
  }
}
Context.getGroupDir10+
getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void;
通过使用元服务应用中的Group ID获取对应的共享目录,使用callback异步回调。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| dataGroupID | string | 是 | 元服务应用项目创建时,系统会指定分配唯一Group ID。 | 
| callback | AsyncCallback<string> | 是 | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。 | 
错误码:
以下错误码详细介绍请参考errcode-ability。
| 错误码ID | 错误信息 | 
|---|---|
| 16000011 | The context does not exist. | 
示例:
import UIAbility from '@ohos.app.ability.UIAbility';
import common from '@ohos.app.ability.common';
export default class EntryAbility extends UIAbility {
  onCreate() {
    console.log('MyAbility onCreate');
    let getGroupDirContext: common.Context = this.context;
    getGroupDirContext.getGroupDir("1", (err, data) => {
      if (err) {
        console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`);
      } else {
        console.log(`getGroupDir result is: ${JSON.stringify(data)}`);
      }
    });
  }
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙BundleStatusCallback
harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)
harmony 鸿蒙@ohos.distributedBundle (分布式包管理)
harmony 鸿蒙@ohos.bundle (Bundle模块)
harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)
- 所属分类: 后端技术
- 本文标签:
热门推荐
- 
                        2、 - 优质文章
- 
                        3、 gate.io
- 
                        8、 openharmony
- 
                        9、 golang