Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
c7e35649
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看板
未验证
提交
c7e35649
编写于
8月 05, 2022
作者:
O
openharmony_ci
提交者:
Gitee
8月 05, 2022
浏览文件
操作
浏览文件
下载
差异文件
!7699 fa模型service资料修改
Merge pull request !7699 from 黄师伟/master
上级
5be36dc9
2d5a00cc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
93 addition
and
117 deletion
+93
-117
zh-cn/application-dev/ability/fa-serviceability.md
zh-cn/application-dev/ability/fa-serviceability.md
+93
-117
未找到文件。
zh-cn/application-dev/ability/fa-serviceability.md
浏览文件 @
c7e35649
...
...
@@ -32,7 +32,7 @@
},
onConnect
(
want
)
{
console
.
log
(
'
ServiceAbility OnConnect
'
);
return
n
ull
;
return
n
ew
FirstServiceAbilityStub
(
'
test
'
)
;
},
onDisconnect
(
want
)
{
console
.
log
(
'
ServiceAbility OnDisConnect
'
);
...
...
@@ -139,41 +139,36 @@ let promise = featureAbility.startAbility(
```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 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();
var option = {
onConnect: function onConnectCallback(element, proxy) {
console.log(`onConnectLocalService onConnectDone`)
if (proxy === null) {
prompt.showToast({
message: "Connect service failed"
})
return
}
let data = rpc.MessageParcel.create()
let reply = rpc.MessageParcel.create()
let option = new rpc.MessageOption()
data.writeInterfaceToken("connect.test.token")
proxy.sendRequest(0, data, reply, option)
prompt.showToast({
message: "
onConnectLocalService connect result: " + msg,
duration: 3000
});
}).catch((e) =>
{
console.log(
'sendRequest error:' + e);
});
}
function onDisconnectCallback(element)
{
console.log('ConnectAbility onDisconnect Callback'
)
}
function onFailedCallback(code){
console.log('ConnectAbility onFailed Callback')
message: "
Connect service success"
})
},
onDisconnect: function onDisconnectCallback(element)
{
console.log(
`onConnectLocalService onDisconnectDone element:${element}`)
prompt.showToast({
message: "Disconnect service success"
})
},
onFailed: function onFailedCallback(code)
{
console.log(`onConnectLocalService onFailed errCode:${code}`
)
prompt.showToast({
message: "Connect local service onFailed"
})
}
}
```
...
...
@@ -201,44 +196,27 @@ let promise = featureAbility.startAbility(
```javascript
import rpc from "@ohos.rpc";
let mMyStub;
export default {
onStart() {
class MyStub extends rpc.RemoteObject{
constructor(des) {
if (typeof des === 'string') {
super(des);
}
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");
}
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');
},
class FirstServiceAbilityStub extends rpc.RemoteObject {
constructor(des: any) {
if (typeof des === 'string') {
super(des)
} else {
return
}
}
onRemoteRequest(code: number, data: any, reply: any, option: any) {
console.log(printLog + ` onRemoteRequest called`)
if (code === 1) {
let string = data.readString()
console.log(printLog + ` string=${string}`)
let result = Array.from(string).sort().join('')
console.log(printLog + ` result=${result}`)
reply.writeString(result)
} else {
console.log(printLog + ` unknown request code`)
}
return true;
}
```
...
...
@@ -255,40 +233,36 @@ let promise = featureAbility.startAbility(
```
ts
import
prompt
from
'
@system.prompt
'
let
mRemote
;
function
onConnectCallback
(
element
,
remote
){
console
.
log
(
'
onConnectRemoteService onConnectDone element:
'
+
element
);
console
.
log
(
'
onConnectRemoteService onConnectDone remote:
'
+
remote
);
mRemote
=
remote
;
if
(
mRemote
==
null
)
{
prompt
.
showToast
({
message
:
"
onConnectRemoteService 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
();
var
option
=
{
onConnect
:
function
onConnectCallback
(
element
,
proxy
)
{
console
.
log
(
`onConnectRemoteService onConnectDone`
)
if
(
proxy
===
null
)
{
prompt
.
showToast
({
message
:
"
Connect service failed
"
})
return
}
let
data
=
rpc
.
MessageParcel
.
create
()
let
reply
=
rpc
.
MessageParcel
.
create
()
let
option
=
new
rpc
.
MessageOption
()
data
.
writeInterfaceToken
(
"
connect.test.token
"
)
proxy
.
sendRequest
(
0
,
data
,
reply
,
option
)
prompt
.
showToast
({
message
:
"
onConnectRemoteService connect result:
"
+
msg
,
duration
:
3000
});
}).
catch
((
e
)
=>
{
console
.
log
(
'
sendRequest error:
'
+
e
);
});
}
function
onDisconnectCallback
(
element
){
console
.
log
(
'
ConnectRemoteAbility onDisconnect Callback
'
)
}
function
onFailedCallback
(
code
){
console
.
log
(
'
ConnectRemoteAbility onFailed Callback
'
)
message
:
"
Connect service success
"
})
},
onDisconnect
:
function
onDisconnectCallback
(
element
)
{
console
.
log
(
`onConnectRemoteService onDisconnectDone element:
${
element
}
`
)
prompt
.
showToast
({
message
:
"
Disconnect service success
"
})
},
onFailed
:
function
onFailedCallback
(
code
)
{
console
.
log
(
`onConnectRemoteService onFailed errCode:
${
code
}
`
)
prompt
.
showToast
({
message
:
"
Connect local service onFailed
"
})
}
}
```
...
...
@@ -374,23 +348,25 @@ Service侧把自身的实例返回给调用侧的代码示例如下:
```
ts
import
rpc
from
"
@ohos.rpc
"
;
class
FirstServiceAbilityStub
extends
rpc
.
RemoteObject
{
constructor
(
des
)
{
class
FirstServiceAbilityStub
extends
rpc
.
RemoteObject
{
constructor
(
des
:
any
)
{
if
(
typeof
des
===
'
string
'
)
{
super
(
des
)
;
super
(
des
)
}
else
{
return
null
;
return
}
}
onRemoteRequest
(
code
,
data
,
reply
,
option
)
{
console
.
log
(
"
ServiceAbility onRemoteRequest called
"
);
onRemoteRequest
(
code
:
number
,
data
:
any
,
reply
:
any
,
option
:
any
)
{
console
.
log
(
printLog
+
` onRemoteRequest called`
)
if
(
code
===
1
)
{
let
op1
=
data
.
readInt
();
let
op2
=
data
.
readInt
();
console
.
log
(
"
op1 =
"
+
op1
+
"
, op2 =
"
+
op2
);
reply
.
writeInt
(
op1
+
op2
);
let
string
=
data
.
readString
()
console
.
log
(
printLog
+
` string=
${
string
}
`
)
let
result
=
Array
.
from
(
string
).
sort
().
join
(
''
)
console
.
log
(
printLog
+
` result=
${
result
}
`
)
reply
.
writeString
(
result
)
}
else
{
console
.
log
(
"
ServiceAbility unknown request code
"
);
console
.
log
(
printLog
+
` unknown request code`
)
}
return
true
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录