1# Starting a Remote PageAbility (for System Applications Only)
2
3
4The **startAbility()** method in the **featureAbility** class is used to start a remote PageAbility.
5
6
7In addition to '\@ohos.ability.featureAbility', you must import '\@ohos.distributedHardware.deviceManager', which provides account-independent distributed device networking capabilities. Then you can use [getTrustedDeviceListSync](../reference/apis-distributedservice-kit/js-apis-device-manager-sys.md#gettrusteddevicelistsync) of the DeviceManager module to obtain the remote device ID and pass the remote device ID in the **want** parameter for starting the remote PageAbility.
8
9
10The [getTrustedDeviceListSync](../reference/apis-distributedservice-kit/js-apis-device-manager-sys.md#gettrusteddevicelistsync) method is available only for system applications. Therefore, non-system applications cannot obtain remote device information or start a remote ability.
11
12
13**Table 1** featureAbility APIs
14
15| API| Description|
16| -------- | -------- |
17| startAbility(parameter: StartAbilityParameter)| Starts an ability.|
18| startAbilityForResult(parameter: StartAbilityParameter)| Starts an ability and returns the execution result when the ability is terminated.|
19
20
21**Table 2** deviceManager APIs
22
23| API| Description|
24| -------- | -------- |
25| getTrustedDeviceListSync(): Array<DeviceInfo> | Obtains all trusted devices synchronously.|
26
27
28In the cross-device scenario, before starting a remote PageAbility, you must request the data synchronization permission. The related APIs are described in the table below.
29
30
31**Table 3** AtManager APIs
32
33| API| Description|
34| -------- | -------- |
35| checkAccessToken(tokenID: number, permissionName: string): Promise<GrantStatus> | Verifies whether a permission is granted to an application. This API uses a promise to return the result **GrantStatus**. You are advised to use **checkAccessToken** instead of **verifyAccessToken**, which is deprecated since API version 9.|
36
37
38**Table 4** context APIs
39
40| API| Description|
41| -------- | -------- |
42| requestPermissionsFromUser(permissions: Array<string>, requestCode: number, resultCallback: AsyncCallback< PermissionRequestResult>): void | Requests permissions from the system. This API uses an asynchronous callback to return the result. For details, see [API Reference](../reference/apis-ability-kit/js-apis-inner-app-context.md#contextrequestpermissionsfromuser7-1).|
43
44
45The following sample code shows how to request the data synchronization permission from users:
46
47```ts
48import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
49import featureAbility from '@ohos.ability.featureAbility';
50import bundle from '@ohos.bundle.bundleManager';
51import hilog from '@ohos.hilog';
52
53const TAG: string = 'PagePageAbilitySecond'
54const domain: number = 0xFF00;
55
56@Entry
57@Component
58struct PagePageAbilitySecond {
59  async requestPermission(): Promise<void> {
60    hilog.info(domain, TAG, 'RequestPermission begin');
61    let array: Array<string> = ['ohos.permission.DISTRIBUTED_DATASYNC'];
62    let bundleFlag = 0;
63    let tokenID: number | undefined = undefined;
64    let userID = 100;
65    let appInfo = await bundle.getApplicationInfo('com.samples.famodelabilitydevelop', bundleFlag, userID);
66    tokenID = appInfo.accessTokenId;
67    let atManager = abilityAccessCtrl.createAtManager();
68    let requestPermissions: Array<string> = [];
69    for (let i = 0;i < array.length; i++) {
70      let result = await atManager.verifyAccessToken(tokenID, array[i]);
71      hilog.info(domain, TAG, 'checkAccessToken result:' + JSON.stringify(result));
72      if (result != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
73        requestPermissions.push(array[i]);
74      }
75    }
76    hilog.info(domain, TAG, 'requestPermissions:' + JSON.stringify(requestPermissions));
77    if (requestPermissions.length == 0) {
78      return;
79    }
80    let context = featureAbility.getContext();
81    context.requestPermissionsFromUser(requestPermissions, 1, (error, data) => {
82      hilog.info(domain, TAG, 'error:' + error.message + ',data:' + JSON.stringify(data));
83      hilog.info(domain, TAG, 'data requestCode:' + data.requestCode);
84      hilog.info(domain, TAG, 'data permissions:' + data.permissions);
85      hilog.info(domain, TAG, 'data authResults:' + data.authResults);
86    });
87    hilog.info(domain, TAG, 'RequestPermission end');
88  }
89
90  build() {
91    //...
92  }
93}
94
95```
96
97
98After obtaining the data synchronization permission, obtain the trusted device list for device selection.
99
100
101The following sample code shows how to use **getAvailableDeviceListSync()** to obtain the trusted device list.
102
103```ts
104import deviceManager from '@ohos.distributedDeviceManager';
105import promptAction from '@ohos.promptAction';
106import hilog from '@ohos.hilog';
107
108const TAG: string = 'PagePageAbilitySecond'
109const domain: number = 0xFF00;
110
111@Entry
112@Component
113struct PagePageAbilitySecond {
114  @State deviceID: string = '';
115
116  getRemoteDeviceId(): void {
117    let dmClass: deviceManager.DeviceManager;
118    dmClass = deviceManager.createDeviceManager('com.samples.famodelabilitydevelop');
119    try {
120      if (typeof dmClass === 'object' && dmClass !== null) {
121        let list = dmClass.getAvailableDeviceListSync();
122        if (typeof (list) == undefined || list.length == 0) {
123          hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: list is null');
124          return;
125        }
126        hilog.info(domain, TAG, `EntryAbility onButtonClick getRemoteDeviceId success[${list.length}]:` + JSON.stringify(list[0]));
127        if (list[0].networkId != undefined) {
128          this.deviceID = list[0].networkId;
129        }
130        promptAction.showToast({
131          message: this.deviceID
132        });
133      } else {
134        hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: dmClass is null');
135      }
136    } catch (error) {
137      hilog.info(domain, TAG, `getRemoteDeviceId error, error=${error}, message=${error.message}`);
138    }
139  }
140
141  build() {
142    //...
143  }
144}
145```
146
147
148After a device is selected, call **startAbility()** to explicitly start the remote PageAbility.
149
150
151The following sample code shows how to explicitly start a remote PageAbility through **startAbility()**.
152
153```ts
154import featureAbility from '@ohos.ability.featureAbility';
155import Want from '@ohos.app.ability.Want';
156import promptAction from '@ohos.promptAction';
157import { BusinessError } from '@ohos.base';
158import hilog from '@ohos.hilog';
159
160const TAG: string = 'PagePageAbilitySecond'
161const domain: number = 0xFF00;
162
163@Entry
164@Component
165struct PagePageAbilitySecond {
166  @State deviceID: string = '';
167
168  onStartRemoteAbility(): void {
169    hilog.info(domain, TAG, 'onStartRemoteAbility begin');
170    let wantValue: Want = {
171      bundleName: 'ohos.samples.distributedmusicplayer',
172      abilityName: 'ohos.samples.distributedmusicplayer.MainAbility',
173      deviceId: this.deviceID, // The method of obtaining this.deviceID is described in the preceding sample code.
174    };
175    hilog.info(domain, TAG, 'onStartRemoteAbility want=' + JSON.stringify(wantValue));
176    featureAbility.startAbility({
177      want: wantValue
178    }).then((data) => {
179      promptAction.showToast({
180        message: 'start_remote_success_toast'
181      });
182      hilog.info(domain, TAG, 'onStartRemoteAbility finished, ' + JSON.stringify(data));
183    }).catch((error: BusinessError) => {
184      promptAction.showToast({
185        message: JSON.stringify(error)
186      });
187      hilog.error(domain, TAG, 'onStartRemoteAbility failed: ' + JSON.stringify(error));
188    });
189    hilog.info(domain, TAG, 'onStartRemoteAbility end');
190  }
191  build() {
192    //...
193  }
194}
195
196```
197