Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
de2ab2d5
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
de2ab2d5
编写于
5月 18, 2022
作者:
O
openharmony_ci
提交者:
Gitee
5月 18, 2022
浏览文件
操作
浏览文件
下载
差异文件
!4081 修改Context资料问题
Merge pull request !4081 from jerry/master
上级
1be97dc1
fbe4fd81
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
35 addition
and
110 deletion
+35
-110
zh-cn/application-dev/ability/context-userguide.md
zh-cn/application-dev/ability/context-userguide.md
+35
-110
未找到文件。
zh-cn/application-dev/ability/context-userguide.md
浏览文件 @
de2ab2d5
...
...
@@ -11,7 +11,7 @@
**FA模型**
只有app/Context中的方法属于FA模型对应的Context。该模式下,应用级别的Context和Ability级别的Context都是该类型的实例,如果在应用级别的Context里面调用了Ability级别的方法,会产生错误。所以开发者需要注意context实例所代表的实际含义。
**Stage模型**
除了app/Context之外的Context都属于Stage模型,分别有application/Context、application/ApplicationContext、application/AbilityStageContext、application/ExtensionContext、application/AbilityContext、application/FormExtensionContext
和application/ServiceExtensionContext七种
Context。这些Context的介绍及使用方式将会在
[
Stage模型和Context详细介绍
](
#stage模型和context详细介绍
)
种进行说明。
**Stage模型**
除了app/Context之外的Context都属于Stage模型,分别有application/Context、application/ApplicationContext、application/AbilityStageContext、application/ExtensionContext、application/AbilityContext、application/FormExtensionContext
等
Context。这些Context的介绍及使用方式将会在
[
Stage模型和Context详细介绍
](
#stage模型和context详细介绍
)
种进行说明。
![
contextIntroduction
](
figures/contextIntroduction.png
)
...
...
@@ -29,26 +29,18 @@
应用的使用方式:
```
javascript
// 1.引入featureAbility
import
featureAbility
from
'
@ohos.ability.featureAbility
'
export
default
{
onCreate
()
{
console
.
log
(
'
Application onCreate
'
)
// 2.获取Context
// 获取context并调用相关方法
let
context
=
featureAbility
.
getContext
();
// 3.调用对应的方法
context
.
setShowOnLockScreen
(
false
,
(
data
)
=>
{
console
.
log
(
"
data:
"
+
JSON
.
stringify
(
data
));
context
.
getBundleName
((
data
,
bundleName
)
=>
{
console
.
info
(
"
ability bundleName:
"
+
bundleName
)
});
},
onActive
()
{
console
.
log
(
'
Application onActive
'
)
console
.
info
(
'
Application onCreate
'
)
},
onDestroy
()
{
console
.
log
(
'
Application onDestroy
'
)
console
.
info
(
'
Application onDestroy
'
)
},
}
```
...
...
@@ -63,28 +55,6 @@ export default {
application/Context类型的Context是基类Context,里面提供了应用的一些基础信息:resourceManager、applicationInfo、cacheDir、area等,还有应用的一些基本方法:createBundleContext等。
**获取方法**
需要在AbilityStage、Ability、Extension等组件中通过context.getApplicationContext()拿到。
**示例**
```
javascript
export
default
class
MainAbility
extends
Ability
{
onCreate
(
want
,
launchParam
)
{
console
.
log
(
'
MainAbility onCreate is called
'
+
want
+
launchParam
);
// 获取ApplicationContext
let
appContext
=
this
.
context
.
getApplicationContext
();
// 获取路径
console
.
log
(
'
filesDir is
'
+
appContext
.
filesDir
);
}
onDestroy
()
{
console
.
log
(
'
MainAbility onDestroy is called
'
);
}
}
```
**d.ts声明**
https://gitee.com/openharmony/interface_sdk-js/blob/master/api/application/Context.d.ts
...
...
@@ -191,91 +161,46 @@ export default class MyAbilityStage extends AbilityStage {
**示例**
```
javascript
import
Ability
from
'
@ohos.application.Ability
'
export
default
class
MainAbility
extends
Ability
{
onCreate
(
want
,
launchParam
)
{
console
.
log
(
'
MainAbility onCreate is called
'
+
want
+
launchParam
);
var
want
=
{
"
bundleName
"
:
"
com.example.MyApplication
"
,
"
abilityName
"
:
"
ServiceExtAbility
"
,
onCreate
(
want
,
launchParam
)
{
console
.
log
(
"
[Demo] MainAbility onCreate
"
)
globalThis
.
abilityWant
=
want
;
}
// 1.这里的Context就是AbilityContext
let
contxt
=
this
.
context
;
// 2.startAbility
contxt
.
startAbility
(
want
).
then
((
data
)
=>
{
console
.
info
(
"
startAbility success:
"
+
JSON
.
stringify
(
data
));
}).
catch
((
error
)
=>
{
console
.
error
(
"
startAbility failed:
"
+
JSON
.
stringify
(
error
));
});
}
onDestroy
()
{
console
.
log
(
"
MainAbility on Destroy is called
"
);
}
}
```
**d.ts声明**
https://gitee.com/openharmony/interface_sdk-js/blob/master/api/application/AbilityContext.d.ts
### application/ExtensionContext
**概述**
和FA模型不同的是,Stage模型把Service从Ability中剥离出来,单独定义了一组通用扩展类Extension用来处理等同的功能。Extension是一个基类,不承担具体业务功能。业务方根据自己的需要去扩展对应的Extension,例如:ServiceAbility扩展为了ServiceExtensionAbility,卡片扩展为了FormExtension。
因此新增了一种和Extension匹配的ExtensionContext。ExtensionContext中包含HapModuleInfo和Configuration两个属性。
**获取方法**
不会单独使用。
**d.ts声明**
https://gitee.com/openharmony/interface_sdk-js/blob/master/api/application/ExtensionContext.d.ts
### application/ServiceExtensionContext
**概述**
ServiceExtensionAbility类似于FA模型的ServiceAbility,里面只有生命周期回调相关的处理。
操作ServiceExtensionAbility的方法移动到了ServiceExtensionContext中(如startAbility、connectAbility等)。
**获取方法**
ServiceExtensionAbility中通过context属性获取。
onDestroy
()
{
console
.
log
(
"
[Demo] MainAbility onDestroy
"
)
}
**示例**
```
javascript
export
default
class
ServiceExtAbility
extends
ServiceExtensionAbility
{
onCreate
(
want
)
{
console
.
info
(
"
ServiceAbility onCreate**
"
);
// 1.这里的Context就是ServiceExtensionContext
let
contxt
=
this
.
context
;
}
onWindowStageCreate
(
windowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] MainAbility onWindowStageCreate
"
)
onRequest
(
want
,
startId
)
{
console
.
info
(
"
ServiceAbility onRequest**
"
)
;
}
// 在这里获取AbilityContext,打印ability的信息
let
context
=
this
.
context
;
console
.
log
(
"
[Demo] MainAbility bundleName
"
+
context
.
abilityInfo
.
bundleName
)
onConnect
(
want
)
{
console
.
info
(
"
ServiceAbility onConnect**
"
);
return
new
StubTest
(
"
test
"
);
}
windowStage
.
setUIContent
(
this
.
context
,
"
pages/index
"
,
null
)
}
onDisconnect
(
want
)
{
console
.
info
(
"
ServiceAbility onDisconnect**
"
);
}
onWindowStageDestroy
()
{
// Main window is destroyed, release UI related resources
console
.
log
(
"
[Demo] MainAbility onWindowStageDestroy
"
)
}
onDestroy
()
{
console
.
info
(
"
ServiceAbility onDestroy**
"
);
}
}
onForeground
()
{
// Ability has brought to foreground
console
.
log
(
"
[Demo] MainAbility onForeground
"
)
}
onBackground
()
{
// Ability has back to background
console
.
log
(
"
[Demo] MainAbility onBackground
"
)
}
};
```
### application/FormExtensionContext
[
FormExtensionContext
](
/zh-cn/application-dev/reference/apis/js-apis-formextensioncontext.md
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录