• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Context
2
3Context模块提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 本模块接口仅可在Stage模型下使用。
9
10## 导入模块
11
12```ts
13import common from '@ohos.app.ability.common';
14```
15
16## 属性
17
18**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
19
20| 名称          | 类型     | 可读   | 可写   | 说明      |
21| ----------- | ------ | ---- | ---- | ------- |
22| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | 是    | 否    | 资源管理对象。   |
23| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | 是    | 否    | 当前应用程序的信息。 |
24| cacheDir | string | 是    | 否    | 缓存目录。 |
25| tempDir | string | 是    | 否    | 临时目录。 |
26| filesDir | string | 是    | 否    | 文件目录。 |
27| databaseDir | string | 是    | 否    | 数据库目录。 |
28| preferencesDir | string | 是    | 否    | preferences目录。 |
29| bundleCodeDir | string | 是    | 否    | 安装包目录。不能拼接路径访问资源文件,请使用[资源管理接口](js-apis-resource-manager.md)访问资源。 |
30| distributedFilesDir | string | 是    | 否    | 分布式文件目录。 |
31| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | 是    | 否    | 事件中心,提供订阅、取消订阅、触发事件对象。 |
32| area | [AreaMode](#areamode) | 是    | 否    | 文件分区信息。 |
33
34
35## Context.createBundleContext
36
37createBundleContext(bundleName: string): Context;
38
39根据包名创建安装包的上下文Context。
40
41**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
42
43**系统API**:该接口为系统接口,三方应用不支持调用。
44
45**参数:**
46
47| 名称       | 类型                     | 必填   | 说明            |
48| -------- | ---------------------- | ---- | ------------- |
49| bundleName | string | 是    | 包名。 |
50
51**返回值:**
52
53| 类型 | 说明 |
54| -------- | -------- |
55| Context | 安装包的上下文Context。 |
56
57**示例:**
58
59```ts
60let bundleContext: common.Context;
61try {
62    bundleContext = this.context.createBundleContext('com.example.test');
63} catch (error) {
64    console.error('createBundleContext failed, error.code: ${error.code}, error.message: ${error.message}');
65}
66```
67
68## Context.createModuleContext
69
70createModuleContext(moduleName: string): Context;
71
72根据模块名创建上下文Context。
73
74**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
75
76**参数:**
77
78| 名称       | 类型                     | 必填   | 说明            |
79| -------- | ---------------------- | ---- | ------------- |
80| moduleName | string | 是    | 模块名。 |
81
82**返回值:**
83
84| 类型 | 说明 |
85| -------- | -------- |
86| Context | 上下文Context。 |
87
88**示例:**
89
90```ts
91let moduleContext: common.Context;
92try {
93    moduleContext = this.context.createModuleContext('entry');
94} catch (error) {
95    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
96}
97```
98
99## Context.createModuleContext
100
101createModuleContext(bundleName: string, moduleName: string): Context;
102
103根据包名和模块名创建上下文Context。
104
105**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
106
107**系统API**:该接口为系统接口,三方应用不支持调用。
108
109**参数:**
110
111| 名称       | 类型                     | 必填   | 说明            |
112| -------- | ---------------------- | ---- | ------------- |
113| bundleName | string | 是    | 包名。 |
114| moduleName | string | 是    | 模块名。 |
115
116**返回值:**
117
118| 类型 | 说明 |
119| -------- | -------- |
120| Context | 上下文Context。 |
121
122**示例:**
123
124```ts
125let moduleContext: common.Context;
126try {
127    moduleContext = this.context.createModuleContext('com.example.test', 'entry');
128} catch (error) {
129    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
130}
131```
132
133## Context.getApplicationContext
134
135getApplicationContext(): ApplicationContext;
136
137获取应用上下文Context。
138
139**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
140
141**返回值:**
142
143| 类型 | 说明 |
144| -------- | -------- |
145| Context | 应用上下文Context。 |
146
147**示例:**
148
149```ts
150let applicationContext: common.Context;
151try {
152    applicationContext = this.context.getApplicationContext();
153} catch (error) {
154    console.error('getApplicationContext failed, error.code: ${error.code}, error.message: ${error.message}');
155}
156```
157
158## AreaMode
159
160文件分区
161
162**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
163
164| 名称 | 值 | 说明 |
165| -------- | -------- | -------- |
166| EL1 | 0 | 设备级加密区,设备开机后可访问的数据区。 |
167| EL2 | 1 | 用户级加密区,设备开机,首次输入密码后才能够访问的数据区。 |
168