• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 启动远程PageAbility(仅对系统应用开放)
2
3
4启动远程PageAbility同样通过featureAbility中的startAbility接口实现。
5
6
7除引入'\@ohos.ability.featureAbility'外,还需引入'\@ohos.distributedHardware.deviceManager',通过DeviceManager(该组件在OpenHarmony上提供帐号无关的分布式设备的认证组网能力)的getTrustedDeviceListSync接口(获取信任设备列表)获取远端的deviceId,写入want中,用于启动远程PageAbility。
8
9
10由于当前DeviceManager的getTrustedDeviceListSync接口仅对系统应用开放,故现阶段非系统应用无法获取其他设备信息,无远程启动设备选择入口,远程启动Ability开发。
11
12
13  **表1** featureAbility接口说明
14
15| 接口名 | 接口描述 |
16| -------- | -------- |
17| startAbility(parameter: StartAbilityParameter) | 启动Ability。 |
18| startAbilityForResult(parameter: StartAbilityParameter) | 启动Ability,并在该Ability被销毁时返回执行结果。 |
19
20
21  **表2** deviceManager接口说明
22
23| 接口名 | 接口描述 |
24| -------- | -------- |
25| getTrustedDeviceListSync(): Array<DeviceInfo> | 同步获取所有可信设备列表。 |
26
27
28在跨设备场景下,启动远程PageAbility首先需要向用户申请数据同步的权限,相关接口说明如下:
29
30
31  **表3** AtManager接口说明
32
33| 接口名 | 接口描述 |
34| -------- | -------- |
35| checkAccessToken(tokenID:&nbsp;number,&nbsp;permissionName:&nbsp;string)<br>:&nbsp;Promise&lt;GrantStatus&gt; | 校验应用是否授予权限。使用Promise异步回调。返回值GrantStatus。建议使用checkAccessToken代替verifyAccessToken(已废弃),verifyAccessToken从API&nbsp;version&nbsp;9开始不再维护。 |
36
37
38  **表4** context接口说明
39
40| 接口名 | 接口描述 |
41| -------- | -------- |
42| requestPermissionsFromUser(permissions:&nbsp;Array&lt;string&gt;,&nbsp;<br>requestCode:&nbsp;number,&nbsp;resultCallback:&nbsp;AsyncCallback&lt;<br>PermissionRequestResult&gt;):&nbsp;void | 以callback形式从系统请求某些权限,详见对应[接口文档](../reference/apis/js-apis-inner-app-context.md#contextrequestpermissionsfromuser7-1)。 |
43
44
45如下示例代码展示了向用户申请数据同步权限的方法:
46
47```ts
48import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
49import featureAbility from '@ohos.ability.featureAbility';
50import bundle from '@ohos.bundle.bundleManager';
51async function RequestPermission() {
52  console.info('RequestPermission begin');
53  let array: Array<string> = ["ohos.permission.DISTRIBUTED_DATASYNC"];
54  let bundleFlag = 0;
55  let tokenID = undefined;
56  let userID = 100;
57  let appInfo = await bundle.getApplicationInfo('ohos.samples.etsDemo', bundleFlag, userID);
58  tokenID = appInfo.accessTokenId;
59  let atManager = abilityAccessCtrl.createAtManager();
60  let requestPermissions: Array<string> = [];
61  for (let i = 0;i < array.length; i++) {
62    let result = await atManager.verifyAccessToken(tokenID, array[i]);
63    console.info("checkAccessToken result:" + JSON.stringify(result));
64    if (result != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
65      requestPermissions.push(array[i]);
66    }
67  }
68  console.info("requestPermissions:" + JSON.stringify(requestPermissions));
69  if (requestPermissions.length == 0 || requestPermissions == []) {
70    return;
71  }
72  let context = featureAbility.getContext();
73  context.requestPermissionsFromUser(requestPermissions, 1, (error, data)=>{
74    console.info("data:" + JSON.stringify(data));
75    console.info("data requestCode:" + data.requestCode);
76    console.info("data permissions:" + data.permissions);
77    console.info("data authResults:" + data.authResults);
78  });
79  console.info('RequestPermission end');
80}
81```
82
83
84在获取数据同步权限后,需要获取可信设备列表,进行设备选择。
85
86
87  如下示例展示了通过getTrustedDeviceListSync获取可信设备列表,选择设备的方法。
88
89```ts
90import deviceManager from '@ohos.distributedHardware.deviceManager';
91
92let dmClass;
93
94function getDeviceManager() {
95  deviceManager.createDeviceManager('ohos.example.distributedService', (error, dm) => {
96    if (error) {
97      console.info('create device manager failed with ' + error)
98    }
99    dmClass = dm;
100  })
101}
102
103function getRemoteDeviceId() {
104  if (typeof dmClass === 'object' && dmClass != null) {
105    let list = dmClass.getTrustedDeviceListSync();
106    if (typeof (list) == 'undefined' || typeof (list.length) == 'undefined') {
107      console.info("EntryAbility onButtonClick getRemoteDeviceId err: list is null");
108      return;
109    }
110    console.info("EntryAbility onButtonClick getRemoteDeviceId success:" + list[0].deviceId);
111    return list[0].deviceId;
112  } else {
113    console.info("EntryAbility onButtonClick getRemoteDeviceId err: dmClass is null");
114  }
115}
116```
117
118
119设备选择完成后,通过调用startAbility接口,显式启动远程PageAbility。
120
121
122如下示例展示了通过startAbility显式启动远程PageAbility的方法。
123
124```ts
125import featureAbility from '@ohos.ability.featureAbility';
126
127function onStartRemoteAbility() {
128  console.info('onStartRemoteAbility begin');
129  let params;
130  let wantValue = {
131    bundleName: 'ohos.samples.etsDemo',
132    abilityName: 'ohos.samples.etsDemo.RemoteAbility',
133    deviceId: getRemoteDeviceId(), // getRemoteDeviceId的定义在前面的示例代码中
134    parameters: params
135  };
136  console.info('onStartRemoteAbility want=' + JSON.stringify(wantValue));
137  featureAbility.startAbility({
138    want: wantValue
139  }).then((data) => {
140    console.info('onStartRemoteAbility finished, ' + JSON.stringify(data));
141  });
142  console.info('onStartRemoteAbility end');
143}
144```
145