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** 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** method is available only for system applications. Therefore, non-system applications cannot obtain remote device information or start a remote ability. 11 12**Table 1** featureAbility APIs 13 14| API| Description| 15| -------- | -------- | 16| startAbility(parameter: StartAbilityParameter)| Starts an ability.| 17| startAbilityForResult(parameter: StartAbilityParameter)| Starts an ability and returns the execution result when the ability is terminated.| 18 19**Table 2** deviceManager APIs 20 21| API| Description| 22| -------- | -------- | 23| getTrustedDeviceListSync(): Array<DeviceInfo> | Obtains all trusted devices synchronously.| 24 25 26In 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. 27 28**Table 3** AtManager APIs 29 30| API| Description| 31| -------- | -------- | 32| 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.| 33 34**Table 4** context APIs 35 36| API| Description| 37| -------- | -------- | 38| 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/js-apis-inner-app-context.md#contextrequestpermissionsfromuser7-1).| 39 40 41The following sample code shows how to request the data synchronization permission from users: 42 43```ts 44import abilityAccessCtrl from "@ohos.abilityAccessCtrl"; 45import featureAbility from '@ohos.ability.featureAbility'; 46import bundle from '@ohos.bundle.bundleManager'; 47async function RequestPermission() { 48 console.info('RequestPermission begin'); 49 let array: Array<string> = ["ohos.permission.DISTRIBUTED_DATASYNC"]; 50 let bundleFlag = 0; 51 let tokenID = undefined; 52 let userID = 100; 53 let appInfo = await bundle.getApplicationInfo('ohos.samples.etsDemo', bundleFlag, userID); 54 tokenID = appInfo.accessTokenId; 55 let atManager = abilityAccessCtrl.createAtManager(); 56 let requestPermissions: Array<string> = []; 57 for (let i = 0;i < array.length; i++) { 58 let result = await atManager.verifyAccessToken(tokenID, array[i]); 59 console.info("checkAccessToken result:" + JSON.stringify(result)); 60 if (result != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) { 61 requestPermissions.push(array[i]); 62 } 63 } 64 console.info("requestPermissions:" + JSON.stringify(requestPermissions)); 65 if (requestPermissions.length == 0 || requestPermissions == []) { 66 return; 67 } 68 let context = featureAbility.getContext(); 69 context.requestPermissionsFromUser(requestPermissions, 1, (error, data)=>{ 70 console.info("data:" + JSON.stringify(data)); 71 console.info("data requestCode:" + data.requestCode); 72 console.info("data permissions:" + data.permissions); 73 console.info("data authResults:" + data.authResults); 74 }); 75 console.info('RequestPermission end'); 76} 77``` 78 79 80After obtaining the data synchronization permission, obtain the trusted device list for device selection. 81 82 83The following sample code shows how to use **getTrustedDeviceListSync()** to obtain the trusted device list. 84 85```ts 86import deviceManager from '@ohos.distributedHardware.deviceManager'; 87let dmClass; 88function getDeviceManager() { 89 deviceManager.createDeviceManager('ohos.example.distributedService', (error, dm) => { 90 if (error) { 91 console.info('create device manager failed with ' + error) 92 } 93 dmClass = dm; 94 }) 95} 96function getRemoteDeviceId() { 97 if (typeof dmClass === 'object' && dmClass != null) { 98 let list = dmClass.getTrustedDeviceListSync(); 99 if (typeof (list) == 'undefined' || typeof (list.length) == 'undefined') { 100 console.info("EntryAbility onButtonClick getRemoteDeviceId err: list is null"); 101 return; 102 } 103 console.info("EntryAbility onButtonClick getRemoteDeviceId success:" + list[0].deviceId); 104 return list[0].deviceId; 105 } else { 106 console.info("EntryAbility onButtonClick getRemoteDeviceId err: dmClass is null"); 107 } 108} 109``` 110 111 112After a device is selected, call **startAbility()** to explicitly start the remote PageAbility. 113 114 115The following sample code shows how to explicitly start a remote PageAbility through **startAbility()**. 116 117```ts 118import featureAbility from '@ohos.ability.featureAbility'; 119function onStartRemoteAbility() { 120 console.info('onStartRemoteAbility begin'); 121 let params; 122 let wantValue = { 123 bundleName: 'ohos.samples.etsDemo', 124 abilityName: 'ohos.samples.etsDemo.RemoteAbility', 125 deviceId: getRemoteDeviceId(), // getRemoteDeviceId is defined in the preceding sample code. 126 parameters: params 127 }; 128 console.info('onStartRemoteAbility want=' + JSON.stringify(wantValue)); 129 featureAbility.startAbility({ 130 want: wantValue 131 }).then((data) => { 132 console.info('onStartRemoteAbility finished, ' + JSON.stringify(data)); 133 }); 134 console.info('onStartRemoteAbility end'); 135} 136``` 137