1# ServiceExtAbilityContext 2 3 4> **NOTE** 5> 6> The initial APIs of this module are supported since API version 9. API version 9 is a canary version for trial use. The APIs of this version may be unstable. 7 8 9## Attributes 10 11**System capability**: SystemCapability.Ability.AbilityRuntime.Core 12 13| Name| Type| Readable| Writable| Description| 14| -------- | -------- | -------- | -------- | -------- | 15| extensionAbilityInfo | ExtensionAbilityInfo | Yes| No| Extension ability information. | 16 17 18## startAbility 19 20startAbility(want: Want, callback: AsyncCallback<void>): void 21 22Starts an ability. This API uses an asynchronous callback to return the result. 23 24**System capability**: SystemCapability.Ability.AbilityRuntime.Core 25 26**Parameters** 27 28| Name| Type| Mandatory| Description| 29| -------- | -------- | -------- | -------- | 30| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 31| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 32 33**Example** 34 35```js 36var want = { 37 "deviceId": "", 38 "bundleName": "com.extreme.test", 39 "abilityName": "com.extreme.test.MainAbility" 40}; 41this.context.startAbility(want, (error) => { 42 console.log("error.code = " + error.code) 43}) 44``` 45 46## startAbility 47 48startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 49 50Starts an ability with **options** specified. This API uses an asynchronous callback to return the result. 51 52**System capability**: SystemCapability.Ability.AbilityRuntime.Core 53 54**Parameters** 55 56| Name| Type| Mandatory| Description| 57| -------- | -------- | -------- | -------- | 58| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.| 59| options | StartOptions | Yes| Parameters used for starting the ability.| 60| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 61 62**Example** 63 64```js 65var want = { 66 "deviceId": "", 67 "bundleName": "com.extreme.test", 68 "abilityName": "com.extreme.test.MainAbility" 69}; 70var options = { 71 windowMode: 0, 72}; 73this.context.startAbility(want, options, (error) => { 74 console.log("error.code = " + error.code) 75}) 76``` 77 78 79## startAbility 80 81startAbility(want: Want, options: StartOptions): Promise<void> 82 83Starts an ability with **options** specified. This API uses a promise to return the result. 84 85**System capability**: SystemCapability.Ability.AbilityRuntime.Core 86 87**Parameters** 88 89| Name| Type| Mandatory| Description| 90| -------- | -------- | -------- | -------- | 91| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 92| options | StartOptions | Yes| Parameters used for starting the ability.| 93 94**Return value** 95 96| Type| Description| 97| -------- | -------- | 98| Promise<void> | Promise used to return the result.| 99 100**Example** 101```js 102var want = { 103 "deviceId": "", 104 "bundleName": "com.extreme.test", 105 "abilityName": "com.extreme.test.MainAbility" 106}; 107var options = { 108 windowMode: 0, 109}; 110this.context.startAbility(want, options) 111.then((data) => { 112 console.log('Operation successful.') 113}).catch((error) => { 114 console.log('Operation failed.'); 115}) 116``` 117 118## startAbilityWithAccount 119 120startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void 121 122Starts an ability based on an account. This API uses an asynchronous callback to return the result. 123 124**System capability**: SystemCapability.Ability.AbilityRuntime.Core 125 126**Parameters** 127 128| Name| Type| Mandatory| Description| 129| -------- | -------- | -------- | -------- | 130| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 131| accountId | number | Yes| Account ID. | 132| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 133 134**Example** 135 136```js 137var want = { 138 "deviceId": "", 139 "bundleName": "com.extreme.test", 140 "abilityName": "com.extreme.test.MainAbility" 141}; 142var accountId = 11; 143this.context.startAbility(want, accountId, (error) => { 144 console.log("error.code = " + error.code) 145}) 146``` 147 148## startAbilityWithAccount 149 150startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void>): void 151 152Starts an ability based on an account and **options**. This API uses an asynchronous callback to return the result. 153 154**System capability**: SystemCapability.Ability.AbilityRuntime.Core 155 156**Parameters** 157 158| Name| Type| Mandatory| Description| 159| -------- | -------- | -------- | -------- | 160| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.| 161| accountId | number | Yes| Account ID. | 162| options | StartOptions | Yes| Parameters used for starting the ability.| 163| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 164 165**Example** 166 167```js 168var want = { 169 "deviceId": "", 170 "bundleName": "com.extreme.test", 171 "abilityName": "com.extreme.test.MainAbility" 172}; 173var options = { 174 windowMode: 0, 175}; 176var accountId = 11; 177this.context.startAbility(want, accountId, options, (error) => { 178 console.log("error.code = " + error.code) 179}) 180``` 181 182 183## startAbilityWithAccount 184 185startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void> 186 187Starts an ability based on an account and **options**. This API uses a promise to return the result. 188 189**System capability**: SystemCapability.Ability.AbilityRuntime.Core 190 191**Parameters** 192 193| Name| Type| Mandatory| Description| 194| -------- | -------- | -------- | -------- | 195| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 196| accountId | number | Yes| Account ID. | 197| options | StartOptions | No| Parameters used for starting the ability.| 198 199**Return value** 200 201| Type| Description| 202| -------- | -------- | 203| Promise<void> | Promise used to return the result.| 204 205**Example** 206```js 207var want = { 208 "deviceId": "", 209 "bundleName": "com.extreme.test", 210 "abilityName": "com.extreme.test.MainAbility" 211}; 212var options = { 213 windowMode: 0, 214}; 215var accountId = 11; 216this.context.startAbility(want, accountId, options) 217.then((data) => { 218 console.log('Operation successful.') 219}).catch((error) => { 220 console.log('Operation failed.'); 221}) 222``` 223 224## terminateSelf 225 226terminateSelf(callback: AsyncCallback<void>): void 227 228Terminates this ability. This API uses an asynchronous callback to return the result. 229 230**System capability**: SystemCapability.Ability.AbilityRuntime.Core 231 232**Parameters** 233 234| Name| Type| Mandatory| Description| 235| -------- | -------- | -------- | -------- | 236| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 237 238**Example** 239 240```js 241this.context.terminateSelf((err) => { 242 console.log('terminateSelf result:' + JSON.stringify(err)); 243}); 244``` 245 246## terminateSelf 247 248terminateSelf(): Promise<void> 249 250Terminates this ability. This API uses a promise to return the result. 251 252**System capability**: SystemCapability.Ability.AbilityRuntime.Core 253 254**Return value** 255 256| Type| Description| 257| -------- | -------- | 258| Promise<void> | Promise used to return the result.| 259 260**Example** 261 262```js 263this.context.terminateSelf(want).then((data) => { 264 console.log('success:' + JSON.stringify(data)); 265}).catch((error) => { 266 console.log('failed:' + JSON.stringify(error)); 267}); 268``` 269 270 271## connectAbility 272 273connectAbility(want: Want, options: ConnectOptions): number 274 275Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to another ability. 276 277**System capability**: SystemCapability.Ability.AbilityRuntime.Core 278 279**Parameters** 280 281| Name| Type| Mandatory| Description| 282| -------- | -------- | -------- | -------- | 283| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 284| options | ConnectOptions | Yes| Connection channel.| 285 286**Return value** 287 288| Type| Description| 289| -------- | -------- | 290| number | ID of the connection between the two abilities.| 291 292**Example** 293```js 294var want = { 295 "deviceId": "", 296 "bundleName": "com.extreme.test", 297 "abilityName": "com.extreme.test.MainAbility" 298} 299var options = { 300 onConnect: (elementName, remote) => { 301 console.log('connectAbility onConnect, elementName: ' + elementName + ', remote: ' remote) 302 }, 303 onDisconnect: (elementName) => { 304 console.log('connectAbility onDisconnect, elementName: ' + elementName) 305 }, 306 onFailed: (code) => { 307 console.log('connectAbility onFailed, code: ' + code) 308 } 309} 310this.context.connectAbility(want, options) { 311 console.log('code: ' + code) 312} 313``` 314 315## connectAbilityWithAccount 316 317connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number 318 319Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to another ability based on an account. 320 321**System capability**: SystemCapability.Ability.AbilityRuntime.Core 322 323**Parameters** 324 325| Name| Type| Mandatory| Description| 326| -------- | -------- | -------- | -------- | 327| want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| 328| accountId | number | Yes| Account ID.| 329| options | ConnectOptions | Yes| Connection channel.| 330 331**Return value** 332 333| Type| Description| 334| -------- | -------- | 335| number | ID of the connection between the two abilities.| 336 337**Example** 338```js 339var want = { 340 "deviceId": "", 341 "bundleName": "com.extreme.test", 342 "abilityName": "com.extreme.test.MainAbility" 343} 344var accountId = 111; 345var options = { 346 onConnect: (elementName, remote) => { 347 console.log('connectAbility onConnect, elementName: ' + elementName + ', remote: ' remote) 348 }, 349 onDisconnect: (elementName) => { 350 console.log('connectAbility onDisconnect, elementName: ' + elementName) 351 }, 352 onFailed: (code) => { 353 console.log('connectAbility onFailed, code: ' + code) 354 } 355} 356this.context.connectAbility(want, accountId, options) { 357 console.log('code: ' + code) 358} 359``` 360 361## disconnectAbility 362 363disconnectAbility(connection: number, callback:AsyncCallback\<void>): void 364 365Disconnects this ability from another ability. This API uses an asynchronous callback to return the result. 366 367**System capability**: SystemCapability.Ability.AbilityRuntime.Core 368 369**Parameters** 370 371| Name| Type| Mandatory| Description| 372| -------- | -------- | -------- | -------- | 373| connection | number | Yes| ID of the connection to be disconnected.| 374| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 375 376**Example** 377 378```js 379var connection = 111; 380this.context.disconnectAbility(connection, () => { 381 console.log('disconnection') 382}) 383``` 384 385## disconnectAbility 386 387disconnectAbility(connection: number): Promise\<void> 388 389Disconnects this ability from another ability. This API uses a promise to return the result. 390 391**System capability**: SystemCapability.Ability.AbilityRuntime.Core 392 393**Parameters** 394 395| Name| Type| Mandatory| Description| 396| -------- | -------- | -------- | -------- | 397| connection | number | Yes| ID of the connection to be disconnected.| 398 399**Return value** 400 401| Type| Description| 402| -------- | -------- | 403| Promise<void> | Promise used to return the result.| 404 405**Example** 406 407```js 408var connection = 111; 409this.context.disconnectAbility(connection).then(() => { 410 console.log('disconnected successfully') 411}).catch((err) => { 412 console.log('disconnected failed') 413}) 414``` 415