Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Tonvia
Docs
提交
acc94d75
D
Docs
项目概览
Tonvia
/
Docs
与 Fork 源项目一致
Fork自
OpenHarmony / Docs
通知
1
Star
0
Fork
0
代码
文件
提交
分支
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,发现更多精彩内容 >>
未验证
提交
acc94d75
编写于
3月 26, 2022
作者:
O
openharmony_ci
提交者:
Gitee
3月 26, 2022
浏览文件
操作
浏览文件
下载
差异文件
!2582 PageAbility和ServiceAbility开发指导修改
Merge pull request !2582 from ql/master
上级
58d83f99
62e37407
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
84 addition
and
28 deletion
+84
-28
zh-cn/application-dev/ability/fa-pageability.md
zh-cn/application-dev/ability/fa-pageability.md
+11
-11
zh-cn/application-dev/ability/fa-serviceability.md
zh-cn/application-dev/ability/fa-serviceability.md
+73
-17
未找到文件。
zh-cn/application-dev/ability/fa-pageability.md
浏览文件 @
acc94d75
...
@@ -130,8 +130,8 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
...
@@ -130,8 +130,8 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
```
ts
```
ts
function
onStartRemoteAbility
()
{
function
onStartRemoteAbility
()
{
console
.
info
(
'
onStartRemoteAbility begin
'
);
console
.
info
(
'
onStartRemoteAbility begin
'
);
var
params
;
let
params
;
var
wantValue
=
{
let
wantValue
=
{
bundleName
:
'
ohos.samples.etsDemo
'
,
bundleName
:
'
ohos.samples.etsDemo
'
,
abilityName
:
'
ohos.samples.etsDemo.RemoteAbility
'
,
abilityName
:
'
ohos.samples.etsDemo.RemoteAbility
'
,
deviceId
:
getRemoteDeviceId
(),
deviceId
:
getRemoteDeviceId
(),
...
@@ -150,10 +150,10 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
...
@@ -150,10 +150,10 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
从DeviceManager获取
`deviceId`
,具体示例代码如下:
从DeviceManager获取
`deviceId`
,具体示例代码如下:
```
ts
```
ts
import
deviceManager
from
'
@ohos.distributedHardware.deviceManager
'
;
import
deviceManager
from
'
@ohos.distributedHardware.deviceManager
'
;
var
dmClass
;
let
dmClass
;
function
getRemoteDeviceId
()
{
function
getRemoteDeviceId
()
{
if
(
typeof
dmClass
===
'
object
'
&&
dmClass
!=
null
)
{
if
(
typeof
dmClass
===
'
object
'
&&
dmClass
!=
null
)
{
var
list
=
dmClass
.
getTrustedDeviceListSync
();
let
list
=
dmClass
.
getTrustedDeviceListSync
();
if
(
typeof
(
list
)
==
'
undefined
'
||
typeof
(
list
.
length
)
==
'
undefined
'
)
{
if
(
typeof
(
list
)
==
'
undefined
'
||
typeof
(
list
.
length
)
==
'
undefined
'
)
{
console
.
log
(
"
MainAbility onButtonClick getRemoteDeviceId err: list is null
"
);
console
.
log
(
"
MainAbility onButtonClick getRemoteDeviceId err: list is null
"
);
return
;
return
;
...
@@ -168,20 +168,20 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
...
@@ -168,20 +168,20 @@ PageAbility提供命周期回调,开发者可以在`app.js/app.ets`中重写
在跨设备场景下,需要向开发者申请数据同步的权限。具体示例代码如下:
在跨设备场景下,需要向开发者申请数据同步的权限。具体示例代码如下:
```
ts
```
ts
import
a
ccessContro
l
from
"
@ohos.abilityAccessCtrl
"
;
import
a
bilityAccessCtr
l
from
"
@ohos.abilityAccessCtrl
"
;
import
bundle
from
'
@ohos.bundle
'
;
import
bundle
from
'
@ohos.bundle
'
;
async
function
RequestPermission
()
{
async
function
RequestPermission
()
{
console
.
info
(
'
RequestPermission begin
'
);
console
.
info
(
'
RequestPermission begin
'
);
let
array
:
Array
<
string
>
=
[
"
ohos.permission.DISTRIBUTED_DATASYNC
"
];
let
array
:
Array
<
string
>
=
[
"
ohos.permission.DISTRIBUTED_DATASYNC
"
];
var
bundleFlag
=
0
;
let
bundleFlag
=
0
;
var
tokenID
=
undefined
;
let
tokenID
=
undefined
;
var
userID
=
100
;
let
userID
=
100
;
var
appInfo
=
await
bundle
.
getApplicationInfo
(
'
ohos.samples.etsDemo
'
,
bundleFlag
,
userID
);
let
appInfo
=
await
bundle
.
getApplicationInfo
(
'
ohos.samples.etsDemo
'
,
bundleFlag
,
userID
);
tokenID
=
appInfo
.
accessTokenId
;
tokenID
=
appInfo
.
accessTokenId
;
var
atManager
=
abilityAccessCtrl
.
createAtManager
();
let
atManager
=
abilityAccessCtrl
.
createAtManager
();
let
requestPermissions
:
Array
<
string
>
=
[];
let
requestPermissions
:
Array
<
string
>
=
[];
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
var
result
=
await
atManager
.
verifyAccessToken
(
tokenID
,
array
[
i
]);
let
result
=
await
atManager
.
verifyAccessToken
(
tokenID
,
array
[
i
]);
console
.
info
(
"
verifyAccessToken result:
"
+
JSON
.
stringify
(
result
));
console
.
info
(
"
verifyAccessToken result:
"
+
JSON
.
stringify
(
result
));
if
(
result
==
abilityAccessCtrl
.
GrantStatus
.
PERMISSION_GRANTED
)
{
if
(
result
==
abilityAccessCtrl
.
GrantStatus
.
PERMISSION_GRANTED
)
{
}
else
{
}
else
{
...
...
zh-cn/application-dev/ability/fa-serviceability.md
浏览文件 @
acc94d75
...
@@ -78,7 +78,7 @@ Ability为开发者提供了startAbility()方法来启动另外一个Ability。
...
@@ -78,7 +78,7 @@ Ability为开发者提供了startAbility()方法来启动另外一个Ability。
```
javascript
```
javascript
import
featureAbility
from
'
@ohos.ability.featureability
'
;
import
featureAbility
from
'
@ohos.ability.featureability
'
;
var
promise
=
await
featureAbility
.
startAbility
(
let
promise
=
await
featureAbility
.
startAbility
(
{
{
want
:
want
:
{
{
...
@@ -110,10 +110,28 @@ var promise = await featureAbility.startAbility(
...
@@ -110,10 +110,28 @@ var promise = await featureAbility.startAbility(
创建连接本地Service回调实例的代码示例如下:
创建连接本地Service回调实例的代码示例如下:
```
javascript
```
javascript
var
mRemote
;
let
mRemote
;
function
onConnectCallback
(
element
,
remote
){
function
onConnectCallback
(
element
,
remote
){
console
.
log
(
'
ConnectAbility onConnect Callback
'
)
console
.
log
(
'
onConnectLocalService onConnectDone element:
'
+
element
);
console
.
log
(
'
onConnectLocalService onConnectDone remote:
'
+
remote
);
mRemote
=
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
);
await
mRemote
.
sendRequest
(
1
,
data
,
reply
,
option
);
let
msg
=
reply
.
readInt
();
prompt
.
showToast
({
message
:
"
onConnectLocalService connect result:
"
+
msg
,
duration
:
3000
});
}
}
function
onDisconnectCallback
(
element
){
function
onDisconnectCallback
(
element
){
...
@@ -129,7 +147,7 @@ function onFailedCallback(code){
...
@@ -129,7 +147,7 @@ function onFailedCallback(code){
```
javascript
```
javascript
import
featureAbility
from
'
@ohos.ability.featureability
'
;
import
featureAbility
from
'
@ohos.ability.featureability
'
;
var
connId
=
featureAbility
.
connectAbility
(
let
connId
=
featureAbility
.
connectAbility
(
{
{
bundleName
:
"
com.jstest.serviceability
"
,
bundleName
:
"
com.jstest.serviceability
"
,
abilityName
:
"
com.jstest.serviceability.MainAbility
"
,
abilityName
:
"
com.jstest.serviceability.MainAbility
"
,
...
@@ -149,7 +167,7 @@ Service侧把自身的实例返回给调用侧的代码示例如下:
...
@@ -149,7 +167,7 @@ Service侧把自身的实例返回给调用侧的代码示例如下:
```
javascript
```
javascript
import
rpc
from
"
@ohos.rpc
"
;
import
rpc
from
"
@ohos.rpc
"
;
var
mMyStub
;
let
mMyStub
;
export
default
{
export
default
{
onStart
(
want
)
{
onStart
(
want
)
{
class
MyStub
extends
rpc
.
RemoteObject
{
class
MyStub
extends
rpc
.
RemoteObject
{
...
@@ -160,6 +178,16 @@ export default {
...
@@ -160,6 +178,16 @@ export default {
return
null
;
return
null
;
}
}
onRemoteRequest
(
code
,
message
,
reply
,
option
)
{
onRemoteRequest
(
code
,
message
,
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
"
);
mMyStub
=
new
MyStub
(
"
ServiceAbility-test
"
);
...
@@ -190,10 +218,28 @@ export default {
...
@@ -190,10 +218,28 @@ export default {
创建连接远程Service回调实例的代码示例如下:
创建连接远程Service回调实例的代码示例如下:
```
ts
```
ts
var
mRemote
;
let
mRemote
;
function
onConnectCallback
(
element
,
remote
){
function
onConnectCallback
(
element
,
remote
){
console
.
log
(
'
ConnectRemoteAbility onConnect Callback
'
)
console
.
log
(
'
onConnectLocalService onConnectDone element:
'
+
element
);
console
.
log
(
'
onConnectLocalService onConnectDone remote:
'
+
remote
);
mRemote
=
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
);
await
mRemote
.
sendRequest
(
1
,
data
,
reply
,
option
);
let
msg
=
reply
.
readInt
();
prompt
.
showToast
({
message
:
"
onConnectLocalService connect result:
"
+
msg
,
duration
:
3000
});
}
}
function
onDisconnectCallback
(
element
){
function
onDisconnectCallback
(
element
){
...
@@ -209,10 +255,10 @@ function onFailedCallback(code){
...
@@ -209,10 +255,10 @@ function onFailedCallback(code){
```
ts
```
ts
import
deviceManager
from
'
@ohos.distributedHardware.deviceManager
'
;
import
deviceManager
from
'
@ohos.distributedHardware.deviceManager
'
;
var
dmClass
;
let
dmClass
;
function
getRemoteDeviceId
()
{
function
getRemoteDeviceId
()
{
if
(
typeof
dmClass
===
'
object
'
&&
dmClass
!=
null
)
{
if
(
typeof
dmClass
===
'
object
'
&&
dmClass
!=
null
)
{
var
list
=
dmClass
.
getTrustedDeviceListSync
();
let
list
=
dmClass
.
getTrustedDeviceListSync
();
if
(
typeof
(
list
)
==
'
undefined
'
||
typeof
(
list
.
length
)
==
'
undefined
'
)
{
if
(
typeof
(
list
)
==
'
undefined
'
||
typeof
(
list
.
length
)
==
'
undefined
'
)
{
console
.
log
(
"
MainAbility onButtonClick getRemoteDeviceId err: list is null
"
);
console
.
log
(
"
MainAbility onButtonClick getRemoteDeviceId err: list is null
"
);
return
;
return
;
...
@@ -229,7 +275,7 @@ function getRemoteDeviceId() {
...
@@ -229,7 +275,7 @@ function getRemoteDeviceId() {
```
ts
```
ts
import
featureAbility
from
'
@ohos.ability.featureability
'
;
import
featureAbility
from
'
@ohos.ability.featureability
'
;
var
connId
=
featureAbility
.
connectAbility
(
let
connId
=
featureAbility
.
connectAbility
(
{
{
deviceId
:
getRemoteDeviceId
(),
deviceId
:
getRemoteDeviceId
(),
bundleName
:
"
ohos.samples.etsDemo
"
,
bundleName
:
"
ohos.samples.etsDemo
"
,
...
@@ -245,20 +291,20 @@ var connId = featureAbility.connectAbility(
...
@@ -245,20 +291,20 @@ var connId = featureAbility.connectAbility(
在跨设备场景下,需要向用户申请数据同步的权限。具体示例代码如下:
在跨设备场景下,需要向用户申请数据同步的权限。具体示例代码如下:
```
ts
```
ts
import
a
ccessContro
l
from
"
@ohos.abilityAccessCtrl
"
;
import
a
bilityAccessCtr
l
from
"
@ohos.abilityAccessCtrl
"
;
import
bundle
from
'
@ohos.bundle
'
;
import
bundle
from
'
@ohos.bundle
'
;
async
function
RequestPermission
()
{
async
function
RequestPermission
()
{
console
.
info
(
'
RequestPermission begin
'
);
console
.
info
(
'
RequestPermission begin
'
);
let
array
:
Array
<
string
>
=
[
"
ohos.permission.DISTRIBUTED_DATASYNC
"
];
let
array
:
Array
<
string
>
=
[
"
ohos.permission.DISTRIBUTED_DATASYNC
"
];
var
bundleFlag
=
0
;
let
bundleFlag
=
0
;
var
tokenID
=
undefined
;
let
tokenID
=
undefined
;
var
userID
=
100
;
let
userID
=
100
;
var
appInfo
=
await
bundle
.
getApplicationInfo
(
'
ohos.samples.etsDemo
'
,
bundleFlag
,
userID
);
let
appInfo
=
await
bundle
.
getApplicationInfo
(
'
ohos.samples.etsDemo
'
,
bundleFlag
,
userID
);
tokenID
=
appInfo
.
accessTokenId
;
tokenID
=
appInfo
.
accessTokenId
;
var
atManager
=
abilityAccessCtrl
.
createAtManager
();
let
atManager
=
abilityAccessCtrl
.
createAtManager
();
let
requestPermissions
:
Array
<
string
>
=
[];
let
requestPermissions
:
Array
<
string
>
=
[];
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
var
result
=
await
atManager
.
verifyAccessToken
(
tokenID
,
array
[
i
]);
let
result
=
await
atManager
.
verifyAccessToken
(
tokenID
,
array
[
i
]);
console
.
info
(
"
verifyAccessToken result:
"
+
JSON
.
stringify
(
result
));
console
.
info
(
"
verifyAccessToken result:
"
+
JSON
.
stringify
(
result
));
if
(
result
==
abilityAccessCtrl
.
GrantStatus
.
PERMISSION_GRANTED
)
{
if
(
result
==
abilityAccessCtrl
.
GrantStatus
.
PERMISSION_GRANTED
)
{
}
else
{
}
else
{
...
@@ -296,6 +342,16 @@ class FirstServiceAbilityStub extends rpc.RemoteObject{
...
@@ -296,6 +342,16 @@ class FirstServiceAbilityStub extends rpc.RemoteObject{
}
}
}
}
onRemoteRequest
(
code
,
data
,
reply
,
option
)
{
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
;
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录