1e41f4b71Sopenharmony_ci# ServiceExtensionContext (系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciServiceExtensionContext模块提供ServiceExtensionAbility具有的能力,包括启动、停止、绑定、解绑Ability。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10e41f4b71Sopenharmony_ci> - 本模块接口仅可在Stage模型下使用。 11e41f4b71Sopenharmony_ci> - 本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。 12e41f4b71Sopenharmony_ci> - 本模块接口为系统接口。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## 导入模块 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci```ts 17e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 18e41f4b71Sopenharmony_ci``` 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci## 使用说明 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**示例:** 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci```ts 27e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 28e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_cilet commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 33e41f4b71Sopenharmony_ci onCreate() { 34e41f4b71Sopenharmony_ci let context = this.context; // 获取ServiceExtensionContext 35e41f4b71Sopenharmony_ci } 36e41f4b71Sopenharmony_ci} 37e41f4b71Sopenharmony_ci``` 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbility 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_cistartAbility(want: Want, callback: AsyncCallback<void>): void; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci启动Ability。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**参数:** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 52e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 53e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 | 54e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**错误码:** 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 61e41f4b71Sopenharmony_ci| ------- | -------- | 62e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 63e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 64e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 65e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 66e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 67e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 68e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 69e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 70e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 71e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 72e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 73e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 74e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 75e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 76e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 77e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 78e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 79e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 80e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 81e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 82e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 83e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 84e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 85e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 86e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 87e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**示例:** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci```ts 92e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 93e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 96e41f4b71Sopenharmony_ci onCreate() { 97e41f4b71Sopenharmony_ci let want: Want = { 98e41f4b71Sopenharmony_ci bundleName: 'com.example.myapp', 99e41f4b71Sopenharmony_ci abilityName: 'MyAbility' 100e41f4b71Sopenharmony_ci }; 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci try { 103e41f4b71Sopenharmony_ci this.context.startAbility(want, (error: BusinessError) => { 104e41f4b71Sopenharmony_ci if (error.code) { 105e41f4b71Sopenharmony_ci // 处理业务逻辑错误 106e41f4b71Sopenharmony_ci console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 107e41f4b71Sopenharmony_ci return; 108e41f4b71Sopenharmony_ci } 109e41f4b71Sopenharmony_ci // 执行正常业务 110e41f4b71Sopenharmony_ci console.log('startAbility succeed'); 111e41f4b71Sopenharmony_ci }); 112e41f4b71Sopenharmony_ci } catch (paramError) { 113e41f4b71Sopenharmony_ci // 处理入参错误异常 114e41f4b71Sopenharmony_ci console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci } 117e41f4b71Sopenharmony_ci} 118e41f4b71Sopenharmony_ci``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbility 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_cistartAbility(want: Want, options?: StartOptions): Promise\<void>; 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci启动Ability,结果以Promise的形式返回。 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci**参数:** 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 133e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 134e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 | 135e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**返回值:** 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci| 类型 | 说明 | 140e41f4b71Sopenharmony_ci| -------- | -------- | 141e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含启动的结果。 | 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**错误码:** 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 148e41f4b71Sopenharmony_ci| ------- | -------- | 149e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 150e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 151e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 152e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 153e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 154e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 155e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 156e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 157e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 158e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 159e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 160e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 161e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 162e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 163e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 164e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 165e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 166e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 167e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 168e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 169e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 170e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 171e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 172e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 173e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 174e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**示例:** 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci```ts 179e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 180e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 183e41f4b71Sopenharmony_ci onCreate() { 184e41f4b71Sopenharmony_ci let want: Want = { 185e41f4b71Sopenharmony_ci bundleName: 'com.example.myapp', 186e41f4b71Sopenharmony_ci abilityName: 'MyAbility' 187e41f4b71Sopenharmony_ci }; 188e41f4b71Sopenharmony_ci let options: StartOptions = { 189e41f4b71Sopenharmony_ci windowMode: 0, 190e41f4b71Sopenharmony_ci }; 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci try { 193e41f4b71Sopenharmony_ci this.context.startAbility(want, options) 194e41f4b71Sopenharmony_ci .then((data: void) => { 195e41f4b71Sopenharmony_ci // 执行正常业务 196e41f4b71Sopenharmony_ci console.log('startAbility succeed'); 197e41f4b71Sopenharmony_ci }) 198e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 199e41f4b71Sopenharmony_ci // 处理业务逻辑错误 200e41f4b71Sopenharmony_ci console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 201e41f4b71Sopenharmony_ci }); 202e41f4b71Sopenharmony_ci } catch (paramError) { 203e41f4b71Sopenharmony_ci // 处理入参错误异常 204e41f4b71Sopenharmony_ci console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci } 207e41f4b71Sopenharmony_ci} 208e41f4b71Sopenharmony_ci``` 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbility 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_cistartAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci启动Ability,结果以Callback的形式返回。 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci**参数:** 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 223e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 224e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 225e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 226e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 | 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci**错误码:** 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 233e41f4b71Sopenharmony_ci| ------- | -------- | 234e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 235e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 236e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 237e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 238e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 239e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 240e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 241e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 242e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 243e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 244e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 245e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 246e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 247e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 248e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 249e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 250e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 251e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 252e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 253e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 254e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 255e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 256e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 257e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 258e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 259e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**示例:** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci```ts 264e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 265e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 268e41f4b71Sopenharmony_ci onCreate() { 269e41f4b71Sopenharmony_ci let want: Want = { 270e41f4b71Sopenharmony_ci deviceId: '', 271e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 272e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 273e41f4b71Sopenharmony_ci }; 274e41f4b71Sopenharmony_ci let options: StartOptions = { 275e41f4b71Sopenharmony_ci windowMode: 0 276e41f4b71Sopenharmony_ci }; 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci try { 279e41f4b71Sopenharmony_ci this.context.startAbility(want, options, (error: BusinessError) => { 280e41f4b71Sopenharmony_ci if (error.code) { 281e41f4b71Sopenharmony_ci // 处理业务逻辑错误 282e41f4b71Sopenharmony_ci console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 283e41f4b71Sopenharmony_ci return; 284e41f4b71Sopenharmony_ci } 285e41f4b71Sopenharmony_ci // 执行正常业务 286e41f4b71Sopenharmony_ci console.log('startAbility succeed'); 287e41f4b71Sopenharmony_ci }); 288e41f4b71Sopenharmony_ci } catch (paramError) { 289e41f4b71Sopenharmony_ci // 处理入参错误异常 290e41f4b71Sopenharmony_ci console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 291e41f4b71Sopenharmony_ci } 292e41f4b71Sopenharmony_ci } 293e41f4b71Sopenharmony_ci} 294e41f4b71Sopenharmony_ci``` 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityWithAccount 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_cistartAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci根据account启动Ability(callback形式)。 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci> **说明:** 303e41f4b71Sopenharmony_ci> 304e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**参数:** 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 315e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 316e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 317e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 318e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci**错误码:** 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 325e41f4b71Sopenharmony_ci| ------- | -------- | 326e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 327e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 328e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 329e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 330e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 331e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 332e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 333e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 334e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 335e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 336e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 337e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 338e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 339e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 340e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 341e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 342e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 343e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 344e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 345e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 346e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 347e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 348e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 349e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 350e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 351e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 352e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci**示例:** 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci```ts 357e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 358e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 361e41f4b71Sopenharmony_ci onCreate() { 362e41f4b71Sopenharmony_ci let want: Want = { 363e41f4b71Sopenharmony_ci deviceId: '', 364e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 365e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 366e41f4b71Sopenharmony_ci }; 367e41f4b71Sopenharmony_ci let accountId = 100; 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci try { 370e41f4b71Sopenharmony_ci this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => { 371e41f4b71Sopenharmony_ci if (error.code) { 372e41f4b71Sopenharmony_ci // 处理业务逻辑错误 373e41f4b71Sopenharmony_ci console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 374e41f4b71Sopenharmony_ci return; 375e41f4b71Sopenharmony_ci } 376e41f4b71Sopenharmony_ci // 执行正常业务 377e41f4b71Sopenharmony_ci console.log('startAbilityWithAccount succeed'); 378e41f4b71Sopenharmony_ci }); 379e41f4b71Sopenharmony_ci } catch (paramError) { 380e41f4b71Sopenharmony_ci // 处理入参错误异常 381e41f4b71Sopenharmony_ci console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`); 382e41f4b71Sopenharmony_ci } 383e41f4b71Sopenharmony_ci } 384e41f4b71Sopenharmony_ci} 385e41f4b71Sopenharmony_ci``` 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityWithAccount 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_cistartAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void; 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci根据account启动Ability(callback形式)。 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci> **说明:** 394e41f4b71Sopenharmony_ci> 395e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci**参数:** 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 406e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 407e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 408e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 409e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 410e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci**错误码:** 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 417e41f4b71Sopenharmony_ci| ------- | -------- | 418e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 419e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 420e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 421e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 422e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 423e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 424e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 425e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 426e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 427e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 428e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 429e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 430e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 431e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 432e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 433e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 434e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 435e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 436e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 437e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 438e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 439e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 440e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 441e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 442e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 443e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 444e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci**示例:** 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci```ts 449e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 450e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 453e41f4b71Sopenharmony_ci onCreate() { 454e41f4b71Sopenharmony_ci let want: Want = { 455e41f4b71Sopenharmony_ci deviceId: '', 456e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 457e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 458e41f4b71Sopenharmony_ci }; 459e41f4b71Sopenharmony_ci let accountId = 100; 460e41f4b71Sopenharmony_ci let options: StartOptions = { 461e41f4b71Sopenharmony_ci windowMode: 0 462e41f4b71Sopenharmony_ci }; 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci try { 465e41f4b71Sopenharmony_ci this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => { 466e41f4b71Sopenharmony_ci if (error.code) { 467e41f4b71Sopenharmony_ci // 处理业务逻辑错误 468e41f4b71Sopenharmony_ci console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 469e41f4b71Sopenharmony_ci return; 470e41f4b71Sopenharmony_ci } 471e41f4b71Sopenharmony_ci // 执行正常业务 472e41f4b71Sopenharmony_ci console.log('startAbilityWithAccount succeed'); 473e41f4b71Sopenharmony_ci }); 474e41f4b71Sopenharmony_ci } catch (paramError) { 475e41f4b71Sopenharmony_ci // 处理入参错误异常 476e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 477e41f4b71Sopenharmony_ci } 478e41f4b71Sopenharmony_ci } 479e41f4b71Sopenharmony_ci} 480e41f4b71Sopenharmony_ci``` 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci 483e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityWithAccount 484e41f4b71Sopenharmony_ci 485e41f4b71Sopenharmony_cistartAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>; 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci根据account启动Ability(Promise形式)。 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci> **说明:** 490e41f4b71Sopenharmony_ci> 491e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci**参数:** 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 502e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 503e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 504e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 | 505e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci**返回值:** 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci| 类型 | 说明 | 510e41f4b71Sopenharmony_ci| -------- | -------- | 511e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**错误码:** 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 518e41f4b71Sopenharmony_ci| ------- | -------- | 519e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 520e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 521e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 522e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 523e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 524e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 525e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 526e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 527e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 528e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 529e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 530e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 531e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 532e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 533e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 534e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 535e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 536e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 537e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 538e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 539e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 540e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 541e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 542e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 543e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 544e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 545e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci**示例:** 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci```ts 550e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 551e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 554e41f4b71Sopenharmony_ci onCreate() { 555e41f4b71Sopenharmony_ci let want: Want = { 556e41f4b71Sopenharmony_ci deviceId: '', 557e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 558e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 559e41f4b71Sopenharmony_ci }; 560e41f4b71Sopenharmony_ci let accountId = 100; 561e41f4b71Sopenharmony_ci let options: StartOptions = { 562e41f4b71Sopenharmony_ci windowMode: 0 563e41f4b71Sopenharmony_ci }; 564e41f4b71Sopenharmony_ci 565e41f4b71Sopenharmony_ci try { 566e41f4b71Sopenharmony_ci this.context.startAbilityWithAccount(want, accountId, options) 567e41f4b71Sopenharmony_ci .then((data: void) => { 568e41f4b71Sopenharmony_ci // 执行正常业务 569e41f4b71Sopenharmony_ci console.log('startAbilityWithAccount succeed'); 570e41f4b71Sopenharmony_ci }) 571e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 572e41f4b71Sopenharmony_ci // 处理业务逻辑错误 573e41f4b71Sopenharmony_ci console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 574e41f4b71Sopenharmony_ci }); 575e41f4b71Sopenharmony_ci } catch (paramError) { 576e41f4b71Sopenharmony_ci // 处理入参错误异常 577e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 578e41f4b71Sopenharmony_ci } 579e41f4b71Sopenharmony_ci } 580e41f4b71Sopenharmony_ci} 581e41f4b71Sopenharmony_ci``` 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci## ServiceExtensionContext.startServiceExtensionAbility 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_cistartServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci启动一个新的ServiceExtensionAbility(callback形式)。 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci**参数:** 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 596e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 597e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 598e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ci**错误码:** 601e41f4b71Sopenharmony_ci 602e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 603e41f4b71Sopenharmony_ci 604e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 605e41f4b71Sopenharmony_ci| ------- | -------- | 606e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 607e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 608e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 609e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 610e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 611e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 612e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 613e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 614e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 615e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 616e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 617e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 618e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 619e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 620e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ci**示例:** 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci```ts 625e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 626e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 629e41f4b71Sopenharmony_ci onCreate() { 630e41f4b71Sopenharmony_ci let want: Want = { 631e41f4b71Sopenharmony_ci deviceId: '', 632e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 633e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 634e41f4b71Sopenharmony_ci }; 635e41f4b71Sopenharmony_ci 636e41f4b71Sopenharmony_ci try { 637e41f4b71Sopenharmony_ci this.context.startServiceExtensionAbility(want, (error: BusinessError) => { 638e41f4b71Sopenharmony_ci if (error.code) { 639e41f4b71Sopenharmony_ci // 处理业务逻辑错误 640e41f4b71Sopenharmony_ci console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 641e41f4b71Sopenharmony_ci return; 642e41f4b71Sopenharmony_ci } 643e41f4b71Sopenharmony_ci // 执行正常业务 644e41f4b71Sopenharmony_ci console.log('startServiceExtensionAbility succeed'); 645e41f4b71Sopenharmony_ci }); 646e41f4b71Sopenharmony_ci } catch (paramError) { 647e41f4b71Sopenharmony_ci // 处理入参错误异常 648e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 649e41f4b71Sopenharmony_ci } 650e41f4b71Sopenharmony_ci } 651e41f4b71Sopenharmony_ci} 652e41f4b71Sopenharmony_ci``` 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ci## ServiceExtensionContext.startServiceExtensionAbility 655e41f4b71Sopenharmony_ci 656e41f4b71Sopenharmony_cistartServiceExtensionAbility(want: Want): Promise\<void>; 657e41f4b71Sopenharmony_ci 658e41f4b71Sopenharmony_ci启动一个新的ServiceExtensionAbility(Promise形式)。 659e41f4b71Sopenharmony_ci 660e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 661e41f4b71Sopenharmony_ci 662e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 663e41f4b71Sopenharmony_ci 664e41f4b71Sopenharmony_ci**参数:** 665e41f4b71Sopenharmony_ci 666e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 667e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 668e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 669e41f4b71Sopenharmony_ci 670e41f4b71Sopenharmony_ci**返回值:** 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_ci| 类型 | 说明 | 673e41f4b71Sopenharmony_ci| -------- | -------- | 674e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 675e41f4b71Sopenharmony_ci 676e41f4b71Sopenharmony_ci**错误码:** 677e41f4b71Sopenharmony_ci 678e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 679e41f4b71Sopenharmony_ci 680e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 681e41f4b71Sopenharmony_ci| ------- | -------- | 682e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 683e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 684e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 685e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 686e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 687e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 688e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 689e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 690e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 691e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 692e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 693e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 694e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 695e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 696e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 697e41f4b71Sopenharmony_ci 698e41f4b71Sopenharmony_ci**示例:** 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_ci```ts 701e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 702e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 703e41f4b71Sopenharmony_ci 704e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 705e41f4b71Sopenharmony_ci onCreate() { 706e41f4b71Sopenharmony_ci let want: Want = { 707e41f4b71Sopenharmony_ci deviceId: '', 708e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 709e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 710e41f4b71Sopenharmony_ci }; 711e41f4b71Sopenharmony_ci 712e41f4b71Sopenharmony_ci try { 713e41f4b71Sopenharmony_ci this.context.startServiceExtensionAbility(want) 714e41f4b71Sopenharmony_ci .then((data) => { 715e41f4b71Sopenharmony_ci // 执行正常业务 716e41f4b71Sopenharmony_ci console.log('startServiceExtensionAbility succeed'); 717e41f4b71Sopenharmony_ci }) 718e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 719e41f4b71Sopenharmony_ci // 处理业务逻辑错误 720e41f4b71Sopenharmony_ci console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 721e41f4b71Sopenharmony_ci }); 722e41f4b71Sopenharmony_ci } catch (paramError) { 723e41f4b71Sopenharmony_ci // 处理入参错误异常 724e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 725e41f4b71Sopenharmony_ci } 726e41f4b71Sopenharmony_ci } 727e41f4b71Sopenharmony_ci} 728e41f4b71Sopenharmony_ci``` 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_cistartServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 733e41f4b71Sopenharmony_ci 734e41f4b71Sopenharmony_ci启动一个新的ServiceExtensionAbility(callback形式)。 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci> **说明:** 737e41f4b71Sopenharmony_ci> 738e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 739e41f4b71Sopenharmony_ci> 当accountId为当前用户时,无需进行权限校验。 740e41f4b71Sopenharmony_ci 741e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ci**参数:** 748e41f4b71Sopenharmony_ci 749e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 750e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 751e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 752e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 753e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 | 754e41f4b71Sopenharmony_ci 755e41f4b71Sopenharmony_ci**错误码:** 756e41f4b71Sopenharmony_ci 757e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 760e41f4b71Sopenharmony_ci| ------- | -------- | 761e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 762e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 763e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 764e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 765e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 766e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 767e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 768e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 769e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 770e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 771e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 772e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 773e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 774e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 775e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci**示例:** 778e41f4b71Sopenharmony_ci 779e41f4b71Sopenharmony_ci```ts 780e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 781e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 784e41f4b71Sopenharmony_ci onCreate() { 785e41f4b71Sopenharmony_ci let want: Want = { 786e41f4b71Sopenharmony_ci deviceId: '', 787e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 788e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 789e41f4b71Sopenharmony_ci }; 790e41f4b71Sopenharmony_ci let accountId = 100; 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci try { 793e41f4b71Sopenharmony_ci this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => { 794e41f4b71Sopenharmony_ci if (error.code) { 795e41f4b71Sopenharmony_ci // 处理业务逻辑错误 796e41f4b71Sopenharmony_ci console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 797e41f4b71Sopenharmony_ci return; 798e41f4b71Sopenharmony_ci } 799e41f4b71Sopenharmony_ci // 执行正常业务 800e41f4b71Sopenharmony_ci console.log('startServiceExtensionAbilityWithAccount succeed'); 801e41f4b71Sopenharmony_ci }); 802e41f4b71Sopenharmony_ci } catch (paramError) { 803e41f4b71Sopenharmony_ci // 处理入参错误异常 804e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 805e41f4b71Sopenharmony_ci } 806e41f4b71Sopenharmony_ci } 807e41f4b71Sopenharmony_ci} 808e41f4b71Sopenharmony_ci``` 809e41f4b71Sopenharmony_ci 810e41f4b71Sopenharmony_ci## ServiceExtensionContext.startServiceExtensionAbilityWithAccount 811e41f4b71Sopenharmony_ci 812e41f4b71Sopenharmony_cistartServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci启动一个新的ServiceExtensionAbility(Promise形式)。 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci> **说明:** 817e41f4b71Sopenharmony_ci> 818e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 819e41f4b71Sopenharmony_ci> 当accountId为当前用户时,无需进行权限校验。 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci**参数:** 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 830e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 831e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 832e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 | 833e41f4b71Sopenharmony_ci 834e41f4b71Sopenharmony_ci**返回值:** 835e41f4b71Sopenharmony_ci 836e41f4b71Sopenharmony_ci| 类型 | 说明 | 837e41f4b71Sopenharmony_ci| -------- | -------- | 838e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 839e41f4b71Sopenharmony_ci 840e41f4b71Sopenharmony_ci**错误码:** 841e41f4b71Sopenharmony_ci 842e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 843e41f4b71Sopenharmony_ci 844e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 845e41f4b71Sopenharmony_ci| ------- | -------- | 846e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 847e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 848e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 849e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 850e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 851e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 852e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 853e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 854e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 855e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 856e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 857e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 858e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 859e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 860e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci**示例:** 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_ci```ts 865e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 866e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 867e41f4b71Sopenharmony_ci 868e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 869e41f4b71Sopenharmony_ci onCreate() { 870e41f4b71Sopenharmony_ci let want: Want = { 871e41f4b71Sopenharmony_ci deviceId: '', 872e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 873e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 874e41f4b71Sopenharmony_ci }; 875e41f4b71Sopenharmony_ci let accountId = 100; 876e41f4b71Sopenharmony_ci 877e41f4b71Sopenharmony_ci try { 878e41f4b71Sopenharmony_ci this.context.startServiceExtensionAbilityWithAccount(want, accountId) 879e41f4b71Sopenharmony_ci .then((data: void) => { 880e41f4b71Sopenharmony_ci // 执行正常业务 881e41f4b71Sopenharmony_ci console.log('startServiceExtensionAbilityWithAccount succeed'); 882e41f4b71Sopenharmony_ci }) 883e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 884e41f4b71Sopenharmony_ci // 处理业务逻辑错误 885e41f4b71Sopenharmony_ci console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 886e41f4b71Sopenharmony_ci }); 887e41f4b71Sopenharmony_ci } catch (paramError) { 888e41f4b71Sopenharmony_ci // 处理入参错误异常 889e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 890e41f4b71Sopenharmony_ci } 891e41f4b71Sopenharmony_ci } 892e41f4b71Sopenharmony_ci} 893e41f4b71Sopenharmony_ci``` 894e41f4b71Sopenharmony_ci 895e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 896e41f4b71Sopenharmony_ci 897e41f4b71Sopenharmony_cistartAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void; 898e41f4b71Sopenharmony_ci 899e41f4b71Sopenharmony_ci使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。 900e41f4b71Sopenharmony_ci 901e41f4b71Sopenharmony_ci> **说明:** 902e41f4b71Sopenharmony_ci> 903e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci**参数:** 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 912e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 913e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 914e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci**错误码:** 917e41f4b71Sopenharmony_ci 918e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 919e41f4b71Sopenharmony_ci 920e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 921e41f4b71Sopenharmony_ci| ------- | -------- | 922e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 923e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 924e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 925e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 926e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 927e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 928e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 929e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 930e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 931e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 932e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 933e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 934e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 935e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 936e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 937e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 938e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 939e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 940e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 941e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 942e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 943e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 944e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 945e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 946e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 947e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci**示例:** 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ci```ts 952e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 953e41f4b71Sopenharmony_ci 954e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 955e41f4b71Sopenharmony_ci onCreate(want: Want) { 956e41f4b71Sopenharmony_ci // want包含启动该应用的Caller信息 957e41f4b71Sopenharmony_ci let localWant: Want = want; 958e41f4b71Sopenharmony_ci localWant.bundleName = 'com.example.demo'; 959e41f4b71Sopenharmony_ci localWant.moduleName = 'entry'; 960e41f4b71Sopenharmony_ci localWant.abilityName = 'TestAbility'; 961e41f4b71Sopenharmony_ci 962e41f4b71Sopenharmony_ci // 使用启动方的Caller身份信息启动新Ability 963e41f4b71Sopenharmony_ci this.context.startAbilityAsCaller(localWant, (err) => { 964e41f4b71Sopenharmony_ci if (err && err.code != 0) { 965e41f4b71Sopenharmony_ci console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 966e41f4b71Sopenharmony_ci } else { 967e41f4b71Sopenharmony_ci console.log('startAbilityAsCaller success.'); 968e41f4b71Sopenharmony_ci } 969e41f4b71Sopenharmony_ci }) 970e41f4b71Sopenharmony_ci } 971e41f4b71Sopenharmony_ci} 972e41f4b71Sopenharmony_ci``` 973e41f4b71Sopenharmony_ci 974e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 975e41f4b71Sopenharmony_ci 976e41f4b71Sopenharmony_cistartAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void; 977e41f4b71Sopenharmony_ci 978e41f4b71Sopenharmony_ci使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。 979e41f4b71Sopenharmony_ci 980e41f4b71Sopenharmony_ci> **说明:** 981e41f4b71Sopenharmony_ci> 982e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 983e41f4b71Sopenharmony_ci 984e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ci**参数:** 989e41f4b71Sopenharmony_ci 990e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 991e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 992e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 993e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 994e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 | 995e41f4b71Sopenharmony_ci 996e41f4b71Sopenharmony_ci**错误码:** 997e41f4b71Sopenharmony_ci 998e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1001e41f4b71Sopenharmony_ci| ------- | -------- | 1002e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1003e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1004e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1005e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1006e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1007e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1008e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1009e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1010e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1011e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1012e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 1013e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 1014e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1015e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 1016e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 1017e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 1018e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 1019e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 1020e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 1021e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 1022e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 1023e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1024e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 1025e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1026e41f4b71Sopenharmony_ci 1027e41f4b71Sopenharmony_ci**示例:** 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci```ts 1030e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 1031e41f4b71Sopenharmony_ci 1032e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1033e41f4b71Sopenharmony_ci onCreate(want: Want) { 1034e41f4b71Sopenharmony_ci // want包含启动该应用的Caller信息 1035e41f4b71Sopenharmony_ci let localWant: Want = want; 1036e41f4b71Sopenharmony_ci localWant.bundleName = 'com.example.demo'; 1037e41f4b71Sopenharmony_ci localWant.moduleName = 'entry'; 1038e41f4b71Sopenharmony_ci localWant.abilityName = 'TestAbility'; 1039e41f4b71Sopenharmony_ci 1040e41f4b71Sopenharmony_ci let option: StartOptions = { 1041e41f4b71Sopenharmony_ci displayId: 0 1042e41f4b71Sopenharmony_ci } 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci // 使用启动方的Caller身份信息启动新Ability 1045e41f4b71Sopenharmony_ci this.context.startAbilityAsCaller(localWant, option, (err) => { 1046e41f4b71Sopenharmony_ci if (err && err.code != 0) { 1047e41f4b71Sopenharmony_ci console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1048e41f4b71Sopenharmony_ci } else { 1049e41f4b71Sopenharmony_ci console.log('startAbilityAsCaller success.'); 1050e41f4b71Sopenharmony_ci } 1051e41f4b71Sopenharmony_ci }) 1052e41f4b71Sopenharmony_ci } 1053e41f4b71Sopenharmony_ci} 1054e41f4b71Sopenharmony_ci``` 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup> 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_cistartAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>; 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。 1061e41f4b71Sopenharmony_ci 1062e41f4b71Sopenharmony_ci> **说明:** 1063e41f4b71Sopenharmony_ci> 1064e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1065e41f4b71Sopenharmony_ci 1066e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1067e41f4b71Sopenharmony_ci 1068e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1069e41f4b71Sopenharmony_ci 1070e41f4b71Sopenharmony_ci**参数:** 1071e41f4b71Sopenharmony_ci 1072e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1073e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1074e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1075e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 1076e41f4b71Sopenharmony_ci 1077e41f4b71Sopenharmony_ci**返回值:** 1078e41f4b71Sopenharmony_ci 1079e41f4b71Sopenharmony_ci| 类型 | 说明 | 1080e41f4b71Sopenharmony_ci| -------- | -------- | 1081e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 1082e41f4b71Sopenharmony_ci 1083e41f4b71Sopenharmony_ci**错误码:** 1084e41f4b71Sopenharmony_ci 1085e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1086e41f4b71Sopenharmony_ci 1087e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1088e41f4b71Sopenharmony_ci| ------- | -------- | 1089e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1090e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1091e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1092e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1093e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1094e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1095e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1096e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1097e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1098e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1099e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 1100e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1101e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 1102e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 1103e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1104e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 1105e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 1106e41f4b71Sopenharmony_ci| 16000071 | App clone is not supported. | 1107e41f4b71Sopenharmony_ci| 16000072 | App clone or multi-instance is not supported. | 1108e41f4b71Sopenharmony_ci| 16000073 | The app clone index is invalid. | 1109e41f4b71Sopenharmony_ci| 16000076 | The app instance key is invalid. | 1110e41f4b71Sopenharmony_ci| 16000077 | The number of app instances reaches the limit. | 1111e41f4b71Sopenharmony_ci| 16000078 | The multi-instance is not supported. | 1112e41f4b71Sopenharmony_ci| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1113e41f4b71Sopenharmony_ci| 16000080 | Creating an instance is not supported. | 1114e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ci**示例:** 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ci```ts 1119e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 1120e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1123e41f4b71Sopenharmony_ci onCreate(want: Want) { 1124e41f4b71Sopenharmony_ci // want包含启动该应用的Caller信息 1125e41f4b71Sopenharmony_ci let localWant: Want = want; 1126e41f4b71Sopenharmony_ci localWant.bundleName = 'com.example.demo'; 1127e41f4b71Sopenharmony_ci localWant.moduleName = 'entry'; 1128e41f4b71Sopenharmony_ci localWant.abilityName = 'TestAbility'; 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ci let option: StartOptions = { 1131e41f4b71Sopenharmony_ci displayId: 0 1132e41f4b71Sopenharmony_ci }; 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci // 使用启动方的Caller身份信息启动新Ability 1135e41f4b71Sopenharmony_ci this.context.startAbilityAsCaller(localWant, option) 1136e41f4b71Sopenharmony_ci .then(() => { 1137e41f4b71Sopenharmony_ci console.log('startAbilityAsCaller success.'); 1138e41f4b71Sopenharmony_ci }) 1139e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 1140e41f4b71Sopenharmony_ci console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err)); 1141e41f4b71Sopenharmony_ci }) 1142e41f4b71Sopenharmony_ci } 1143e41f4b71Sopenharmony_ci} 1144e41f4b71Sopenharmony_ci``` 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ci## ServiceExtensionContext.stopServiceExtensionAbility 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_cistopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci停止同一应用程序内的服务(callback形式)。 1151e41f4b71Sopenharmony_ci 1152e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ci**参数:** 1157e41f4b71Sopenharmony_ci 1158e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1159e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1160e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 | 1161e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 | 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci**错误码:** 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1166e41f4b71Sopenharmony_ci 1167e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1168e41f4b71Sopenharmony_ci| ------- | -------- | 1169e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1170e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1171e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1172e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1173e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1174e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1175e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1176e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1177e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1178e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1179e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci**示例:** 1182e41f4b71Sopenharmony_ci 1183e41f4b71Sopenharmony_ci```ts 1184e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1185e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1186e41f4b71Sopenharmony_ci 1187e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1188e41f4b71Sopenharmony_ci onCreate() { 1189e41f4b71Sopenharmony_ci let want: Want = { 1190e41f4b71Sopenharmony_ci deviceId: '', 1191e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 1192e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 1193e41f4b71Sopenharmony_ci }; 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci try { 1196e41f4b71Sopenharmony_ci this.context.stopServiceExtensionAbility(want, (error: BusinessError) => { 1197e41f4b71Sopenharmony_ci if (error.code) { 1198e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1199e41f4b71Sopenharmony_ci console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1200e41f4b71Sopenharmony_ci return; 1201e41f4b71Sopenharmony_ci } 1202e41f4b71Sopenharmony_ci // 执行正常业务 1203e41f4b71Sopenharmony_ci console.log('stopServiceExtensionAbility succeed'); 1204e41f4b71Sopenharmony_ci }); 1205e41f4b71Sopenharmony_ci } catch (paramError) { 1206e41f4b71Sopenharmony_ci // 处理入参错误异常 1207e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1208e41f4b71Sopenharmony_ci } 1209e41f4b71Sopenharmony_ci } 1210e41f4b71Sopenharmony_ci} 1211e41f4b71Sopenharmony_ci``` 1212e41f4b71Sopenharmony_ci 1213e41f4b71Sopenharmony_ci## ServiceExtensionContext.stopServiceExtensionAbility 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_cistopServiceExtensionAbility(want: Want): Promise\<void>; 1216e41f4b71Sopenharmony_ci 1217e41f4b71Sopenharmony_ci停止同一应用程序内的服务(Promise形式)。 1218e41f4b71Sopenharmony_ci 1219e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1220e41f4b71Sopenharmony_ci 1221e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1222e41f4b71Sopenharmony_ci 1223e41f4b71Sopenharmony_ci**参数:** 1224e41f4b71Sopenharmony_ci 1225e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1226e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1227e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 | 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ci**返回值:** 1230e41f4b71Sopenharmony_ci 1231e41f4b71Sopenharmony_ci| 类型 | 说明 | 1232e41f4b71Sopenharmony_ci| -------- | -------- | 1233e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 1234e41f4b71Sopenharmony_ci 1235e41f4b71Sopenharmony_ci**错误码:** 1236e41f4b71Sopenharmony_ci 1237e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1238e41f4b71Sopenharmony_ci 1239e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1240e41f4b71Sopenharmony_ci| ------- | -------- | 1241e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1242e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1243e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1244e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1245e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1246e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1247e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1248e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1249e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1250e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1251e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1252e41f4b71Sopenharmony_ci 1253e41f4b71Sopenharmony_ci**示例:** 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci```ts 1256e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1257e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1258e41f4b71Sopenharmony_ci 1259e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1260e41f4b71Sopenharmony_ci onCreate() { 1261e41f4b71Sopenharmony_ci let want: Want = { 1262e41f4b71Sopenharmony_ci deviceId: '', 1263e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 1264e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 1265e41f4b71Sopenharmony_ci }; 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_ci try { 1268e41f4b71Sopenharmony_ci this.context.stopServiceExtensionAbility(want) 1269e41f4b71Sopenharmony_ci .then(() => { 1270e41f4b71Sopenharmony_ci // 执行正常业务 1271e41f4b71Sopenharmony_ci console.log('stopServiceExtensionAbility succeed'); 1272e41f4b71Sopenharmony_ci }) 1273e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 1274e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1275e41f4b71Sopenharmony_ci console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1276e41f4b71Sopenharmony_ci }); 1277e41f4b71Sopenharmony_ci } catch (paramError) { 1278e41f4b71Sopenharmony_ci // 处理入参错误异常 1279e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1280e41f4b71Sopenharmony_ci } 1281e41f4b71Sopenharmony_ci } 1282e41f4b71Sopenharmony_ci} 1283e41f4b71Sopenharmony_ci``` 1284e41f4b71Sopenharmony_ci 1285e41f4b71Sopenharmony_ci## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1286e41f4b71Sopenharmony_ci 1287e41f4b71Sopenharmony_cistopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 1288e41f4b71Sopenharmony_ci 1289e41f4b71Sopenharmony_ci使用帐户停止同一应用程序内的服务(callback形式)。 1290e41f4b71Sopenharmony_ci 1291e41f4b71Sopenharmony_ci> **说明:** 1292e41f4b71Sopenharmony_ci> 1293e41f4b71Sopenharmony_ci> 当accountId为当前用户时,无需进行权限校验。 1294e41f4b71Sopenharmony_ci 1295e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1296e41f4b71Sopenharmony_ci 1297e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1298e41f4b71Sopenharmony_ci 1299e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1300e41f4b71Sopenharmony_ci 1301e41f4b71Sopenharmony_ci**参数:** 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1304e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1305e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 | 1306e41f4b71Sopenharmony_ci| accountId | number | 是 | 需要停止的系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 1307e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 | 1308e41f4b71Sopenharmony_ci 1309e41f4b71Sopenharmony_ci**错误码:** 1310e41f4b71Sopenharmony_ci 1311e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1312e41f4b71Sopenharmony_ci 1313e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1314e41f4b71Sopenharmony_ci| ------- | -------- | 1315e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1316e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1317e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1318e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1319e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1320e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1321e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1322e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1323e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1324e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1325e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1326e41f4b71Sopenharmony_ci 1327e41f4b71Sopenharmony_ci**示例:** 1328e41f4b71Sopenharmony_ci 1329e41f4b71Sopenharmony_ci```ts 1330e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1331e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1332e41f4b71Sopenharmony_ci 1333e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1334e41f4b71Sopenharmony_ci onCreate() { 1335e41f4b71Sopenharmony_ci let want: Want = { 1336e41f4b71Sopenharmony_ci deviceId: '', 1337e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 1338e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 1339e41f4b71Sopenharmony_ci }; 1340e41f4b71Sopenharmony_ci let accountId = 100; 1341e41f4b71Sopenharmony_ci 1342e41f4b71Sopenharmony_ci try { 1343e41f4b71Sopenharmony_ci this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => { 1344e41f4b71Sopenharmony_ci if (error.code) { 1345e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1346e41f4b71Sopenharmony_ci console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 1347e41f4b71Sopenharmony_ci return; 1348e41f4b71Sopenharmony_ci } 1349e41f4b71Sopenharmony_ci // 执行正常业务 1350e41f4b71Sopenharmony_ci console.log('stopServiceExtensionAbilityWithAccount succeed'); 1351e41f4b71Sopenharmony_ci }); 1352e41f4b71Sopenharmony_ci } catch (paramError) { 1353e41f4b71Sopenharmony_ci // 处理入参错误异常 1354e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1355e41f4b71Sopenharmony_ci } 1356e41f4b71Sopenharmony_ci } 1357e41f4b71Sopenharmony_ci} 1358e41f4b71Sopenharmony_ci``` 1359e41f4b71Sopenharmony_ci 1360e41f4b71Sopenharmony_ci## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount 1361e41f4b71Sopenharmony_ci 1362e41f4b71Sopenharmony_cistopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 1363e41f4b71Sopenharmony_ci 1364e41f4b71Sopenharmony_ci使用帐户停止同一应用程序内的服务(Promise形式)。 1365e41f4b71Sopenharmony_ci 1366e41f4b71Sopenharmony_ci> **说明:** 1367e41f4b71Sopenharmony_ci> 1368e41f4b71Sopenharmony_ci> 当accountId为当前用户时,无需进行权限校验。 1369e41f4b71Sopenharmony_ci 1370e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1371e41f4b71Sopenharmony_ci 1372e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1373e41f4b71Sopenharmony_ci 1374e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ci**参数:** 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1379e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1380e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 | 1381e41f4b71Sopenharmony_ci| accountId | number | 是 | 需要停止的系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 | 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci**返回值:** 1384e41f4b71Sopenharmony_ci 1385e41f4b71Sopenharmony_ci| 类型 | 说明 | 1386e41f4b71Sopenharmony_ci| -------- | -------- | 1387e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 1388e41f4b71Sopenharmony_ci 1389e41f4b71Sopenharmony_ci**错误码:** 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1392e41f4b71Sopenharmony_ci 1393e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1394e41f4b71Sopenharmony_ci| ------- | -------- | 1395e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1396e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1397e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1398e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1399e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1400e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1401e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1402e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1403e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1404e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1405e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1406e41f4b71Sopenharmony_ci 1407e41f4b71Sopenharmony_ci**示例:** 1408e41f4b71Sopenharmony_ci 1409e41f4b71Sopenharmony_ci```ts 1410e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 1411e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1412e41f4b71Sopenharmony_ci 1413e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1414e41f4b71Sopenharmony_ci onCreate() { 1415e41f4b71Sopenharmony_ci let want: Want = { 1416e41f4b71Sopenharmony_ci deviceId: '', 1417e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 1418e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 1419e41f4b71Sopenharmony_ci }; 1420e41f4b71Sopenharmony_ci let accountId = 100; 1421e41f4b71Sopenharmony_ci 1422e41f4b71Sopenharmony_ci try { 1423e41f4b71Sopenharmony_ci this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 1424e41f4b71Sopenharmony_ci .then(() => { 1425e41f4b71Sopenharmony_ci // 执行正常业务 1426e41f4b71Sopenharmony_ci console.log('stopServiceExtensionAbilityWithAccount succeed'); 1427e41f4b71Sopenharmony_ci }) 1428e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 1429e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1430e41f4b71Sopenharmony_ci console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 1431e41f4b71Sopenharmony_ci }); 1432e41f4b71Sopenharmony_ci } catch (paramError) { 1433e41f4b71Sopenharmony_ci // 处理入参错误异常 1434e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1435e41f4b71Sopenharmony_ci } 1436e41f4b71Sopenharmony_ci } 1437e41f4b71Sopenharmony_ci} 1438e41f4b71Sopenharmony_ci``` 1439e41f4b71Sopenharmony_ci 1440e41f4b71Sopenharmony_ci## ServiceExtensionContext.terminateSelf 1441e41f4b71Sopenharmony_ci 1442e41f4b71Sopenharmony_citerminateSelf(callback: AsyncCallback<void>): void; 1443e41f4b71Sopenharmony_ci 1444e41f4b71Sopenharmony_ci停止Ability自身。 1445e41f4b71Sopenharmony_ci 1446e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1447e41f4b71Sopenharmony_ci 1448e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1449e41f4b71Sopenharmony_ci 1450e41f4b71Sopenharmony_ci**参数:** 1451e41f4b71Sopenharmony_ci 1452e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1453e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1454e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | 1455e41f4b71Sopenharmony_ci 1456e41f4b71Sopenharmony_ci**错误码:** 1457e41f4b71Sopenharmony_ci 1458e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1459e41f4b71Sopenharmony_ci 1460e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1461e41f4b71Sopenharmony_ci| ------- | -------- | 1462e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1463e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1464e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1465e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1466e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1467e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1468e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1469e41f4b71Sopenharmony_ci 1470e41f4b71Sopenharmony_ci**示例:** 1471e41f4b71Sopenharmony_ci 1472e41f4b71Sopenharmony_ci```ts 1473e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 1474e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1475e41f4b71Sopenharmony_ci 1476e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1477e41f4b71Sopenharmony_ci onCreate() { 1478e41f4b71Sopenharmony_ci this.context.terminateSelf((error: BusinessError) => { 1479e41f4b71Sopenharmony_ci if (error.code) { 1480e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1481e41f4b71Sopenharmony_ci console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`); 1482e41f4b71Sopenharmony_ci return; 1483e41f4b71Sopenharmony_ci } 1484e41f4b71Sopenharmony_ci // 执行正常业务 1485e41f4b71Sopenharmony_ci console.log('terminateSelf succeed'); 1486e41f4b71Sopenharmony_ci }); 1487e41f4b71Sopenharmony_ci } 1488e41f4b71Sopenharmony_ci} 1489e41f4b71Sopenharmony_ci``` 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci## ServiceExtensionContext.terminateSelf 1492e41f4b71Sopenharmony_ci 1493e41f4b71Sopenharmony_citerminateSelf(): Promise<void>; 1494e41f4b71Sopenharmony_ci 1495e41f4b71Sopenharmony_ci停止Ability自身。通过Promise返回结果。 1496e41f4b71Sopenharmony_ci 1497e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1498e41f4b71Sopenharmony_ci 1499e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1500e41f4b71Sopenharmony_ci 1501e41f4b71Sopenharmony_ci**返回值:** 1502e41f4b71Sopenharmony_ci 1503e41f4b71Sopenharmony_ci| 类型 | 说明 | 1504e41f4b71Sopenharmony_ci| -------- | -------- | 1505e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 1506e41f4b71Sopenharmony_ci 1507e41f4b71Sopenharmony_ci**错误码:** 1508e41f4b71Sopenharmony_ci 1509e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 1510e41f4b71Sopenharmony_ci 1511e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1512e41f4b71Sopenharmony_ci| ------- | -------------------------------- | 1513e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1514e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1515e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1516e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1517e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1518e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1519e41f4b71Sopenharmony_ci 1520e41f4b71Sopenharmony_ci**示例:** 1521e41f4b71Sopenharmony_ci 1522e41f4b71Sopenharmony_ci```ts 1523e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 1524e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1525e41f4b71Sopenharmony_ci 1526e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1527e41f4b71Sopenharmony_ci onCreate() { 1528e41f4b71Sopenharmony_ci this.context.terminateSelf().then(() => { 1529e41f4b71Sopenharmony_ci // 执行正常业务 1530e41f4b71Sopenharmony_ci console.log('terminateSelf succeed'); 1531e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1532e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1533e41f4b71Sopenharmony_ci console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`); 1534e41f4b71Sopenharmony_ci }); 1535e41f4b71Sopenharmony_ci } 1536e41f4b71Sopenharmony_ci} 1537e41f4b71Sopenharmony_ci``` 1538e41f4b71Sopenharmony_ci 1539e41f4b71Sopenharmony_ci## ServiceExtensionContext.connectServiceExtensionAbility 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ciconnectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 1542e41f4b71Sopenharmony_ci 1543e41f4b71Sopenharmony_ci将当前Ability连接到一个ServiceExtensionAbility。 1544e41f4b71Sopenharmony_ci 1545e41f4b71Sopenharmony_ci> **说明:** 1546e41f4b71Sopenharmony_ci> 1547e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1548e41f4b71Sopenharmony_ci 1549e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1550e41f4b71Sopenharmony_ci 1551e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1552e41f4b71Sopenharmony_ci 1553e41f4b71Sopenharmony_ci**参数:** 1554e41f4b71Sopenharmony_ci 1555e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1556e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1557e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 | 1558e41f4b71Sopenharmony_ci| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 | 1559e41f4b71Sopenharmony_ci 1560e41f4b71Sopenharmony_ci**返回值:** 1561e41f4b71Sopenharmony_ci 1562e41f4b71Sopenharmony_ci| 类型 | 说明 | 1563e41f4b71Sopenharmony_ci| -------- | -------- | 1564e41f4b71Sopenharmony_ci| number | 返回一个number,后续根据这个number去断开连接。 | 1565e41f4b71Sopenharmony_ci 1566e41f4b71Sopenharmony_ci**错误码:** 1567e41f4b71Sopenharmony_ci 1568e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1569e41f4b71Sopenharmony_ci 1570e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1571e41f4b71Sopenharmony_ci| ------- | -------- | 1572e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1573e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1574e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1575e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1576e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1577e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1578e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1579e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1580e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 1581e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 1582e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1583e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1584e41f4b71Sopenharmony_ci 1585e41f4b71Sopenharmony_ci**示例:** 1586e41f4b71Sopenharmony_ci 1587e41f4b71Sopenharmony_ci```ts 1588e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit'; 1589e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 1590e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1591e41f4b71Sopenharmony_ci 1592e41f4b71Sopenharmony_cilet commRemote: rpc.IRemoteObject; // 断开连接时需要释放 1593e41f4b71Sopenharmony_ci 1594e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1595e41f4b71Sopenharmony_ci onCreate() { 1596e41f4b71Sopenharmony_ci let want: Want = { 1597e41f4b71Sopenharmony_ci bundleName: 'com.example.myapp', 1598e41f4b71Sopenharmony_ci abilityName: 'MyAbility' 1599e41f4b71Sopenharmony_ci }; 1600e41f4b71Sopenharmony_ci let options: common.ConnectOptions = { 1601e41f4b71Sopenharmony_ci onConnect(elementName, remote) { 1602e41f4b71Sopenharmony_ci commRemote = remote; 1603e41f4b71Sopenharmony_ci console.log('----------- onConnect -----------'); 1604e41f4b71Sopenharmony_ci }, 1605e41f4b71Sopenharmony_ci onDisconnect(elementName) { 1606e41f4b71Sopenharmony_ci console.log('----------- onDisconnect -----------'); 1607e41f4b71Sopenharmony_ci }, 1608e41f4b71Sopenharmony_ci onFailed(code) { 1609e41f4b71Sopenharmony_ci console.error('----------- onFailed -----------'); 1610e41f4b71Sopenharmony_ci } 1611e41f4b71Sopenharmony_ci }; 1612e41f4b71Sopenharmony_ci let connection: number; 1613e41f4b71Sopenharmony_ci 1614e41f4b71Sopenharmony_ci try { 1615e41f4b71Sopenharmony_ci connection = this.context.connectServiceExtensionAbility(want, options); 1616e41f4b71Sopenharmony_ci } catch (paramError) { 1617e41f4b71Sopenharmony_ci // 处理入参错误异常 1618e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1619e41f4b71Sopenharmony_ci } 1620e41f4b71Sopenharmony_ci } 1621e41f4b71Sopenharmony_ci} 1622e41f4b71Sopenharmony_ci``` 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount 1625e41f4b71Sopenharmony_ci 1626e41f4b71Sopenharmony_ciconnectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 1627e41f4b71Sopenharmony_ci 1628e41f4b71Sopenharmony_ci将当前Ability连接到一个指定account的ServiceExtensionAbility。 1629e41f4b71Sopenharmony_ci 1630e41f4b71Sopenharmony_ci> **说明:** 1631e41f4b71Sopenharmony_ci> 1632e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1633e41f4b71Sopenharmony_ci> 当accountId为当前用户时,无需进行权限校验。 1634e41f4b71Sopenharmony_ci 1635e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1638e41f4b71Sopenharmony_ci 1639e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1640e41f4b71Sopenharmony_ci 1641e41f4b71Sopenharmony_ci**参数:** 1642e41f4b71Sopenharmony_ci 1643e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1644e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1645e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 | 1646e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 | 1647e41f4b71Sopenharmony_ci| options | ConnectOptions | 是 | 远端对象实例。 | 1648e41f4b71Sopenharmony_ci 1649e41f4b71Sopenharmony_ci**返回值:** 1650e41f4b71Sopenharmony_ci 1651e41f4b71Sopenharmony_ci| 类型 | 说明 | 1652e41f4b71Sopenharmony_ci| -------- | -------- | 1653e41f4b71Sopenharmony_ci| number | 返回Ability连接的结果code。 | 1654e41f4b71Sopenharmony_ci 1655e41f4b71Sopenharmony_ci**错误码:** 1656e41f4b71Sopenharmony_ci 1657e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1658e41f4b71Sopenharmony_ci 1659e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1660e41f4b71Sopenharmony_ci| ------- | -------- | 1661e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1662e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 1663e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1664e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1665e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1666e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1667e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1668e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1669e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1670e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 1671e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 1672e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1673e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1674e41f4b71Sopenharmony_ci 1675e41f4b71Sopenharmony_ci**示例:** 1676e41f4b71Sopenharmony_ci 1677e41f4b71Sopenharmony_ci```ts 1678e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit'; 1679e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 1680e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1681e41f4b71Sopenharmony_ci 1682e41f4b71Sopenharmony_cilet commRemote: rpc.IRemoteObject; // 断开连接时需要释放 1683e41f4b71Sopenharmony_ci 1684e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1685e41f4b71Sopenharmony_ci onCreate() { 1686e41f4b71Sopenharmony_ci let want: Want = { 1687e41f4b71Sopenharmony_ci deviceId: '', 1688e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 1689e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 1690e41f4b71Sopenharmony_ci }; 1691e41f4b71Sopenharmony_ci let accountId = 100; 1692e41f4b71Sopenharmony_ci let options: common.ConnectOptions = { 1693e41f4b71Sopenharmony_ci onConnect(elementName, remote) { 1694e41f4b71Sopenharmony_ci commRemote = remote; 1695e41f4b71Sopenharmony_ci console.log('----------- onConnect -----------'); 1696e41f4b71Sopenharmony_ci }, 1697e41f4b71Sopenharmony_ci onDisconnect(elementName) { 1698e41f4b71Sopenharmony_ci console.log('----------- onDisconnect -----------'); 1699e41f4b71Sopenharmony_ci }, 1700e41f4b71Sopenharmony_ci onFailed(code) { 1701e41f4b71Sopenharmony_ci console.log('----------- onFailed -----------'); 1702e41f4b71Sopenharmony_ci } 1703e41f4b71Sopenharmony_ci }; 1704e41f4b71Sopenharmony_ci let connection: number; 1705e41f4b71Sopenharmony_ci 1706e41f4b71Sopenharmony_ci try { 1707e41f4b71Sopenharmony_ci connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1708e41f4b71Sopenharmony_ci } catch (paramError) { 1709e41f4b71Sopenharmony_ci // 处理入参错误异常 1710e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1711e41f4b71Sopenharmony_ci } 1712e41f4b71Sopenharmony_ci } 1713e41f4b71Sopenharmony_ci} 1714e41f4b71Sopenharmony_ci``` 1715e41f4b71Sopenharmony_ci 1716e41f4b71Sopenharmony_ci## ServiceExtensionContext.disconnectServiceExtensionAbility 1717e41f4b71Sopenharmony_ci 1718e41f4b71Sopenharmony_cidisconnectServiceExtensionAbility(connection: number, callback:AsyncCallback<void>): void; 1719e41f4b71Sopenharmony_ci 1720e41f4b71Sopenharmony_ci将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。 1721e41f4b71Sopenharmony_ci 1722e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1723e41f4b71Sopenharmony_ci 1724e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1725e41f4b71Sopenharmony_ci 1726e41f4b71Sopenharmony_ci**参数:** 1727e41f4b71Sopenharmony_ci 1728e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1729e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1730e41f4b71Sopenharmony_ci| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1731e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | 1732e41f4b71Sopenharmony_ci 1733e41f4b71Sopenharmony_ci**错误码:** 1734e41f4b71Sopenharmony_ci 1735e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1736e41f4b71Sopenharmony_ci 1737e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1738e41f4b71Sopenharmony_ci| ------- | -------- | 1739e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1740e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1741e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1742e41f4b71Sopenharmony_ci 1743e41f4b71Sopenharmony_ci**示例:** 1744e41f4b71Sopenharmony_ci 1745e41f4b71Sopenharmony_ci```ts 1746e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 1747e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 1748e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1749e41f4b71Sopenharmony_ci 1750e41f4b71Sopenharmony_cilet commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放 1751e41f4b71Sopenharmony_ci 1752e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1753e41f4b71Sopenharmony_ci onCreate() { 1754e41f4b71Sopenharmony_ci // connection为connectServiceExtensionAbility中的返回值 1755e41f4b71Sopenharmony_ci let connection = 1; 1756e41f4b71Sopenharmony_ci try { 1757e41f4b71Sopenharmony_ci this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => { 1758e41f4b71Sopenharmony_ci commRemote = null; 1759e41f4b71Sopenharmony_ci if (error.code) { 1760e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1761e41f4b71Sopenharmony_ci console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1762e41f4b71Sopenharmony_ci return; 1763e41f4b71Sopenharmony_ci } 1764e41f4b71Sopenharmony_ci // 执行正常业务 1765e41f4b71Sopenharmony_ci console.log('disconnectServiceExtensionAbility succeed'); 1766e41f4b71Sopenharmony_ci }); 1767e41f4b71Sopenharmony_ci } catch (paramError) { 1768e41f4b71Sopenharmony_ci commRemote = null; 1769e41f4b71Sopenharmony_ci // 处理入参错误异常 1770e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1771e41f4b71Sopenharmony_ci } 1772e41f4b71Sopenharmony_ci } 1773e41f4b71Sopenharmony_ci} 1774e41f4b71Sopenharmony_ci``` 1775e41f4b71Sopenharmony_ci 1776e41f4b71Sopenharmony_ci## ServiceExtensionContext.disconnectServiceExtensionAbility 1777e41f4b71Sopenharmony_ci 1778e41f4b71Sopenharmony_cidisconnectServiceExtensionAbility(connection: number): Promise<void>; 1779e41f4b71Sopenharmony_ci 1780e41f4b71Sopenharmony_ci将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空(Promise形式返回结果)。 1781e41f4b71Sopenharmony_ci 1782e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1783e41f4b71Sopenharmony_ci 1784e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1785e41f4b71Sopenharmony_ci 1786e41f4b71Sopenharmony_ci**参数:** 1787e41f4b71Sopenharmony_ci 1788e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1789e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1790e41f4b71Sopenharmony_ci| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 | 1791e41f4b71Sopenharmony_ci 1792e41f4b71Sopenharmony_ci**返回值:** 1793e41f4b71Sopenharmony_ci 1794e41f4b71Sopenharmony_ci| 类型 | 说明 | 1795e41f4b71Sopenharmony_ci| -------- | -------- | 1796e41f4b71Sopenharmony_ci| Promise<void> | 返回一个Promise,包含接口的结果。 | 1797e41f4b71Sopenharmony_ci 1798e41f4b71Sopenharmony_ci**错误码:** 1799e41f4b71Sopenharmony_ci 1800e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1801e41f4b71Sopenharmony_ci 1802e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1803e41f4b71Sopenharmony_ci| ------- | -------- | 1804e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1805e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1806e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1807e41f4b71Sopenharmony_ci 1808e41f4b71Sopenharmony_ci**示例:** 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ci```ts 1811e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 1812e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 1813e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1814e41f4b71Sopenharmony_ci 1815e41f4b71Sopenharmony_cilet commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放 1816e41f4b71Sopenharmony_ci 1817e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1818e41f4b71Sopenharmony_ci onCreate() { 1819e41f4b71Sopenharmony_ci // connection为connectServiceExtensionAbility中的返回值 1820e41f4b71Sopenharmony_ci let connection = 1; 1821e41f4b71Sopenharmony_ci try { 1822e41f4b71Sopenharmony_ci this.context.disconnectServiceExtensionAbility(connection) 1823e41f4b71Sopenharmony_ci .then(() => { 1824e41f4b71Sopenharmony_ci commRemote = null; 1825e41f4b71Sopenharmony_ci // 执行正常业务 1826e41f4b71Sopenharmony_ci console.log('disconnectServiceExtensionAbility succeed'); 1827e41f4b71Sopenharmony_ci }) 1828e41f4b71Sopenharmony_ci .catch((error: BusinessError) => { 1829e41f4b71Sopenharmony_ci commRemote = null; 1830e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1831e41f4b71Sopenharmony_ci console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`); 1832e41f4b71Sopenharmony_ci }); 1833e41f4b71Sopenharmony_ci } catch (paramError) { 1834e41f4b71Sopenharmony_ci commRemote = null; 1835e41f4b71Sopenharmony_ci // 处理入参错误异常 1836e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1837e41f4b71Sopenharmony_ci } 1838e41f4b71Sopenharmony_ci } 1839e41f4b71Sopenharmony_ci} 1840e41f4b71Sopenharmony_ci``` 1841e41f4b71Sopenharmony_ci 1842e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityByCall 1843e41f4b71Sopenharmony_ci 1844e41f4b71Sopenharmony_cistartAbilityByCall(want: Want): Promise<Caller>; 1845e41f4b71Sopenharmony_ci 1846e41f4b71Sopenharmony_ci启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。 1847e41f4b71Sopenharmony_ci 1848e41f4b71Sopenharmony_ci使用规则: 1849e41f4b71Sopenharmony_ci - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 1850e41f4b71Sopenharmony_ci - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 1851e41f4b71Sopenharmony_ci - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 1852e41f4b71Sopenharmony_ci 1853e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION 1854e41f4b71Sopenharmony_ci 1855e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1856e41f4b71Sopenharmony_ci 1857e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 1858e41f4b71Sopenharmony_ci 1859e41f4b71Sopenharmony_ci**参数:** 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1862e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1863e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId、parameters(可选),parameters缺省或为空表示后台启动Ability。 | 1864e41f4b71Sopenharmony_ci 1865e41f4b71Sopenharmony_ci**返回值:** 1866e41f4b71Sopenharmony_ci 1867e41f4b71Sopenharmony_ci| 类型 | 说明 | 1868e41f4b71Sopenharmony_ci| -------- | -------- | 1869e41f4b71Sopenharmony_ci| Promise<Caller> | 获取要通讯的caller对象。 | 1870e41f4b71Sopenharmony_ci 1871e41f4b71Sopenharmony_ci**错误码:** 1872e41f4b71Sopenharmony_ci 1873e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1874e41f4b71Sopenharmony_ci 1875e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1876e41f4b71Sopenharmony_ci| ------- | -------- | 1877e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1878e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1879e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1880e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1881e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1882e41f4b71Sopenharmony_ci| 16000005 | Static permission denied. The specified process does not have the permission. | 1883e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1884e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1885e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 1886e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 1887e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 1888e41f4b71Sopenharmony_ci 1889e41f4b71Sopenharmony_ci**示例:** 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ci后台启动: 1892e41f4b71Sopenharmony_ci 1893e41f4b71Sopenharmony_ci```ts 1894e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1895e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1896e41f4b71Sopenharmony_ci 1897e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1898e41f4b71Sopenharmony_ci onCreate() { 1899e41f4b71Sopenharmony_ci let caller: Caller; 1900e41f4b71Sopenharmony_ci // 后台启动Ability,不配置parameters 1901e41f4b71Sopenharmony_ci let wantBackground: Want = { 1902e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 1903e41f4b71Sopenharmony_ci moduleName: 'entry', 1904e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 1905e41f4b71Sopenharmony_ci deviceId: '' 1906e41f4b71Sopenharmony_ci }; 1907e41f4b71Sopenharmony_ci 1908e41f4b71Sopenharmony_ci try { 1909e41f4b71Sopenharmony_ci this.context.startAbilityByCall(wantBackground) 1910e41f4b71Sopenharmony_ci .then((obj: Caller) => { 1911e41f4b71Sopenharmony_ci // 执行正常业务 1912e41f4b71Sopenharmony_ci caller = obj; 1913e41f4b71Sopenharmony_ci console.log('startAbilityByCall succeed'); 1914e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1915e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1916e41f4b71Sopenharmony_ci console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1917e41f4b71Sopenharmony_ci }); 1918e41f4b71Sopenharmony_ci } catch (paramError) { 1919e41f4b71Sopenharmony_ci // 处理入参错误异常 1920e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1921e41f4b71Sopenharmony_ci } 1922e41f4b71Sopenharmony_ci } 1923e41f4b71Sopenharmony_ci} 1924e41f4b71Sopenharmony_ci``` 1925e41f4b71Sopenharmony_ci 1926e41f4b71Sopenharmony_ci前台启动: 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci```ts 1929e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit'; 1930e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 1933e41f4b71Sopenharmony_ci onCreate() { 1934e41f4b71Sopenharmony_ci let caller: Caller; 1935e41f4b71Sopenharmony_ci // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true 1936e41f4b71Sopenharmony_ci let wantForeground: Want = { 1937e41f4b71Sopenharmony_ci bundleName: 'com.example.myservice', 1938e41f4b71Sopenharmony_ci moduleName: 'entry', 1939e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 1940e41f4b71Sopenharmony_ci deviceId: '', 1941e41f4b71Sopenharmony_ci parameters: { 1942e41f4b71Sopenharmony_ci 'ohos.aafwk.param.callAbilityToForeground': true 1943e41f4b71Sopenharmony_ci } 1944e41f4b71Sopenharmony_ci }; 1945e41f4b71Sopenharmony_ci 1946e41f4b71Sopenharmony_ci try { 1947e41f4b71Sopenharmony_ci this.context.startAbilityByCall(wantForeground) 1948e41f4b71Sopenharmony_ci .then((obj: Caller) => { 1949e41f4b71Sopenharmony_ci // 执行正常业务 1950e41f4b71Sopenharmony_ci caller = obj; 1951e41f4b71Sopenharmony_ci console.log('startAbilityByCall succeed'); 1952e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 1953e41f4b71Sopenharmony_ci // 处理业务逻辑错误 1954e41f4b71Sopenharmony_ci console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`); 1955e41f4b71Sopenharmony_ci }); 1956e41f4b71Sopenharmony_ci } catch (paramError) { 1957e41f4b71Sopenharmony_ci // 处理入参错误异常 1958e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 1959e41f4b71Sopenharmony_ci } 1960e41f4b71Sopenharmony_ci } 1961e41f4b71Sopenharmony_ci} 1962e41f4b71Sopenharmony_ci``` 1963e41f4b71Sopenharmony_ci## ServiceExtensionContext.startRecentAbility 1964e41f4b71Sopenharmony_ci 1965e41f4b71Sopenharmony_cistartRecentAbility(want: Want, callback: AsyncCallback\<void>): void; 1966e41f4b71Sopenharmony_ci 1967e41f4b71Sopenharmony_ci启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。 1968e41f4b71Sopenharmony_ci 1969e41f4b71Sopenharmony_ci> **说明:** 1970e41f4b71Sopenharmony_ci> 1971e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 1972e41f4b71Sopenharmony_ci 1973e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 1974e41f4b71Sopenharmony_ci 1975e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_ci**参数:** 1978e41f4b71Sopenharmony_ci 1979e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1980e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 1981e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 1982e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 1983e41f4b71Sopenharmony_ci 1984e41f4b71Sopenharmony_ci**错误码:** 1985e41f4b71Sopenharmony_ci 1986e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 1987e41f4b71Sopenharmony_ci 1988e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 1989e41f4b71Sopenharmony_ci| ------- | -------- | 1990e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 1991e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1992e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 1993e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 1994e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 1995e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 1996e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 1997e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 1998e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1999e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 2000e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2001e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2002e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 2003e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 2004e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2005e41f4b71Sopenharmony_ci 2006e41f4b71Sopenharmony_ci**示例:** 2007e41f4b71Sopenharmony_ci 2008e41f4b71Sopenharmony_ci```ts 2009e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2010e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2011e41f4b71Sopenharmony_ci 2012e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 2013e41f4b71Sopenharmony_ci onCreate() { 2014e41f4b71Sopenharmony_ci let want: Want = { 2015e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2016e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 2017e41f4b71Sopenharmony_ci }; 2018e41f4b71Sopenharmony_ci 2019e41f4b71Sopenharmony_ci try { 2020e41f4b71Sopenharmony_ci this.context.startRecentAbility(want, (err: BusinessError) => { 2021e41f4b71Sopenharmony_ci if (err.code) { 2022e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2023e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2024e41f4b71Sopenharmony_ci return; 2025e41f4b71Sopenharmony_ci } 2026e41f4b71Sopenharmony_ci // 执行正常业务 2027e41f4b71Sopenharmony_ci console.info('startRecentAbility succeed'); 2028e41f4b71Sopenharmony_ci }); 2029e41f4b71Sopenharmony_ci } catch (err) { 2030e41f4b71Sopenharmony_ci // 处理入参错误异常 2031e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 2032e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 2033e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2034e41f4b71Sopenharmony_ci } 2035e41f4b71Sopenharmony_ci } 2036e41f4b71Sopenharmony_ci} 2037e41f4b71Sopenharmony_ci``` 2038e41f4b71Sopenharmony_ci## ServiceExtensionContext.startRecentAbility 2039e41f4b71Sopenharmony_ci 2040e41f4b71Sopenharmony_cistartRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void; 2041e41f4b71Sopenharmony_ci 2042e41f4b71Sopenharmony_ci启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。 2043e41f4b71Sopenharmony_ci当开发者需要携带启动参数时可以选择此API。 2044e41f4b71Sopenharmony_ci 2045e41f4b71Sopenharmony_ci> **说明:** 2046e41f4b71Sopenharmony_ci> 2047e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2048e41f4b71Sopenharmony_ci 2049e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2050e41f4b71Sopenharmony_ci 2051e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 2052e41f4b71Sopenharmony_ci 2053e41f4b71Sopenharmony_ci**参数:** 2054e41f4b71Sopenharmony_ci 2055e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2056e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2057e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 2058e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 | 2059e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 | 2060e41f4b71Sopenharmony_ci 2061e41f4b71Sopenharmony_ci**错误码:** 2062e41f4b71Sopenharmony_ci 2063e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2064e41f4b71Sopenharmony_ci 2065e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2066e41f4b71Sopenharmony_ci| ------- | -------- | 2067e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2068e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2069e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 2070e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 2071e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 2072e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 2073e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 2074e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 2075e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2076e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 2077e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2078e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2079e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 2080e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 2081e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2082e41f4b71Sopenharmony_ci 2083e41f4b71Sopenharmony_ci**示例:** 2084e41f4b71Sopenharmony_ci 2085e41f4b71Sopenharmony_ci```ts 2086e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2087e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2088e41f4b71Sopenharmony_ci 2089e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 2090e41f4b71Sopenharmony_ci onCreate() { 2091e41f4b71Sopenharmony_ci let want: Want = { 2092e41f4b71Sopenharmony_ci deviceId: '', 2093e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2094e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 2095e41f4b71Sopenharmony_ci }; 2096e41f4b71Sopenharmony_ci let options: StartOptions = { 2097e41f4b71Sopenharmony_ci windowMode: 0 2098e41f4b71Sopenharmony_ci }; 2099e41f4b71Sopenharmony_ci 2100e41f4b71Sopenharmony_ci try { 2101e41f4b71Sopenharmony_ci this.context.startRecentAbility(want, options, (err: BusinessError) => { 2102e41f4b71Sopenharmony_ci if (err.code) { 2103e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2104e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2105e41f4b71Sopenharmony_ci return; 2106e41f4b71Sopenharmony_ci } 2107e41f4b71Sopenharmony_ci // 执行正常业务 2108e41f4b71Sopenharmony_ci console.info('startRecentAbility succeed'); 2109e41f4b71Sopenharmony_ci }); 2110e41f4b71Sopenharmony_ci } catch (err) { 2111e41f4b71Sopenharmony_ci // 处理入参错误异常 2112e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 2113e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 2114e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2115e41f4b71Sopenharmony_ci } 2116e41f4b71Sopenharmony_ci } 2117e41f4b71Sopenharmony_ci} 2118e41f4b71Sopenharmony_ci``` 2119e41f4b71Sopenharmony_ci## ServiceExtensionContext.startRecentAbility 2120e41f4b71Sopenharmony_ci 2121e41f4b71Sopenharmony_cistartRecentAbility(want: Want, options?: StartOptions): Promise\<void>; 2122e41f4b71Sopenharmony_ci 2123e41f4b71Sopenharmony_ci启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。 2124e41f4b71Sopenharmony_ci当开发者期望启动结果以Promise形式返回时可以选择此API。 2125e41f4b71Sopenharmony_ci 2126e41f4b71Sopenharmony_ci> **说明:** 2127e41f4b71Sopenharmony_ci> 2128e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2129e41f4b71Sopenharmony_ci 2130e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2131e41f4b71Sopenharmony_ci 2132e41f4b71Sopenharmony_ci**系统API**: 此接口为系统接口,三方应用不支持调用。 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci**参数:** 2135e41f4b71Sopenharmony_ci 2136e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2137e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2138e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 | 2139e41f4b71Sopenharmony_ci| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 | 2140e41f4b71Sopenharmony_ci 2141e41f4b71Sopenharmony_ci**错误码:** 2142e41f4b71Sopenharmony_ci 2143e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2144e41f4b71Sopenharmony_ci 2145e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2146e41f4b71Sopenharmony_ci| ------- | -------- | 2147e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2148e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2149e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 2150e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 2151e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 2152e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 2153e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 2154e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 2155e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2156e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 2157e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2158e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2159e41f4b71Sopenharmony_ci| 16000053 | The ability is not on the top of the UI. | 2160e41f4b71Sopenharmony_ci| 16000055 | Installation-free timed out. | 2161e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2162e41f4b71Sopenharmony_ci 2163e41f4b71Sopenharmony_ci**示例:** 2164e41f4b71Sopenharmony_ci 2165e41f4b71Sopenharmony_ci```ts 2166e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 2167e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2168e41f4b71Sopenharmony_ci 2169e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 2170e41f4b71Sopenharmony_ci onCreate() { 2171e41f4b71Sopenharmony_ci let want: Want = { 2172e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2173e41f4b71Sopenharmony_ci abilityName: 'EntryAbility' 2174e41f4b71Sopenharmony_ci }; 2175e41f4b71Sopenharmony_ci let options: StartOptions = { 2176e41f4b71Sopenharmony_ci windowMode: 0, 2177e41f4b71Sopenharmony_ci }; 2178e41f4b71Sopenharmony_ci 2179e41f4b71Sopenharmony_ci try { 2180e41f4b71Sopenharmony_ci this.context.startRecentAbility(want, options) 2181e41f4b71Sopenharmony_ci .then(() => { 2182e41f4b71Sopenharmony_ci // 执行正常业务 2183e41f4b71Sopenharmony_ci console.info('startRecentAbility succeed'); 2184e41f4b71Sopenharmony_ci }) 2185e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 2186e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2187e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`); 2188e41f4b71Sopenharmony_ci }); 2189e41f4b71Sopenharmony_ci } catch (err) { 2190e41f4b71Sopenharmony_ci // 处理入参错误异常 2191e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 2192e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 2193e41f4b71Sopenharmony_ci console.error(`startRecentAbility failed, code is ${code}, message is ${message}`); 2194e41f4b71Sopenharmony_ci } 2195e41f4b71Sopenharmony_ci } 2196e41f4b71Sopenharmony_ci} 2197e41f4b71Sopenharmony_ci``` 2198e41f4b71Sopenharmony_ci 2199e41f4b71Sopenharmony_ci## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup> 2200e41f4b71Sopenharmony_ci 2201e41f4b71Sopenharmony_cistartAbilityByCallWithAccount(want: Want, accountId: number): Promise<Caller>; 2202e41f4b71Sopenharmony_ci 2203e41f4b71Sopenharmony_ci根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。 2204e41f4b71Sopenharmony_ci 2205e41f4b71Sopenharmony_ci使用规则: 2206e41f4b71Sopenharmony_ci - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`与`ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限 2207e41f4b71Sopenharmony_ci - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限 2208e41f4b71Sopenharmony_ci - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限 2209e41f4b71Sopenharmony_ci - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md) 2210e41f4b71Sopenharmony_ci 2211e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 2212e41f4b71Sopenharmony_ci 2213e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2214e41f4b71Sopenharmony_ci 2215e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci**参数:** 2218e41f4b71Sopenharmony_ci 2219e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2220e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2221e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 | 2222e41f4b71Sopenharmony_ci| accountId | number | 是 | 系统账号的账号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 | 2223e41f4b71Sopenharmony_ci 2224e41f4b71Sopenharmony_ci**返回值:** 2225e41f4b71Sopenharmony_ci 2226e41f4b71Sopenharmony_ci| 类型 | 说明 | 2227e41f4b71Sopenharmony_ci| -------- | -------- | 2228e41f4b71Sopenharmony_ci| Promise<Caller> | 获取要通讯的caller对象。 | 2229e41f4b71Sopenharmony_ci 2230e41f4b71Sopenharmony_ci**错误码:** 2231e41f4b71Sopenharmony_ci 2232e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2233e41f4b71Sopenharmony_ci 2234e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2235e41f4b71Sopenharmony_ci| ------- | -------- | 2236e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2237e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2238e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2239e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 2240e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 2241e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 2242e41f4b71Sopenharmony_ci| 16000005 | Static permission denied. The specified process does not have the permission. | 2243e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 2244e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 2245e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2246e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 2247e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 2248e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2249e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2250e41f4b71Sopenharmony_ci 2251e41f4b71Sopenharmony_ci**示例:** 2252e41f4b71Sopenharmony_ci 2253e41f4b71Sopenharmony_ci```ts 2254e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit'; 2255e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2256e41f4b71Sopenharmony_ci 2257e41f4b71Sopenharmony_ciclass EntryAbility extends ServiceExtensionAbility { 2258e41f4b71Sopenharmony_ci onCreate() { 2259e41f4b71Sopenharmony_ci let caller: Caller; 2260e41f4b71Sopenharmony_ci // 系统账号的账号ID, -1表示当前激活用户 2261e41f4b71Sopenharmony_ci let accountId = -1; 2262e41f4b71Sopenharmony_ci // 指定启动的Ability 2263e41f4b71Sopenharmony_ci let want: Want = { 2264e41f4b71Sopenharmony_ci bundleName: 'com.acts.actscalleeabilityrely', 2265e41f4b71Sopenharmony_ci moduleName: 'entry', 2266e41f4b71Sopenharmony_ci abilityName: 'EntryAbility', 2267e41f4b71Sopenharmony_ci deviceId: '', 2268e41f4b71Sopenharmony_ci parameters: { 2269e41f4b71Sopenharmony_ci // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动 2270e41f4b71Sopenharmony_ci 'ohos.aafwk.param.callAbilityToForeground': true 2271e41f4b71Sopenharmony_ci } 2272e41f4b71Sopenharmony_ci }; 2273e41f4b71Sopenharmony_ci 2274e41f4b71Sopenharmony_ci try { 2275e41f4b71Sopenharmony_ci this.context.startAbilityByCallWithAccount(want, accountId) 2276e41f4b71Sopenharmony_ci .then((obj: Caller) => { 2277e41f4b71Sopenharmony_ci // 执行正常业务 2278e41f4b71Sopenharmony_ci caller = obj; 2279e41f4b71Sopenharmony_ci console.log('startAbilityByCallWithAccount succeed'); 2280e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 2281e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2282e41f4b71Sopenharmony_ci console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`); 2283e41f4b71Sopenharmony_ci }); 2284e41f4b71Sopenharmony_ci } catch (paramError) { 2285e41f4b71Sopenharmony_ci // 处理入参错误异常 2286e41f4b71Sopenharmony_ci console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`); 2287e41f4b71Sopenharmony_ci } 2288e41f4b71Sopenharmony_ci } 2289e41f4b71Sopenharmony_ci} 2290e41f4b71Sopenharmony_ci``` 2291e41f4b71Sopenharmony_ci 2292e41f4b71Sopenharmony_ci## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2293e41f4b71Sopenharmony_ci 2294e41f4b71Sopenharmony_cirequestModalUIExtension(pickerWant: Want): Promise\<void> 2295e41f4b71Sopenharmony_ci 2296e41f4b71Sopenharmony_ci请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用promise形式异步回调。 2297e41f4b71Sopenharmony_ci 2298e41f4b71Sopenharmony_ci在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2299e41f4b71Sopenharmony_ci 2300e41f4b71Sopenharmony_ci> **说明:** 2301e41f4b71Sopenharmony_ci> 2302e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2303e41f4b71Sopenharmony_ci 2304e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2305e41f4b71Sopenharmony_ci 2306e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。 2307e41f4b71Sopenharmony_ci 2308e41f4b71Sopenharmony_ci**参数:** 2309e41f4b71Sopenharmony_ci 2310e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2311e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2312e41f4b71Sopenharmony_ci| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2313e41f4b71Sopenharmony_ci 2314e41f4b71Sopenharmony_ci**返回值:** 2315e41f4b71Sopenharmony_ci 2316e41f4b71Sopenharmony_ci| 类型 | 说明 | 2317e41f4b71Sopenharmony_ci| -------- | -------- | 2318e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2319e41f4b71Sopenharmony_ci 2320e41f4b71Sopenharmony_ci**错误码:** 2321e41f4b71Sopenharmony_ci 2322e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2323e41f4b71Sopenharmony_ci 2324e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2325e41f4b71Sopenharmony_ci| ------- | -------- | 2326e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2327e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2328e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2329e41f4b71Sopenharmony_ci 2330e41f4b71Sopenharmony_ci**示例:** 2331e41f4b71Sopenharmony_ci 2332e41f4b71Sopenharmony_ci```ts 2333e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2334e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2335e41f4b71Sopenharmony_ci 2336e41f4b71Sopenharmony_ciclass ServiceExtension extends ServiceExtensionAbility { 2337e41f4b71Sopenharmony_ci onCreate() { 2338e41f4b71Sopenharmony_ci let pickerWant: Want = { 2339e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2340e41f4b71Sopenharmony_ci abilityName: 'UIExtAbility', 2341e41f4b71Sopenharmony_ci moduleName: 'entry_test', 2342e41f4b71Sopenharmony_ci parameters: { 2343e41f4b71Sopenharmony_ci 'bundleName': 'com.example.myapplication', 2344e41f4b71Sopenharmony_ci //与com.example.myapplication.UIExtAbility配置的type相同 2345e41f4b71Sopenharmony_ci 'ability.want.params.uiExtensionType': 'sys/commonUI' 2346e41f4b71Sopenharmony_ci } 2347e41f4b71Sopenharmony_ci }; 2348e41f4b71Sopenharmony_ci 2349e41f4b71Sopenharmony_ci try { 2350e41f4b71Sopenharmony_ci this.context.requestModalUIExtension(pickerWant) 2351e41f4b71Sopenharmony_ci .then(() => { 2352e41f4b71Sopenharmony_ci // 执行正常业务 2353e41f4b71Sopenharmony_ci console.info('requestModalUIExtension succeed'); 2354e41f4b71Sopenharmony_ci }) 2355e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 2356e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2357e41f4b71Sopenharmony_ci console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2358e41f4b71Sopenharmony_ci }); 2359e41f4b71Sopenharmony_ci } catch (err) { 2360e41f4b71Sopenharmony_ci // 处理入参错误异常 2361e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 2362e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 2363e41f4b71Sopenharmony_ci console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2364e41f4b71Sopenharmony_ci } 2365e41f4b71Sopenharmony_ci } 2366e41f4b71Sopenharmony_ci} 2367e41f4b71Sopenharmony_ci``` 2368e41f4b71Sopenharmony_ci 2369e41f4b71Sopenharmony_ci## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup> 2370e41f4b71Sopenharmony_ci 2371e41f4b71Sopenharmony_cirequestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void 2372e41f4b71Sopenharmony_ci 2373e41f4b71Sopenharmony_ci请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用callback形式异步回调。 2374e41f4b71Sopenharmony_ci 2375e41f4b71Sopenharmony_ci在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。 2376e41f4b71Sopenharmony_ci 2377e41f4b71Sopenharmony_ci> **说明:** 2378e41f4b71Sopenharmony_ci> 2379e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2380e41f4b71Sopenharmony_ci 2381e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2382e41f4b71Sopenharmony_ci 2383e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口。 2384e41f4b71Sopenharmony_ci 2385e41f4b71Sopenharmony_ci**参数:** 2386e41f4b71Sopenharmony_ci 2387e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2388e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2389e41f4b71Sopenharmony_ci| pickerWant | [Want](js-apis-app-ability-want.md) | 是 | 拉起UIExtension的want信息。 | 2390e41f4b71Sopenharmony_ci| callback | AsyncCallback<void> | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 | 2391e41f4b71Sopenharmony_ci 2392e41f4b71Sopenharmony_ci**错误码:** 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2395e41f4b71Sopenharmony_ci 2396e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2397e41f4b71Sopenharmony_ci| ------- | -------- | 2398e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2399e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2400e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2401e41f4b71Sopenharmony_ci 2402e41f4b71Sopenharmony_ci**示例:** 2403e41f4b71Sopenharmony_ci 2404e41f4b71Sopenharmony_ci```ts 2405e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2406e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2407e41f4b71Sopenharmony_ci 2408e41f4b71Sopenharmony_ciclass ServiceExtension extends ServiceExtensionAbility { 2409e41f4b71Sopenharmony_ci onCreate() { 2410e41f4b71Sopenharmony_ci let pickerWant: Want = { 2411e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2412e41f4b71Sopenharmony_ci abilityName: 'com.example.myapplication.UIExtAbility', 2413e41f4b71Sopenharmony_ci moduleName: 'entry_test', 2414e41f4b71Sopenharmony_ci parameters: { 2415e41f4b71Sopenharmony_ci 'bundleName': 'com.example.myapplication', 2416e41f4b71Sopenharmony_ci //与com.example.myapplication.UIExtAbility配置的type相同 2417e41f4b71Sopenharmony_ci 'ability.want.params.uiExtensionType': 'sys/commonUI' 2418e41f4b71Sopenharmony_ci } 2419e41f4b71Sopenharmony_ci }; 2420e41f4b71Sopenharmony_ci 2421e41f4b71Sopenharmony_ci try { 2422e41f4b71Sopenharmony_ci this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => { 2423e41f4b71Sopenharmony_ci if (err.code) { 2424e41f4b71Sopenharmony_ci // 处理业务逻辑错误 2425e41f4b71Sopenharmony_ci console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`); 2426e41f4b71Sopenharmony_ci return; 2427e41f4b71Sopenharmony_ci } 2428e41f4b71Sopenharmony_ci // 执行正常业务 2429e41f4b71Sopenharmony_ci console.info('requestModalUIExtension succeed'); 2430e41f4b71Sopenharmony_ci }); 2431e41f4b71Sopenharmony_ci } catch (err) { 2432e41f4b71Sopenharmony_ci // 处理入参错误异常 2433e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 2434e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 2435e41f4b71Sopenharmony_ci console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`); 2436e41f4b71Sopenharmony_ci } 2437e41f4b71Sopenharmony_ci } 2438e41f4b71Sopenharmony_ci} 2439e41f4b71Sopenharmony_ci``` 2440e41f4b71Sopenharmony_ci 2441e41f4b71Sopenharmony_ci## ServiceExtensionContext.openLink<sup>12+<sup> 2442e41f4b71Sopenharmony_ciopenLink(link:string, options?: OpenLinkOptions): Promise<void> 2443e41f4b71Sopenharmony_ci 2444e41f4b71Sopenharmony_ci通过AppLinking启动UIAbility,使用Promise异步回调。 2445e41f4b71Sopenharmony_ci 2446e41f4b71Sopenharmony_ci通过在link字段中传入标准格式的URL,基于隐式want匹配规则拉起目标UIAbility。目标方必须具备以下过滤器特征,才能处理AppLinking链接: 2447e41f4b71Sopenharmony_ci- "actions"列表中包含"ohos.want.action.viewData"。 2448e41f4b71Sopenharmony_ci- "entities"列表中包含"entity.system.browsable"。 2449e41f4b71Sopenharmony_ci- "uris"列表中包含"scheme"为"https"且"domainVerify"为true的元素。 2450e41f4b71Sopenharmony_ci 2451e41f4b71Sopenharmony_ci传入的参数不合法时,如未设置必选参数或link字符串不是标准格式的URL,接口会直接抛出异常。参数校验通过,拉起目标方时出现的错误通过promise返回错误信息。 2452e41f4b71Sopenharmony_ci 2453e41f4b71Sopenharmony_ci> **说明:** 2454e41f4b71Sopenharmony_ci> 2455e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2456e41f4b71Sopenharmony_ci 2457e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2458e41f4b71Sopenharmony_ci 2459e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。 2460e41f4b71Sopenharmony_ci 2461e41f4b71Sopenharmony_ci**参数:** 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2464e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2465e41f4b71Sopenharmony_ci| link | string | 是 | 指示要打开的标准格式URL。 | 2466e41f4b71Sopenharmony_ci| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | 否 | 打开URL的选项参数。 | 2467e41f4b71Sopenharmony_ci 2468e41f4b71Sopenharmony_ci**返回值:** 2469e41f4b71Sopenharmony_ci 2470e41f4b71Sopenharmony_ci| 类型 | 说明 | 2471e41f4b71Sopenharmony_ci| -------- | -------- | 2472e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2473e41f4b71Sopenharmony_ci 2474e41f4b71Sopenharmony_ci**错误码:** 2475e41f4b71Sopenharmony_ci 2476e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2477e41f4b71Sopenharmony_ci 2478e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2479e41f4b71Sopenharmony_ci| ------- | -------- | 2480e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2481e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2482e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2483e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 2484e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 2485e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 2486e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 2487e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 2488e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 2489e41f4b71Sopenharmony_ci| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2490e41f4b71Sopenharmony_ci| 16000010 | The call with the continuation flag is forbidden. | 2491e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2492e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 2493e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 2494e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 2495e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2496e41f4b71Sopenharmony_ci 2497e41f4b71Sopenharmony_ci**示例:** 2498e41f4b71Sopenharmony_ci 2499e41f4b71Sopenharmony_ci```ts 2500e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit'; 2501e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2502e41f4b71Sopenharmony_ci 2503e41f4b71Sopenharmony_cifunction log(info: string) { 2504e41f4b71Sopenharmony_ci console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2505e41f4b71Sopenharmony_ci} 2506e41f4b71Sopenharmony_ci 2507e41f4b71Sopenharmony_ciexport default class ServiceExtAbility extends ServiceExtensionAbility { 2508e41f4b71Sopenharmony_ci onCreate(want: Want) { 2509e41f4b71Sopenharmony_ci log(`ServiceExtAbility OnCreate`); 2510e41f4b71Sopenharmony_ci } 2511e41f4b71Sopenharmony_ci 2512e41f4b71Sopenharmony_ci onRequest(want: Want, startId: number) { 2513e41f4b71Sopenharmony_ci log(`ServiceExtAbility onRequest`); 2514e41f4b71Sopenharmony_ci let link: string = 'https://www.example.com'; 2515e41f4b71Sopenharmony_ci let openLinkOptions: OpenLinkOptions = { 2516e41f4b71Sopenharmony_ci appLinkingOnly: false 2517e41f4b71Sopenharmony_ci }; 2518e41f4b71Sopenharmony_ci try { 2519e41f4b71Sopenharmony_ci this.context.openLink( 2520e41f4b71Sopenharmony_ci link, 2521e41f4b71Sopenharmony_ci openLinkOptions 2522e41f4b71Sopenharmony_ci ).then(() => { 2523e41f4b71Sopenharmony_ci log(`open link success.`); 2524e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2525e41f4b71Sopenharmony_ci log(`open link failed, errCode ${JSON.stringify(err.code)}`); 2526e41f4b71Sopenharmony_ci }); 2527e41f4b71Sopenharmony_ci } 2528e41f4b71Sopenharmony_ci catch (e) { 2529e41f4b71Sopenharmony_ci log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2530e41f4b71Sopenharmony_ci } 2531e41f4b71Sopenharmony_ci } 2532e41f4b71Sopenharmony_ci 2533e41f4b71Sopenharmony_ci onDestroy() { 2534e41f4b71Sopenharmony_ci log(`ServiceExtAbility onDestroy`); 2535e41f4b71Sopenharmony_ci } 2536e41f4b71Sopenharmony_ci} 2537e41f4b71Sopenharmony_ci``` 2538e41f4b71Sopenharmony_ci 2539e41f4b71Sopenharmony_ci## ServiceExtensionContext.preStartMission<sup>12+<sup> 2540e41f4b71Sopenharmony_cipreStartMission(bundleName:string, moduleName: string, abilitName: string, startTime: string): Promise<void> 2541e41f4b71Sopenharmony_ci 2542e41f4b71Sopenharmony_ci打开原子化服务跳过loading框并预打开窗口,使用Promise异步回调。 2543e41f4b71Sopenharmony_ci 2544e41f4b71Sopenharmony_ci参数校验通过,拉起目标方时出现的错误需要通过异常机制捕获。 2545e41f4b71Sopenharmony_ci 2546e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.PRE_START_ATOMIC_SERVICE 2547e41f4b71Sopenharmony_ci 2548e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2549e41f4b71Sopenharmony_ci 2550e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。 2551e41f4b71Sopenharmony_ci 2552e41f4b71Sopenharmony_ci**参数:** 2553e41f4b71Sopenharmony_ci 2554e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 2555e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 2556e41f4b71Sopenharmony_ci| bundleName | string | 是 | 打开原子化服务对应的包名。 | 2557e41f4b71Sopenharmony_ci| moduleName | string | 是 | 打开原子化服务对应的模块名。 | 2558e41f4b71Sopenharmony_ci| abilityName | string | 是 | 打开原子化服务对应的能力名。 | 2559e41f4b71Sopenharmony_ci| startTime | string | 是 | 打开原子化服务对应的开始时间,单位为毫秒级的时间戳。 | 2560e41f4b71Sopenharmony_ci 2561e41f4b71Sopenharmony_ci 2562e41f4b71Sopenharmony_ci**返回值:** 2563e41f4b71Sopenharmony_ci 2564e41f4b71Sopenharmony_ci| 类型 | 说明 | 2565e41f4b71Sopenharmony_ci| -------- | -------- | 2566e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2567e41f4b71Sopenharmony_ci 2568e41f4b71Sopenharmony_ci**错误码:** 2569e41f4b71Sopenharmony_ci 2570e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2571e41f4b71Sopenharmony_ci 2572e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2573e41f4b71Sopenharmony_ci| ------- | -------- | 2574e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2575e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2576e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2577e41f4b71Sopenharmony_ci| 16300007 | The target free install task does not exist. | 2578e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2579e41f4b71Sopenharmony_ci 2580e41f4b71Sopenharmony_ci**示例:** 2581e41f4b71Sopenharmony_ci 2582e41f4b71Sopenharmony_ci```ts 2583e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2584e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 2585e41f4b71Sopenharmony_ci 2586e41f4b71Sopenharmony_cifunction log(info: string) { 2587e41f4b71Sopenharmony_ci console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`); 2588e41f4b71Sopenharmony_ci} 2589e41f4b71Sopenharmony_ci 2590e41f4b71Sopenharmony_ciexport default class ServiceExtAbility extends ServiceExtensionAbility { 2591e41f4b71Sopenharmony_ci onCreate(want: Want) { 2592e41f4b71Sopenharmony_ci log(`ServiceExtAbility OnCreate`); 2593e41f4b71Sopenharmony_ci } 2594e41f4b71Sopenharmony_ci 2595e41f4b71Sopenharmony_ci onRequest(want: Want, startId: number) { 2596e41f4b71Sopenharmony_ci log(`ServiceExtAbility onRequest`); 2597e41f4b71Sopenharmony_ci try { 2598e41f4b71Sopenharmony_ci this.context.preStartMission( 2599e41f4b71Sopenharmony_ci want.bundleName, 2600e41f4b71Sopenharmony_ci want.moduleName, 2601e41f4b71Sopenharmony_ci want.abilityName, 2602e41f4b71Sopenharmony_ci want.parameters["ohos.aafwk.param.startTime"] 2603e41f4b71Sopenharmony_ci ).then(() => { 2604e41f4b71Sopenharmony_ci log(`pre-start mission success.`); 2605e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 2606e41f4b71Sopenharmony_ci log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`); 2607e41f4b71Sopenharmony_ci }); 2608e41f4b71Sopenharmony_ci } 2609e41f4b71Sopenharmony_ci catch (e) { 2610e41f4b71Sopenharmony_ci log(`exception occured, errCode ${JSON.stringify(e.code)}`); 2611e41f4b71Sopenharmony_ci } 2612e41f4b71Sopenharmony_ci } 2613e41f4b71Sopenharmony_ci 2614e41f4b71Sopenharmony_ci onDestroy() { 2615e41f4b71Sopenharmony_ci log(`ServiceExtAbility onDestroy`); 2616e41f4b71Sopenharmony_ci } 2617e41f4b71Sopenharmony_ci} 2618e41f4b71Sopenharmony_ci``` 2619e41f4b71Sopenharmony_ci 2620e41f4b71Sopenharmony_ci## ServiceExtensionContext.startUIServiceExtensionAbility<sup>13+<sup> 2621e41f4b71Sopenharmony_cistartUIServiceExtensionAbility(want: Want): Promise<void> 2622e41f4b71Sopenharmony_ci 2623e41f4b71Sopenharmony_ci启动一个新的[UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md)(Promise形式)。 2624e41f4b71Sopenharmony_ci 2625e41f4b71Sopenharmony_ci 2626e41f4b71Sopenharmony_ci> **说明:** 2627e41f4b71Sopenharmony_ci> 2628e41f4b71Sopenharmony_ci> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 2629e41f4b71Sopenharmony_ci> 2630e41f4b71Sopenharmony_ci 2631e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 2632e41f4b71Sopenharmony_ci 2633e41f4b71Sopenharmony_ci**系统接口**: 此接口为系统接口。 2634e41f4b71Sopenharmony_ci 2635e41f4b71Sopenharmony_ci**参数:** 2636e41f4b71Sopenharmony_ci| 参数名 | 类型 | 只读 | 可选 | 说明 | 2637e41f4b71Sopenharmony_ci| ------ | ---- | ---- | -------------------- | -------------------- | 2638e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | 是 | 否 | [Want](js-apis-app-ability-want.md)类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。 | 2639e41f4b71Sopenharmony_ci 2640e41f4b71Sopenharmony_ci**返回值:** 2641e41f4b71Sopenharmony_ci 2642e41f4b71Sopenharmony_ci| 类型 | 说明 | 2643e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------- | 2644e41f4b71Sopenharmony_ci| Promise<void> | Promise对象。无返回结果的Promise对象。 | 2645e41f4b71Sopenharmony_ci 2646e41f4b71Sopenharmony_ci**错误码:** 2647e41f4b71Sopenharmony_ci 2648e41f4b71Sopenharmony_ci以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 2649e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 2650e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------------------------| 2651e41f4b71Sopenharmony_ci| 201 | The application does not have permission to call the interface. | 2652e41f4b71Sopenharmony_ci| 202 | The application is not system-app, can not use system-api. | 2653e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2654e41f4b71Sopenharmony_ci| 801 | The Ability is not supported. | 2655e41f4b71Sopenharmony_ci| 16000001 | The specified ability does not exist. | 2656e41f4b71Sopenharmony_ci| 16000002 | Incorrect ability type. | 2657e41f4b71Sopenharmony_ci| 16000004 | Failed to start the invisible ability. | 2658e41f4b71Sopenharmony_ci| 16000005 | The specified process does not have the permission. | 2659e41f4b71Sopenharmony_ci| 16000006 | Cross-user operations are not allowed. | 2660e41f4b71Sopenharmony_ci| 16000008 | The crowdtesting application expires. | 2661e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. | 2662e41f4b71Sopenharmony_ci| 16000012 | The application is controlled. | 2663e41f4b71Sopenharmony_ci| 16000013 | The application is controlled by EDM. | 2664e41f4b71Sopenharmony_ci| 16000019 | No matching ability is found. | 2665e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 2666e41f4b71Sopenharmony_ci| 16200001 | The caller has been released. | 2667e41f4b71Sopenharmony_ci 2668e41f4b71Sopenharmony_ci**示例:** 2669e41f4b71Sopenharmony_ci 2670e41f4b71Sopenharmony_ci```ts 2671e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 2672e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 2673e41f4b71Sopenharmony_ci 2674e41f4b71Sopenharmony_ciexport default class MyServiceExtensionAbility extends ServiceExtensionAbility { 2675e41f4b71Sopenharmony_ci onRequest(want: Want, startId: number) { 2676e41f4b71Sopenharmony_ci const startWant: Want = { 2677e41f4b71Sopenharmony_ci bundleName: 'com.example.myapplication', 2678e41f4b71Sopenharmony_ci abilityName: 'UIServiceExtensionAbility' 2679e41f4b71Sopenharmony_ci } 2680e41f4b71Sopenharmony_ci // 启动一个UIServiceExtensionAbility 2681e41f4b71Sopenharmony_ci this.context.startUIServiceExtensionAbility(startWant).then(() => { 2682e41f4b71Sopenharmony_ci console.info('succeeded'); 2683e41f4b71Sopenharmony_ci }).catch((error: BusinessError) => { 2684e41f4b71Sopenharmony_ci console.error(`error code: ${error.code}, error essage : ${error.message}`); 2685e41f4b71Sopenharmony_ci }) 2686e41f4b71Sopenharmony_ci } 2687e41f4b71Sopenharmony_ci} 2688e41f4b71Sopenharmony_ci```