Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
4575310c
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4575310c
编写于
3月 26, 2022
作者:
O
openharmony_ci
提交者:
Gitee
3月 26, 2022
浏览文件
操作
浏览文件
下载
差异文件
!2527 更新Stage模型ability及call文档
Merge pull request !2527 from 张亚菲/zyf
上级
ede87950
ad8fa98b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
88 addition
and
73 deletion
+88
-73
zh-cn/application-dev/ability/stage-ability.md
zh-cn/application-dev/ability/stage-ability.md
+17
-17
zh-cn/application-dev/ability/stage-call.md
zh-cn/application-dev/ability/stage-call.md
+71
-56
未找到文件。
zh-cn/application-dev/ability/stage-ability.md
浏览文件 @
4575310c
...
...
@@ -11,7 +11,7 @@ Stage模型是基于API version 9的应用开发模型,对此模型的介绍
-
应用迁移,详见
[
应用迁移开发指导
](
stage-ability-continuation.md
)
。
## 接口说明
AbilityStage功能如下(AbilityStage类,拥有context属性,具体的API详见
[
接口文档
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev
/reference/apis/js-apis-application-abilitystage.md
)
):
AbilityStage功能如下(AbilityStage类,拥有context属性,具体的API详见
[
接口文档
](
..
/reference/apis/js-apis-application-abilitystage.md
)
):
**表1**
AbilityStage API接口功能介绍
|接口名|描述|
...
...
@@ -20,7 +20,7 @@ AbilityStage功能如下(AbilityStage类,拥有context属性,具体的API
|string onAcceptWant(want: Want)|启动指定Ability时被调用。|
|void onConfigurationUpdated(config: Configuration)|全局配置发生变更时被调用。|
Ability功能如下(
bility类,具体的API详见
[
接口文档
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev
/reference/apis/js-apis-application-ability.md
)
):
Ability功能如下(
Ability类,具体的API详见
[
接口文档
](
..
/reference/apis/js-apis-application-ability.md
)
):
**表2**
Ability API接口功能介绍
|接口名|描述|
...
...
@@ -34,7 +34,7 @@ Ability功能如下(bility类,具体的API详见[接口文档](https://gitee
|void onNewWant(want: Want)|Ability回调,Ability的启动模式设置为单例时被调用。|
|void onConfigurationUpdated(config: Configuration)|Ability回调,Ability的系统配置更新时被调用。|
Ability类拥有context属性,context属性为AbilityContext类,AbilityContext类拥有abilityInfo、currentHapModuleInfo等属性,具体的API详见
[
接口文档
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev
/reference/apis/js-apis-ability-context.md
)
。
Ability类拥有context属性,context属性为AbilityContext类,AbilityContext类拥有abilityInfo、currentHapModuleInfo等属性,具体的API详见
[
接口文档
](
..
/reference/apis/js-apis-ability-context.md
)
。
**表3**
AbilityContext API接口功能介绍
|接口名|描述|
...
...
@@ -70,36 +70,36 @@ Ability类拥有context属性,context属性为AbilityContext类,AbilityConte
import
Ability
from
'
@ohos.application.Ability
'
```
4.
实现Ability生命周期接口。
在
`onWindowStageCreate(windowStage)`
中通过loadContent接口设置应用要加载的页面,window接口的使用详见
[
窗口开发指导
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/windowmanager/window-guidelines.md/
)
。
在
`onWindowStageCreate(windowStage)`
中通过loadContent接口设置应用要加载的页面,window接口的使用详见
[
窗口开发指导
](
../windowmanager/window-guidelines.md
)
。
```
ts
export
default
class
MainAbility
extends
Ability
{
onCreate
(
want
,
launchParam
)
{
console
.
log
(
"
MainAbility onCreate
"
)
}
onDestroy
()
{
console
.
log
(
"
MainAbility onDestroy
"
)
}
onWindowStageCreate
(
windowStage
)
{
console
.
log
(
"
MainAbility onWindowStageCreate
"
)
windowStage
.
loadContent
(
"
pages/index
"
).
then
((
data
)
=>
{
console
.
log
(
"
MainAbility load content succeed with data:
"
+
JSON
.
stringify
(
data
))
}).
catch
((
error
)
=>
{
console
.
error
(
"
MainAbility load content failed with error:
"
+
JSON
.
stringify
(
error
))
})
}
onWindowStageDestroy
()
{
console
.
log
(
"
MainAbility onWindowStageDestroy
"
)
}
onForeground
()
{
console
.
log
(
"
MainAbility onForeground
"
)
}
onBackground
()
{
console
.
log
(
"
MainAbility onBackground
"
)
}
...
...
@@ -202,7 +202,7 @@ function getRemoteDeviceId() {
应用需要某些权限如存储、位置信息、访问日历时,需要向用户申请授权。具体示例代码如下:
```
ts
let
context
=
this
.
context
let
permissions
=
ohos
.
permission
.
READ_CALENDAR
let
permissions
:
Array
<
string
>
=
[
'
ohos.permission.READ_CALENDAR
'
]
context
.
requestPermissionsFromUser
(
permissions
).
then
((
data
)
=>
{
console
.
log
(
"
Succeed to request permission from user with data:
"
+
JSON
.
stringify
(
data
))
}).
catch
((
error
)
=>
{
...
...
@@ -212,7 +212,7 @@ context.requestPermissionsFromUser(permissions).then((data) => {
在跨设备场景下,需要向用户申请数据同步的权限。具体示例代码如下:
```
ts
let
context
=
this
.
context
let
permissions
=
ohos
.
permission
.
DISTRIBUTED_DATASYNC
let
permissions
:
Array
<
string
>
=
[
'
ohos.permission.DISTRIBUTED_DATASYNC
'
]
context
.
requestPermissionsFromUser
(
permissions
).
then
((
data
)
=>
{
console
.
log
(
"
Succeed to request permission from user with data:
"
+
JSON
.
stringify
(
data
))
}).
catch
((
error
)
=>
{
...
...
@@ -221,7 +221,7 @@ context.requestPermissionsFromUser(permissions).then((data) => {
```
### 系统环境变化通知给AbilityStage及Ability
全局配置,比如系统语言和颜色模式发生变化时,通过
onConfigurationUpdated接口通知给AbilityStage和Ability。如下示例展示了AbilityStage的onConfigurationUpdated
回调实现,系统语言和颜色模式发生变化时触发该回调。具体示例代码如下:
全局配置,比如系统语言和颜色模式发生变化时,通过
`onConfigurationUpdated`
接口通知给AbilityStage和Ability。系统应用可以通过
`updateConfiguration`
接口更新系统语言和颜色模式。如下示例展示了AbilityStage的
`onConfigurationUpdated`
回调实现,系统语言和颜色模式发生变化时触发该回调。具体示例代码如下:
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
ConfigurationConstant
from
'
@ohos.application.ConfigurationConstant
'
...
...
@@ -234,7 +234,7 @@ export default class MyAbilityStage extends AbilityStage {
}
```
如下示例展示了Ability的
onConfigurationUpdated
回调实现,系统语言、颜色模式以及Display相关的参数,比如方向、Density,发生变化时触发该回调。具体示例代码如下:
如下示例展示了Ability的
`onConfigurationUpdated`
回调实现,系统语言、颜色模式以及Display相关的参数,比如方向、Density,发生变化时触发该回调。具体示例代码如下:
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
ConfigurationConstant
from
'
@ohos.application.ConfigurationConstant
'
...
...
@@ -253,6 +253,6 @@ export default class MainAbility extends Ability { {
## 开发实例
针对Stage模型Ability开发,有以下示例工程可供参考:
[
eTSStageCallAbility
](
)
[
eTSStageCallAbility
](
https://gitee.com/openharmony/app_samples/tree/master/ability/eTSStageCallAbility
)
本示例eTSStageCallAbility中,在Application目录的AbilityStage.ts中实现AbilityStage的接口,在MainAbility目录实现Ability的接口并设置"pages/index"中的内容为Ability的界面,在SecondAbility目录实现另一个Ability并设置"pages/second"为Ability的界面。支持MainAbility启动SecondAbility。
\ No newline at end of file
本示例eTSStageCallAbility中,在Application目录的AbilityStage.ts中实现AbilityStage的接口,在MainAbility目录实现Ability的接口并设置"pages/index"为Ability的页面,在CaleeAbility目录实现另一个Ability并设置"pages/second"为Ability的页面。支持MainAbility启动CaleeAbility。
zh-cn/application-dev/ability/stage-call.md
浏览文件 @
4575310c
...
...
@@ -9,7 +9,7 @@ Ability Call调用是Ability能力的扩展,它为Ability提供一种能够被
![
stage-call
](
figures/stage-call.png
)
## 接口说明
Caller及Callee功能如下:具体的API详见
[
接口文档
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev
/reference/apis/js-apis-application-ability.md#caller
)
。
Caller及Callee功能如下:具体的API详见
[
接口文档
](
..
/reference/apis/js-apis-application-ability.md#caller
)
。
**表1**
Call API接口功能介绍
|接口名|描述|
...
...
@@ -53,7 +53,7 @@ import Ability from '@ohos.application.Ability'
调用端及被调用端发送接收的数据格式需协商一致,如下示例约定数据由number和string组成。具体示例代码如下:
```
ts
export
class
MySequenceable
{
export
default
class
MySequenceable
{
num
:
number
=
0
str
:
String
=
""
...
...
@@ -77,32 +77,38 @@ export class MySequenceable {
```
4.
实现Callee.on监听及Callee.off解除监听
被调用端Callee的监听函数注册时机, 取决于应用开发者。注册监听之前的数据不会被处理,取消监听之后的数据不会被处理。如下示例在Ability的onCreate注册'CalleeSortMethod'监听,在onDestroy取消监听,收到序列化数据后
对字符串排序后
返回,应用开发者根据实际需要做相应处理。具体示例代码如下:
被调用端Callee的监听函数注册时机, 取决于应用开发者。注册监听之前的数据不会被处理,取消监听之后的数据不会被处理。如下示例在Ability的onCreate注册'CalleeSortMethod'监听,在onDestroy取消监听,收到序列化数据后
作相应处理并
返回,应用开发者根据实际需要做相应处理。具体示例代码如下:
```
ts
let
TAG
=
'
[CalleeAbility]
'
let
method
=
'
CalleeSortMethod
'
function
CalleeSortFunc
(
data
)
{
let
receiveData
=
new
MySequenceable
(
0
,
''
)
data
.
readSequenceable
(
receiveData
)
console
.
log
(
TAG
+
'
receiveData[
'
+
receiveData
.
num
+
'
,
'
+
receiveData
.
str
+
'
]
'
)
return
new
MySequenceable
(
receiveData
.
num
+
1
,
Array
.
from
(
receiveData
.
str
).
sort
().
join
(
''
))
const
TAG
:
string
=
'
[CalleeAbility]
'
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
function
sendMsgCallback
(
data
)
{
Logger
.
log
(
TAG
,
'
CalleeSortFunc called
'
)
// 获取Caller发送的序列化数据
let
receivedData
=
new
MySequenceable
(
0
,
''
)
data
.
readSequenceable
(
receivedData
)
Logger
.
log
(
TAG
,
`receiveData[
${
receivedData
.
num
}
,
${
receivedData
.
str
}
]`
)
// 作相应处理
// 返回序列化数据result给Caller
return
new
MySequenceable
(
receivedData
.
num
+
1
,
`send
${
receivedData
.
str
}
succeed`
)
}
export
default
class
CalleeAbility
extends
Ability
{
onCreate
(
want
,
launchParam
)
{
try
{
this
.
callee
.
on
(
method
,
CalleeSortFunc
)
this
.
callee
.
on
(
MSG_SEND_METHOD
,
sendMsgCallback
)
}
catch
(
error
)
{
console
.
error
(
TAG
+
method
+
'
register failed with error:
'
+
JSON
.
stringify
(
error
)
)
Logger
.
error
(
TAG
,
`
${
MSG_SEND_METHOD
}
register failed with error
${
JSON
.
stringify
(
error
)}
`
)
}
}
onDestroy
()
{
try
{
this
.
callee
.
off
(
method
)
this
.
callee
.
off
(
MSG_SEND_METHOD
)
}
catch
(
error
)
{
console
.
error
(
TAG
+
method
+
'
unregister failed with error:
'
+
JSON
.
stringify
(
error
)
)
console
.
error
(
TAG
,
`
${
MSG_SEND_METHOD
}
unregister failed with error
${
JSON
.
stringify
(
error
)}
`
)
}
}
}
...
...
@@ -117,26 +123,25 @@ import Ability from '@ohos.application.Ability'
Ability的context属性实现了startAbilityByCall方法,用于获取指定通用组件的Caller通信接口。如下示例通过
`this.context`
获取Ability实例的context属性,使用startAbilityByCall拉起Callee被调用端并获取Caller通信接口,注册Caller的onRelease监听。应用开发者根据实际需要做相应处理。具体示例代码如下:
```
ts
let
TAG
=
'
[MainAbility]
'
var
caller
=
undefined
let
context
=
this
.
context
context
.
startAbilityByCall
({
bundleName
:
'
com.samples.CallApplication
'
,
abilityName
:
'
CalleeAbility
'
}).
then
((
data
)
=>
{
if
(
data
!=
null
)
{
caller
=
data
console
.
log
(
TAG
+
'
get caller success
'
)
// 注册caller的release监听
caller
.
onRelease
((
msg
)
=>
{
console
.
log
(
TAG
+
'
caller onRelease is called
'
+
msg
)
async
onButtonGetCaller
()
{
try
{
this
.
caller
=
await
context
.
startAbilityByCall
({
bundleName
:
'
com.samples.CallApplication
'
,
abilityName
:
'
CalleeAbility
'
})
console
.
log
(
TAG
+
'
caller register OnRelease succeed
'
)
if
(
this
.
caller
===
undefined
)
{
Logger
.
error
(
TAG
,
'
get caller failed
'
)
return
}
Logger
.
log
(
TAG
,
'
get caller success
'
)
this
.
regOnRelease
(
this
.
caller
)
}
catch
(
error
)
{
Logger
.
error
(
TAG
,
`get caller failed with
${
error
}
`
)
}
}
)
.
catch
((
error
)
=>
{
}.
catch
((
error
)
=>
{
console
.
error
(
TAG
+
'
get caller failed with
'
+
error
)
})
```
在跨设备场景下,需指定对端设备deviceId。具体示例代码如下:
```
ts
let
TAG
=
'
[MainAbility]
'
...
...
@@ -190,46 +195,56 @@ context.requestPermissionsFromUser(permissions).then((data) => {
})
```
3.
发送约定序列化数据
向被调用端发送Sequenceable数据有两种方式,一种是不带返回值,一种是获取被调用端返回的数据,method以及序列化数据需要与被调用端协商一致。如下示例调用Call接口,向Calee被调用端发送数据。具体示例代码如下:
```
ts
let method = 'CalleeSortMethod'
let msg = new MySequenceable(1, 'call_str')
caller.call(method, msg).then(() => {
console.log(TAG + 'caller call succeed')
}).catch((error) => {
console.error(TAG + 'caller call failed with ' + error)
})
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
async
onButtonCall
()
{
try
{
let
msg
=
new
MySequenceable
(
1
,
'
origin_Msg
'
)
await
this
.
caller
.
call
(
MSG_SEND_METHOD
,
msg
)
}
catch
(
error
)
{
Logger
.
error
(
TAG
,
`caller call failed with
${
error
}
`
)
}
}
```
如下示例调用CallWithResult接口,向Calee被调用端发送待处理的数据
,并将method方法处理完毕的数据赋值给callback
。具体示例代码如下:
如下示例调用CallWithResult接口,向Calee被调用端发送待处理的数据
`originMsg`
,并将'CallSendMsg'方法处理完毕的数据赋值给
`backMsg`
。具体示例代码如下:
```
ts
let method = 'CalleeSortMethod'
let msg = new MySequenceable(1, sortString)
caller.callWithResult(method, msg)
.then((data) => {
let resultMsg = new MySequenceable(0, '')
data.readSequenceable(resultMsg)
callback(resultMsg.str)
console.log(TAG + 'caller result is [' + resultMsg.num + ',' + resultMsg.str + ']')
}).catch((error) => {
console.error(TAG + 'caller callWithResult failed with ' + error)
})
const
MSG_SEND_METHOD
:
string
=
'
CallSendMsg
'
originMsg
:
string
=
''
backMsg
:
string
=
''
async
onButtonCallWithResult
(
originMsg
,
backMsg
)
{
try
{
let
msg
=
new
MySequenceable
(
1
,
originMsg
)
const
data
=
await
this
.
caller
.
callWithResult
(
MSG_SEND_METHOD
,
msg
)
Logger
.
log
(
TAG
,
'
caller callWithResult succeed
'
)
let
result
=
new
MySequenceable
(
0
,
''
)
data
.
readSequenceable
(
result
)
backMsg
(
result
.
str
)
Logger
.
log
(
TAG
,
`caller result is [
${
result
.
num
}
,
${
result
.
str
}
]`
)
}
catch
(
error
)
{
Logger
.
error
(
TAG
,
`caller callWithResult failed with
${
error
}
`
)
}
}
```
4.
释放Caller通信接口
Caller不再使用后,应用开发者可以通过release接口释放Caller。具体示例代码如下:
```
ts
try
{
caller.release()
caller = undefined
console.log(TAG +
'caller release succeed')
this
.
caller
.
release
()
this
.
caller
=
undefined
Logger
.
log
(
TAG
,
'
caller release succeed
'
)
}
catch
(
error
)
{
console.error(TAG + 'caller release failed with ' + error
)
Logger
.
error
(
TAG
,
`caller release failed with
${
error
}
`
)
}
```
## 开发实例
针对Stage模型本地Call功能开发,有以下示例工程可供参考:
[eTSStageCallAbility]()
[
eTSStageCallAbility
](
https://gitee.com/openharmony/app_samples/tree/master/ability/eTSStageCallAbility
)
本示例eTSStageCallAbility中,在Application目录的AbilityStage.ts中实现AbilityStage的接口,在MainAbility目录实现Ability的接口并设置"pages/index"为应用的界面,在CaleeAbility目录实现Callee被调用端。MainAbility获取Caller通信接口后,支持用户输入字符串,做序列化处理后传递给CaleeAbility被调用端处理,Calee将字符串排序,返回序列化数据并将排序结果显示在页面上。
\ No newline at end of file
本示例eTSStageCallAbility中,在Application目录的AbilityStage.ts中实现AbilityStage的接口,在MainAbility目录实现Ability的接口并设置"pages/index"为Ability的页面,在CaleeAbility目录实现Ability的接口、Callee被调用端,设置"pages/second"为Ability的页面。MainAbility作为调用端,CalleeAbility作为被调用端。MainAbility拉起CalleeAbility,获取Caller通信接口后,支持用户输入字符串,做序列化处理后传递给CaleeAbility处理,CaleeAbility根据收到的数据做页面刷新并返回结果给MainAbility。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录