harmony 鸿蒙Context

2022-12-13 浏览 (1213)

Context

Context模块提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。

说明:

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

导入模块

import common from '@ohos.app.ability.common';

属性

系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

名称类型可读可写说明
resourceManagerresmgr.ResourceManager资源管理对象。
applicationInfoApplicationInfo当前应用程序的信息。
cacheDirstring缓存目录。
tempDirstring临时目录。
filesDirstring文件目录。
databaseDirstring数据库目录。
preferencesDirstringpreferences目录。
bundleCodeDirstring安装包目录。不能拼接路径访问资源文件,请使用资源管理接口访问资源。
distributedFilesDirstring分布式文件目录。
eventHubEventHub事件中心,提供订阅、取消订阅、触发事件对象。
areacontextConstant.AreaMode文件分区信息。

Context.createBundleContext

createBundleContext(bundleName: string): Context;

根据Bundle名称创建安装包的上下文。

需要权限: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

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

系统API:该接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleNamestringBundle名称。

返回值:

类型说明
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

参数:

参数名类型必填说明
moduleNamestring模块名。

返回值:

类型说明
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:该接口为系统接口,三方应用不支持调用。

参数:

参数名类型必填说明
bundleNamestringBundle名称。
moduleNamestring模块名。

返回值:

类型说明
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

参数:

参数名类型必填说明
dataGroupIDstring元服务应用项目创建时,系统会指定分配唯一Group ID。

返回值:

类型说明
Promise<string>以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。

错误码

以下错误码详细介绍请参考errcode-ability

错误码ID错误信息
16000011The 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

参数:

参数名类型必填说明
dataGroupIDstring元服务应用项目创建时,系统会指定分配唯一Group ID。
callbackAsyncCallback<string>以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。

错误码

以下错误码详细介绍请参考errcode-ability

错误码ID错误信息
16000011The 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 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

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