• 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**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
13
14| 名称          | 类型     | 可读   | 可写   | 说明      |
15| ----------- | ------ | ---- | ---- | ------- |
16| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | 是    | 否    | 资源管理对象。   |
17| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | 是    | 否    | 当前应用程序的信息。 |
18| cacheDir | string | 是    | 否    | 缓存目录。 |
19| tempDir | string | 是    | 否    | 临时目录。 |
20| filesDir | string | 是    | 否    | 文件目录。 |
21| databaseDir | string | 是    | 否    | 数据库目录。 |
22| preferencesDir | string | 是    | 否    | preferences目录。 |
23| bundleCodeDir | string | 是    | 否    | 安装包目录。不能拼接路径访问资源文件,请使用[资源管理接口](js-apis-resource-manager.md)访问资源。 |
24| distributedFilesDir | string | 是    | 否    | 分布式文件目录。 |
25| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | 是    | 否    | 事件中心,提供订阅、取消订阅、触发事件对象。 |
26| area | [AreaMode](#areamode) | 是    | 否    | 文件分区信息。 |
27
28
29## Context.createBundleContext
30
31createBundleContext(bundleName: string): Context;
32
33根据包名创建安装包的上下文Context。
34
35**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
36
37**参数:**
38
39| 名称       | 类型                     | 必填   | 说明            |
40| -------- | ---------------------- | ---- | ------------- |
41| bundleName | string | 是    | 包名。 |
42
43**返回值:**
44
45| 类型 | 说明 |
46| -------- | -------- |
47| Context | 安装包的上下文Context。 |
48
49**示例:**
50
51```ts
52let bundleContext = this.context.createBundleContext('com.example.test');
53```
54
55## Context.createModuleContext
56
57createModuleContext(moduleName: string): Context;
58
59根据模块名创建上下文Context。
60
61**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
62
63**参数:**
64
65| 名称       | 类型                     | 必填   | 说明            |
66| -------- | ---------------------- | ---- | ------------- |
67| moduleName | string | 是    | 模块名。 |
68
69**返回值:**
70
71| 类型 | 说明 |
72| -------- | -------- |
73| Context | 上下文Context。 |
74
75**示例:**
76
77```ts
78let moduleContext = this.context.createModuleContext('entry');
79```
80
81## Context.createModuleContext
82
83createModuleContext(bundleName: string, moduleName: string): Context;
84
85根据包名和模块名创建上下文Context。
86
87**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
88
89**参数:**
90
91| 名称       | 类型                     | 必填   | 说明            |
92| -------- | ---------------------- | ---- | ------------- |
93| bundleName | string | 是    | 包名。 |
94| moduleName | string | 是    | 模块名。 |
95
96**返回值:**
97
98| 类型 | 说明 |
99| -------- | -------- |
100| Context | 上下文Context。 |
101
102**示例:**
103
104```ts
105let moduleContext = this.context.createModuleContext('com.example.test', 'entry');
106```
107
108## Context.getApplicationContext
109
110getApplicationContext(): ApplicationContext;
111
112获取应用上下文Context。
113
114**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
115
116**返回值:**
117
118| 类型 | 说明 |
119| -------- | -------- |
120| Context | 应用上下文Context。 |
121
122**示例:**
123
124```ts
125let applicationContext = this.context.getApplicationContext();
126```
127
128## AreaMode
129
130文件分区
131
132**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
133
134| 名称 | 值 | 说明 |
135| -------- | -------- | -------- |
136| EL1 | 0 | 设备级加密区,设备开机后可访问的数据区。 |
137| EL2 | 1 | 用户级加密区,设备开机,首次输入密码后才能够访问的数据区。 |
138