Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
3365fd21
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看板
未验证
提交
3365fd21
编写于
6月 13, 2022
作者:
O
openharmony_ci
提交者:
Gitee
6月 13, 2022
浏览文件
操作
浏览文件
下载
差异文件
!5185 ServiceAbility里添加IDL自动生成方式说明
Merge pull request !5185 from 刘宝/master
上级
0f1e44b2
c303aa64
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
103 addition
and
92 deletion
+103
-92
zh-cn/application-dev/ability/fa-serviceability.md
zh-cn/application-dev/ability/fa-serviceability.md
+103
-92
未找到文件。
zh-cn/application-dev/ability/fa-serviceability.md
浏览文件 @
3365fd21
...
...
@@ -121,115 +121,126 @@ let promise = featureAbility.startAbility(
如果Service需要与Page Ability或其他应用的Service Ability进行交互,则须创建用于连接的Connection。Service支持其他Ability通过connectAbility()方法与其进行连接。
在使用connectAbility()处理回调时,需要传入目标Service的Want与IAbilityConnection的实例。IAbilityConnection提供了以下方法供开发者实现:onConnect()是用来处理连接Service成功的回调,onDisconnect()是用来处理Service异常死亡的回调,onFailed()是用来处理连接Service失败的回调。
创建连接本地Service回调实例的代码示例如下:
开发者可使用如下两种方式实现连接Service。
```
javascript
import
prompt
from
'
@system.prompt
'
1.
使用IDL自动生成代码
let
mRemote
;
function
onConnectCallback
(
element
,
remote
){
console
.
log
(
'
onConnectLocalService onConnectDone element:
'
+
element
);
console
.
log
(
'
onConnectLocalService onConnectDone remote:
'
+
remote
);
mRemote
=
remote
;
if
(
mRemote
==
null
)
{
prompt
.
showToast
({
message
:
"
onConnectLocalService not connected yet
"
});
return
;
}
let
option
=
new
rpc
.
MessageOption
();
let
data
=
new
rpc
.
MessageParcel
();
let
reply
=
new
rpc
.
MessageParcel
()
;
data
.
writeInt
(
1
);
data
.
writeInt
(
99
);
mRemote
.
sendRequest
(
1
,
data
,
reply
,
option
).
then
((
result
)
=>
{
console
.
log
(
'
sendRequest success
'
)
;
let
msg
=
reply
.
readInt
();
使用OpenHarmony IDL(OpenHarmony Interface Definition Language)来自动生成对应客户端服务端及IRemoteObject代码,具体示例代码和说明请参考:
-
[
`OpenHarmony IDL`:TS开发步骤
](
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/IDL/idl-guidelines.md#32-ts开发步骤
)
2.
在对应文件编写代码
在使用connectAbility()处理回调时,需要传入目标Service的Want与IAbilityConnection的实例。IAbilityConnection提供了以下方法供开发者实现:onConnect()是用来处理连接Service成功的回调,onDisconnect()是用来处理Service异常死亡的回调,onFailed()是用来处理连接Service失败的回调。
创建连接本地Service回调实例的代码示例如下:
```javascript
import prompt from '@system.prompt'
let
mRemote
;
function onConnectCallback(element, remote){
console.log('onConnectLocalService onConnectDone element: ' + element
);
console.log('onConnectLocalService onConnectDone remote: ' + remote);
mRemote = remote
;
if (mRemote == null) {
prompt.showToast({
message
:
"
onConnectLocalService connect result:
"
+
msg
,
duration
:
3000
message: "onConnectLocalService not connected yet"
});
return;
}
let option = new rpc.MessageOption();
let data = new rpc.MessageParcel();
let reply = new rpc.MessageParcel();
data.writeInt(1);
data.writeInt(99);
mRemote.sendRequest(1, data, reply, option).then((result) => {
console.log('sendRequest success');
let msg = reply.readInt();
prompt.showToast({
message: "onConnectLocalService connect result: " + msg,
duration: 3000
});
}).catch((e) => {
console.log('sendRequest error:' + e);
});
}).
catch
((
e
)
=>
{
console
.
log
(
'
sendRequest error:
'
+
e
);
});
}
}
function
onDisconnectCallback
(
element
){
console
.
log
(
'
ConnectAbility onDisconnect Callback
'
)
}
function onDisconnectCallback(element){
console.log('ConnectAbility onDisconnect Callback')
}
function
onFailedCallback
(
code
){
console
.
log
(
'
ConnectAbility onFailed Callback
'
)
}
```
function onFailedCallback(code){
console.log('ConnectAbility onFailed Callback')
}
```
连接本地Service的代码示例如下:
连接本地Service的代码示例如下:
```
javascript
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
connId
=
featureAbility
.
connectAbility
(
{
bundleName
:
"
com.jstest.service
"
,
abilityName
:
"
com.jstest.service.ServiceAbility
"
,
},
{
onConnect
:
onConnectCallback
,
onDisconnect
:
onDisconnectCallback
,
onFailed
:
onFailedCallback
,
},
);
```
```javascript
import featureAbility from '@ohos.ability.featureAbility';
let connId = featureAbility.connectAbility(
{
bundleName: "com.jstest.service",
abilityName: "com.jstest.service.ServiceAbility",
},
{
onConnect: onConnectCallback,
onDisconnect: onDisconnectCallback,
onFailed: onFailedCallback,
},
);
```
同时,Service侧也需要在onConnect()时返回IRemoteObject,从而定义与Service进行通信的接口。onConnect()需要返回一个IRemoteObject对象。OpenHarmony提供了IRemoteObject的默认实现,开发者可以通过继承rpc.RemoteObject来创建自定义的实现类。
同时,Service侧也需要在onConnect()时返回IRemoteObject,从而定义与Service进行通信的接口。onConnect()需要返回一个IRemoteObject对象。OpenHarmony提供了IRemoteObject的默认实现,开发者可以通过继承rpc.RemoteObject来创建自定义的实现类。
Service侧把自身的实例返回给调用侧的代码示例如下:
Service侧把自身的实例返回给调用侧的代码示例如下:
```
javascript
import
rpc
from
"
@ohos.rpc
"
;
```javascript
import rpc from "@ohos.rpc";
let
mMyStub
;
export
default
{
onStart
()
{
class
MyStub
extends
rpc
.
RemoteObject
{
constructor
(
des
)
{
if
(
typeof
des
===
'
string
'
)
{
super
(
des
);
let mMyStub;
export default {
onStart() {
class MyStub extends rpc.RemoteObject{
constructor(des) {
if (typeof des === 'string') {
super(des);
}
return null;
}
return
null
;
}
onRemoteRequest
(
code
,
data
,
reply
,
option
)
{
console
.
log
(
"
ServiceAbility onRemoteRequest called
"
);
if
(
code
===
1
)
{
let
op1
=
data
.
readInt
(
);
let
op2
=
data
.
readInt
(
);
console
.
log
(
"
op1 =
"
+
op1
+
"
, op2 =
"
+
op2
);
reply
.
writeInt
(
op1
+
op2
);
}
else
{
console
.
log
(
"
ServiceAbility unknown request code
"
)
;
onRemoteRequest(code, data, reply, option) {
console.log("ServiceAbility onRemoteRequest called");
if (code === 1
) {
let op1 = data.readInt(
);
let op2 = data.readInt();
console.log("op1 = " + op1 + ", op2 = " + op2
);
reply.writeInt(op1 + op2
);
} else {
console.log("ServiceAbility unknown request code"
);
}
return true
;
}
return
true
;
}
}
mMyStub
=
new
MyStub
(
"
ServiceAbility-test
"
);
},
onCommand
(
want
,
startId
)
{
console
.
log
(
'
ServiceAbility onCommand
'
);
},
onConnect
(
want
)
{
console
.
log
(
'
ServiceAbility OnConnect
'
);
return
mMyStub
;
},
onDisconnect
(
want
)
{
console
.
log
(
'
ServiceAbility OnDisConnect
'
);
},
onStop
()
{
console
.
log
(
'
ServiceAbility onStop
'
);
},
}
```
mMyStub = new MyStub("ServiceAbility-test");
},
onCommand(want, startId) {
console.log('ServiceAbility onCommand');
},
onConnect(want) {
console.log('ServiceAbility OnConnect');
return mMyStub;
},
onDisconnect(want) {
console.log('ServiceAbility OnDisConnect');
},
onStop() {
console.log('ServiceAbility onStop');
},
}
```
### 连接远程Service<a name="section126857614019"></a>(当前仅对系统应用开放)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录