js-apis-ability-context.md 58.8 KB
Newer Older
W
wusongqing 已提交
1 2
# AbilityContext

3 4
The **AbilityContext** module, inherited from **Context**, implements the context for abilities.

5
This module provides APIs for accessing ability-specific resources. You can use the APIs to start and terminate an ability, obtain the caller interface, and request permissions from users by displaying a dialog box.
6

W
wusongqing 已提交
7
> **NOTE**
8
>
9 10
>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>  - The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
11

W
wusongqing 已提交
12 13 14 15
## Usage

Before using the **AbilityContext** module, you must define a child class that inherits from **Ability**.

G
Gloria 已提交
16 17 18 19
```ts
import Ability from '@ohos.app.ability.UIAbility';

 let context = undefined;
W
wusongqing 已提交
20 21
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
G
Gloria 已提交
22
        context = this.context;
W
wusongqing 已提交
23 24 25 26
    }
}
```

W
wusongqing 已提交
27
## Attributes
W
wusongqing 已提交
28

W
wusongqing 已提交
29 30
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

W
wusongqing 已提交
31
| Name| Type| Readable| Writable| Description|
W
wusongqing 已提交
32
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
33 34
| abilityInfo | AbilityInfo | Yes| No| Ability information.|
| currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP.|
35
| config | [Configuration](js-apis-configuration.md) | Yes| No| Configuration information.|
W
wusongqing 已提交
36

W
wusongqing 已提交
37
## AbilityContext.startAbility
W
wusongqing 已提交
38

G
Gloria 已提交
39
startAbility(want: Want, callback: AsyncCallback<void>): void;
W
wusongqing 已提交
40

41
Starts an ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
42

W
wusongqing 已提交
43
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
44

W
wusongqing 已提交
45
**Parameters**
W
wusongqing 已提交
46

W
wusongqing 已提交
47 48
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
49
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
W
wusongqing 已提交
50
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
51

G
Gloria 已提交
52 53 54 55 56 57 58
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
59 60
**Example**

G
Gloria 已提交
61
  ```ts
W
wusongqing 已提交
62
  var want = {
G
Gloria 已提交
63 64
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
W
wusongqing 已提交
65
  };
G
Gloria 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

  try {
    this.context.startAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
83 84 85
  ```


W
wusongqing 已提交
86
## AbilityContext.startAbility
W
wusongqing 已提交
87

G
Gloria 已提交
88
startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void;
W
wusongqing 已提交
89

G
Gloria 已提交
90
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
91

W
wusongqing 已提交
92
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
93

W
wusongqing 已提交
94
**Parameters**
W
wusongqing 已提交
95

W
wusongqing 已提交
96 97
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
98
| want | [Want](js-apis-application-Want.md)  | Yes| Want information about the target ability.|
W
wusongqing 已提交
99
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
W
wusongqing 已提交
100
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
101

G
Gloria 已提交
102 103 104 105 106 107 108
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
109
**Example**
110

G
Gloria 已提交
111
  ```ts
W
wusongqing 已提交
112
  var want = {
G
Gloria 已提交
113 114 115
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
W
wusongqing 已提交
116 117
  };
  var options = {
G
Gloria 已提交
118
    windowMode: 0
W
wusongqing 已提交
119 120
  };

G
Gloria 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
  ```
W
wusongqing 已提交
138

W
wusongqing 已提交
139
## AbilityContext.startAbility
W
wusongqing 已提交
140

W
wusongqing 已提交
141
startAbility(want: Want, options?: StartOptions): Promise<void>;
W
wusongqing 已提交
142

W
wusongqing 已提交
143
Starts an ability. This API uses a promise to return the result.
W
wusongqing 已提交
144

W
wusongqing 已提交
145
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**Parameters**
W
wusongqing 已提交
148

W
wusongqing 已提交
149 150
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
151
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
W
wusongqing 已提交
152
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
153 154

**Return value**
W
wusongqing 已提交
155

W
wusongqing 已提交
156 157 158
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to return the result.|
W
wusongqing 已提交
159

G
Gloria 已提交
160 161 162 163 164 165 166
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
167 168
**Example**

G
Gloria 已提交
169
  ```ts
W
wusongqing 已提交
170
  var want = {
G
Gloria 已提交
171 172
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
W
wusongqing 已提交
173
  };
W
wusongqing 已提交
174 175 176
  var options = {
  	windowMode: 0,
  };
G
Gloria 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

  try {
    this.context.startAbility(want, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
194 195 196
  ```


W
wusongqing 已提交
197
## AbilityContext.startAbilityForResult
W
wusongqing 已提交
198 199 200

startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void;

201
Starts an ability. This API uses an asynchronous callback to return the result when the ability is terminated.
W
wusongqing 已提交
202

W
wusongqing 已提交
203
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
204

W
wusongqing 已提交
205
**Parameters**
W
wusongqing 已提交
206

W
wusongqing 已提交
207 208
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
209
| want |[Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
W
wusongqing 已提交
210
| callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
211

G
Gloria 已提交
212 213 214 215 216 217
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
W
wusongqing 已提交
218

W
wusongqing 已提交
219 220
**Example**

G
Gloria 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  ```ts
  var want = {
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
  };

  try {
    this.context.startAbilityForResult(want, (error, result) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log("startAbilityForResult succeed, result.resultCode = " +
        result.resultCode)
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
245 246
  ```

W
wusongqing 已提交
247
## AbilityContext.startAbilityForResult
W
wusongqing 已提交
248 249 250

startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void;

251
Starts an ability with start options specified. This API uses an asynchronous callback to return the result when the ability is terminated.
W
wusongqing 已提交
252

W
wusongqing 已提交
253
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
254

W
wusongqing 已提交
255
**Parameters**
W
wusongqing 已提交
256

W
wusongqing 已提交
257 258
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
259
| want |[Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
W
wusongqing 已提交
260
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
W
wusongqing 已提交
261
| callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
262

G
Gloria 已提交
263 264 265 266 267 268
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
W
wusongqing 已提交
269

W
wusongqing 已提交
270 271
**Example**

G
Gloria 已提交
272 273 274 275 276 277
  ```ts
  var want = {
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
  };
W
wusongqing 已提交
278 279 280
  var options = {
    windowMode: 0,
  };
G
Gloria 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

  try {
    this.context.startAbilityForResult(want, options, (error, result) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log("startAbilityForResult succeed, result.resultCode = " +
        result.resultCode)
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
299 300
  ```

W
wusongqing 已提交
301

W
wusongqing 已提交
302
## AbilityContext.startAbilityForResult
W
wusongqing 已提交
303

W
wusongqing 已提交
304
startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>;
W
wusongqing 已提交
305

W
wusongqing 已提交
306
Starts an ability. This API uses a promise to return the result when the ability is terminated.
W
wusongqing 已提交
307

W
wusongqing 已提交
308
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
309

W
wusongqing 已提交
310
**Parameters**
W
wusongqing 已提交
311

W
wusongqing 已提交
312 313
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
314
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
W
wusongqing 已提交
315
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
316

W
wusongqing 已提交
317

W
wusongqing 已提交
318 319
**Return value**

W
wusongqing 已提交
320 321 322
| Type| Description|
| -------- | -------- |
| Promise<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Promise used to return the result.|
W
wusongqing 已提交
323

G
Gloria 已提交
324 325 326 327 328 329 330
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
331 332
**Example**

G
Gloria 已提交
333 334 335 336 337
  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
  };
W
wusongqing 已提交
338
  var options = {
G
Gloria 已提交
339
  	windowMode: 0,
W
wusongqing 已提交
340
  };
G
Gloria 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

  try {
    this.context.startAbilityForResult(want, options)
      .then((result) => {
        // Carry out normal service processing.
        console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode);
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
358 359
  ```

360 361
## AbilityContext.startAbilityForResultWithAccount

362
startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void;
363 364 365 366 367 368 369 370 371 372 373 374 375

Starts an ability. This API uses an asynchronous callback to return the result when the account of the ability is destroyed.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
376
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
377
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
378 379
| callback | AsyncCallback\<AbilityResult\> | Yes| Callback used to return the result.|

G
Gloria 已提交
380 381 382 383 384 385 386
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

387 388
**Example**

G
Gloria 已提交
389
  ```ts
390
  var want = {
G
Gloria 已提交
391 392 393
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
394 395
  };
  var accountId = 100;
G
Gloria 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

  try {
    this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
        result.resultCode)
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  ```


## AbilityContext.startAbilityForResultWithAccount

startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;

Starts an ability with start options specified. This API uses an asynchronous callback to return the result when the account of the ability is destroyed.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
433
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
434
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
435 436 437
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
438 439 440 441 442 443 444
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

445 446
**Example**

G
Gloria 已提交
447
  ```ts
448
  var want = {
G
Gloria 已提交
449 450 451
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
452 453 454
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
455
    windowMode: 0
456
  };
G
Gloria 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

  try {
    this.context.startAbilityForResultWithAccount(want, accountId, options, (error, result) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
        result.resultCode)
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
475 476 477
  ```


478
## AbilityContext.startAbilityForResultWithAccount
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\>;

Starts an ability with start options specified. This API uses a promise to return the result when the account of the ability is destroyed.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
494
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
495
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
496 497 498 499 500 501 502 503
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;AbilityResult&gt; | Promise used to return the result.|

G
Gloria 已提交
504 505 506 507 508 509 510
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

511 512
**Example**

G
Gloria 已提交
513
  ```ts
514
  var want = {
G
Gloria 已提交
515 516 517
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
518 519 520
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
521
    windowMode: 0
522
  };
G
Gloria 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

  try {
    this.context.startAbilityForResultWithAccount(want, accountId, options)
      .then((result) => {
        // Carry out normal service processing.
        console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
          result.resultCode)
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
541
  ```
542 543 544 545 546 547 548 549 550 551 552 553 554 555
## AbilityContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;

Starts a new Service Extension ability. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
556
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
557 558
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
559 560 561 562 563 564 565
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

566 567
**Example**

G
Gloria 已提交
568
  ```ts
569
  var want = {
G
Gloria 已提交
570 571 572
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
573
  };
G
Gloria 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  ```

## AbilityContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want): Promise\<void>;

Starts a new Service Extension ability. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
607 608 609 610 611 612 613 614
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
615 616 617

**Example**

G
Gloria 已提交
618
  ```ts
619
  var want = {
G
Gloria 已提交
620 621 622
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
623
  };
G
Gloria 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
641
  ```
G
Gloria 已提交
642

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
## AbilityContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

Starts a new Service Extension ability with the account ID specified. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
659
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
660 661 662
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
663 664 665 666 667 668 669
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

670 671
**Example**

G
Gloria 已提交
672
  ```ts
673
  var want = {
G
Gloria 已提交
674 675 676
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
677 678
  };
  var accountId = 100;
G
Gloria 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  ```

## AbilityContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

Starts a new Service Extension ability with the account ID specified. This API uses a promise to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
714
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
715 716
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

G
Gloria 已提交
717 718 719 720 721 722 723
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

724 725
**Example**

G
Gloria 已提交
726
  ```ts
727
  var want = {
G
Gloria 已提交
728 729 730
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
731 732
  };
  var accountId = 100;
G
Gloria 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
750 751
  ```
## AbilityContext.stopServiceExtensionAbility
752

753 754 755 756 757 758 759 760 761 762 763 764
stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;

Stops a Service Extension ability in the same application. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
765
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
766 767
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
768 769 770 771 772 773 774
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

775 776
**Example**

G
Gloria 已提交
777
  ```ts
778
  var want = {
G
Gloria 已提交
779 780 781
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
782
  };
G
Gloria 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  ```

## AbilityContext.stopServiceExtensionAbility

stopServiceExtensionAbility(want: Want): Promise\<void>;

Stops a Service Extension ability in the same application. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
816 817 818 819 820 821 822 823
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
824 825 826

**Example**

G
Gloria 已提交
827
  ```ts
828
  var want = {
G
Gloria 已提交
829 830 831
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
832
  };
G
Gloria 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  ```

## AbilityContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

Stops a Service Extension ability in the same application with the account ID specified. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
868
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
869 870 871
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
872 873 874 875 876 877 878
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

879 880
**Example**

G
Gloria 已提交
881
  ```ts
882
  var want = {
G
Gloria 已提交
883 884 885
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
886 887
  };
  var accountId = 100;
G
Gloria 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  ```

## AbilityContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

Stops a Service Extension ability in the same application with the account ID specified. This API uses a promise to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
923
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
924 925
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

G
Gloria 已提交
926 927 928 929 930 931 932
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

933 934
**Example**

G
Gloria 已提交
935
  ```ts
936
  var want = {
G
Gloria 已提交
937 938 939
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
940 941
  };
  var accountId = 100;
G
Gloria 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // Carry out normal service processing.
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
959
  ```
W
wusongqing 已提交
960

W
wusongqing 已提交
961
## AbilityContext.terminateSelf
W
wusongqing 已提交
962 963 964

terminateSelf(callback: AsyncCallback&lt;void&gt;): void;

965
Terminates this ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
966

W
wusongqing 已提交
967
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
968

W
wusongqing 已提交
969
**Parameters**
W
wusongqing 已提交
970

W
wusongqing 已提交
971 972
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
973
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
974

G
Gloria 已提交
975 976 977 978 979 980 981
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
982 983
**Example**

G
Gloria 已提交
984 985 986 987 988 989 990 991 992 993
  ```ts
  this.context.terminateSelf((error) => {
    if (error.code) {
      // Process service logic errors.
      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
        ' error.message: ' + JSON.stringify(error.message));
      return;
    }
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
W
wusongqing 已提交
994
  });
W
wusongqing 已提交
995 996 997
  ```


W
wusongqing 已提交
998
## AbilityContext.terminateSelf
W
wusongqing 已提交
999 1000 1001

terminateSelf(): Promise&lt;void&gt;;

W
wusongqing 已提交
1002
Terminates this ability. This API uses a promise to return the result.
W
wusongqing 已提交
1003

W
wusongqing 已提交
1004
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1005

W
wusongqing 已提交
1006
**Return value**
W
wusongqing 已提交
1007

W
wusongqing 已提交
1008 1009
| Type| Description|
| -------- | -------- |
1010
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1011

G
Gloria 已提交
1012 1013 1014 1015 1016 1017 1018
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
1019 1020
**Example**

G
Gloria 已提交
1021 1022 1023 1024
  ```ts
  this.context.terminateSelf().then((data) => {
    // Carry out normal service processing.
    console.log('terminateSelf succeed');
W
wusongqing 已提交
1025
  }).catch((error) => {
G
Gloria 已提交
1026 1027 1028
    // Process service logic errors.
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
W
wusongqing 已提交
1029 1030 1031 1032
  });
  ```


W
wusongqing 已提交
1033
## AbilityContext.terminateSelfWithResult
W
wusongqing 已提交
1034 1035 1036

terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void;

1037
Terminates this ability. This API uses an asynchronous callback to return the ability result information. It is used together with **startAbilityForResult**.
W
wusongqing 已提交
1038

W
wusongqing 已提交
1039
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1040

W
wusongqing 已提交
1041
**Parameters**
W
wusongqing 已提交
1042

W
wusongqing 已提交
1043 1044 1045 1046
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
1047

G
Gloria 已提交
1048 1049 1050 1051 1052 1053 1054
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

W
wusongqing 已提交
1055 1056
**Example**

G
Gloria 已提交
1057
  ```ts
1058
  var want = {
G
Gloria 已提交
1059 1060
    bundleName: "com.extreme.myapplication",
    abilityName: "SecondAbility"
1061 1062 1063 1064 1065 1066 1067
  }
  var resultCode = 100;
  // AbilityResult information returned to the caller.
  var abilityResult = {
    want,
    resultCode
  }
G
Gloria 已提交
1068 1069 1070 1071 1072 1073 1074 1075

  try {
    this.context.terminateSelfWithResult(abilityResult, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
W
wusongqing 已提交
1076
      }
G
Gloria 已提交
1077 1078 1079 1080 1081 1082 1083 1084
      // Carry out normal service processing.
      console.log('terminateSelfWithResult succeed');
    });
  } catch (paramError) {
      // Process input parameter errors.
      console.log('error.code: ' + JSON.stringify(paramError.code) +
        ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1085 1086 1087
  ```


W
wusongqing 已提交
1088
## AbilityContext.terminateSelfWithResult
W
wusongqing 已提交
1089 1090 1091

terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;;

1092
Terminates this ability. This API uses a promise to return the ability result information. It is used together with **startAbilityForResult**.
W
wusongqing 已提交
1093

W
wusongqing 已提交
1094
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1095

W
wusongqing 已提交
1096
**Parameters**
W
wusongqing 已提交
1097

W
wusongqing 已提交
1098 1099 1100
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
W
wusongqing 已提交
1101

W
wusongqing 已提交
1102 1103
**Return value**

W
wusongqing 已提交
1104 1105 1106
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1107

G
Gloria 已提交
1108 1109 1110 1111 1112 1113 1114 1115
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|


W
wusongqing 已提交
1116 1117
**Example**

G
Gloria 已提交
1118
  ```ts
1119
  var want = {
G
Gloria 已提交
1120 1121
    bundleName: "com.extreme.myapplication",
    abilityName: "SecondAbility"
1122 1123 1124 1125 1126 1127 1128
  }
  var resultCode = 100;
  // AbilityResult information returned to the caller.
  var abilityResult = {
    want,
    resultCode
  }
G
Gloria 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

  try {
    this.context.terminateSelfWithResult(abilityResult)
      .then((data) => {
        // Carry out normal service processing.
        console.log('terminateSelfWithResult succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
W
wusongqing 已提交
1145 1146
  }
  ```
W
wusongqing 已提交
1147

G
Gloria 已提交
1148
## AbilityContext.connectServiceExtensionAbility
1149

G
Gloria 已提交
1150
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1151 1152 1153 1154 1155 1156 1157 1158 1159

Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to another ability.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1160 1161
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Parameters for the connection.|
1162 1163 1164 1165 1166 1167 1168

**Return value**

| Type| Description|
| -------- | -------- |
| number | Result code of the ability connection.|

G
Gloria 已提交
1169 1170 1171 1172 1173 1174 1175
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1176 1177
**Example**

G
Gloria 已提交
1178
  ```ts
1179
  var want = {
G
Gloria 已提交
1180 1181 1182
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1183 1184 1185 1186 1187 1188
  };
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
G
Gloria 已提交
1189 1190 1191 1192 1193 1194 1195 1196 1197

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1198 1199 1200
  ```


G
Gloria 已提交
1201
## AbilityContext.connectServiceExtensionAbilityWithAccount
1202

G
Gloria 已提交
1203
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1204

1205
Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability with the account ID specified.
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1217
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
1218
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
G
Gloria 已提交
1219
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Parameters for the connection.|
1220 1221 1222 1223 1224 1225 1226

**Return value**

| Type| Description|
| -------- | -------- |
| number | Result code of the ability connection.|

G
Gloria 已提交
1227 1228 1229 1230 1231 1232 1233
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1234 1235
**Example**

G
Gloria 已提交
1236
  ```ts
1237
  var want = {
G
Gloria 已提交
1238 1239 1240
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1241 1242 1243 1244 1245 1246 1247
  };
  var accountId = 100;
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
G
Gloria 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1257 1258
  ```

G
Gloria 已提交
1259
## AbilityContext.disconnectServiceExtensionAbility
1260

G
Gloria 已提交
1261
disconnectServiceExtensionAbility(connection: number): Promise\<void>;
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

Disconnects a connection. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Result code of the ability connection.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

G
Gloria 已提交
1279 1280 1281 1282 1283 1284 1285
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1286
**Example**
1287

G
Gloria 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
  ```ts
  // connection is the return value of connectAbility.
  var connection = 1;

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
        // Carry out normal service processing.
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1308 1309
  ```

G
Gloria 已提交
1310
## AbilityContext.disconnectServiceExtensionAbility
1311

G
Gloria 已提交
1312
disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void;
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

Disconnects a connection. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Result code of the ability connection.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

G
Gloria 已提交
1325 1326 1327 1328 1329 1330 1331
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1332 1333
**Example**

G
Gloria 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
  ```ts
  // connection is the return value of connectServiceExtensionAbility.
  var connection = 1;

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('disconnectServiceExtensionAbility succeed');
1348
    });
G
Gloria 已提交
1349 1350 1351 1352 1353
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1354
  ```
W
wusongqing 已提交
1355

W
wusongqing 已提交
1356
## AbilityContext.startAbilityByCall
W
wusongqing 已提交
1357 1358 1359

startAbilityByCall(want: Want): Promise&lt;Caller&gt;;

1360
Starts an ability in the foreground or background and obtains the caller object for communicating with the ability.
W
wusongqing 已提交
1361

W
wusongqing 已提交
1362
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1363

G
Gloria 已提交
1364 1365
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
1366
**Parameters**
W
wusongqing 已提交
1367

W
wusongqing 已提交
1368 1369
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
1370
| want | [Want](js-apis-application-Want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.|
W
wusongqing 已提交
1371

W
wusongqing 已提交
1372 1373
**Return value**

W
wusongqing 已提交
1374 1375 1376
| Type| Description|
| -------- | -------- |
| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
W
wusongqing 已提交
1377

W
wusongqing 已提交
1378
**Example**
1379

G
Gloria 已提交
1380 1381 1382 1383
  Start an ability in the background.

  ```ts
  var caller = undefined;
W
wusongqing 已提交
1384

1385
  // Start an ability in the background by not passing parameters.
W
wusongqing 已提交
1386 1387 1388 1389 1390 1391
  var wantBackground = {
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: ""
  };
G
Gloria 已提交
1392 1393 1394 1395 1396

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // Carry out normal service processing.
W
wusongqing 已提交
1397
        caller = obj;
G
Gloria 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
  ```

  Start an ability in the foreground.

  ```ts
  var caller = undefined;
W
wusongqing 已提交
1415 1416 1417 1418 1419 1420 1421 1422 1423

  // Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
  var wantForeground = {
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: "",
      parameters: {
        "ohos.aafwk.param.callAbilityToForeground": true
W
wusongqing 已提交
1424
      }
W
wusongqing 已提交
1425
  };
G
Gloria 已提交
1426 1427 1428 1429 1430

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // Carry out normal service processing.
W
wusongqing 已提交
1431
        caller = obj;
G
Gloria 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // Process service logic errors.
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
W
wusongqing 已提交
1443 1444
  ```

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
## AbilityContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void;

Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1461
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
1462
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1463 1464
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
1465 1466 1467 1468 1469 1470 1471
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1472 1473
**Example**

G
Gloria 已提交
1474
  ```ts
1475
  var want = {
G
Gloria 已提交
1476 1477 1478
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1479 1480
  };
  var accountId = 100;
G
Gloria 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

  try {
    this.context.startAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
  ```


## AbilityContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;

Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1517
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
1518
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
G
Gloria 已提交
1519
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
1520 1521
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

G
Gloria 已提交
1522 1523 1524 1525 1526 1527 1528
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|

1529 1530
**Example**

G
Gloria 已提交
1531
  ```ts
1532
  var want = {
G
Gloria 已提交
1533 1534 1535
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1536 1537 1538
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
1539
    windowMode: 0
1540
  };
G
Gloria 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // Process service logic errors.
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // Carry out normal service processing.
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
  ```


## AbilityContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\>;

Starts an ability with the account ID specified. This API uses a promise to return the result.

**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1577
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
1578
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
G
Gloria 已提交
1579 1580 1581 1582 1583 1584 1585 1586
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|

**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
1587 1588 1589

**Example**

G
Gloria 已提交
1590
  ```ts
1591
  var want = {
G
Gloria 已提交
1592 1593 1594
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1595 1596 1597
  };
  var accountId = 100;
  var options = {
G
Gloria 已提交
1598
    windowMode: 0
1599
  };
G
Gloria 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // Carry out normal service processing.
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // Process service logic errors.
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // Process input parameter errors.
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1617
  ```
W
wusongqing 已提交
1618

W
wusongqing 已提交
1619
## AbilityContext.requestPermissionsFromUser
W
wusongqing 已提交
1620 1621 1622

requestPermissionsFromUser(permissions: Array&lt;string&gt;, requestCallback: AsyncCallback&lt;PermissionRequestResult&gt;) : void;

1623
Requests permissions from the user by displaying a dialog box. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1624

W
wusongqing 已提交
1625
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1626

W
wusongqing 已提交
1627
**Parameters**
W
wusongqing 已提交
1628

W
wusongqing 已提交
1629 1630 1631
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.|
1632
| callback | AsyncCallback&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
1633

W
wusongqing 已提交
1634
**Example**
1635

G
Gloria 已提交
1636
  ```ts
W
wusongqing 已提交
1637 1638 1639
       var permissions=['com.example.permission']
       this.context.requestPermissionsFromUser(permissions,(result) => {
       console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
W
wusongqing 已提交
1640
  });
1641

W
wusongqing 已提交
1642 1643 1644
  ```


W
wusongqing 已提交
1645
## AbilityContext.requestPermissionsFromUser
W
wusongqing 已提交
1646 1647 1648

requestPermissionsFromUser(permissions: Array&lt;string&gt;) : Promise&lt;PermissionRequestResult&gt;;

1649
Requests permissions from the user by displaying a dialog box. This API uses a promise to return the result.
W
wusongqing 已提交
1650

W
wusongqing 已提交
1651
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1652

W
wusongqing 已提交
1653
**Parameters**
W
wusongqing 已提交
1654

W
wusongqing 已提交
1655 1656 1657
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.|
W
wusongqing 已提交
1658 1659

**Return value**
W
wusongqing 已提交
1660

W
wusongqing 已提交
1661 1662
| Type| Description|
| -------- | -------- |
1663
| Promise&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | Promise used to return the result.|
W
wusongqing 已提交
1664

W
wusongqing 已提交
1665
**Example**
1666

G
Gloria 已提交
1667
  ```ts
W
wusongqing 已提交
1668 1669
   var permissions=['com.example.permission']
       this.context.requestPermissionsFromUser(permissions).then((data) => {
W
wusongqing 已提交
1670
      console.log('success:' + JSON.stringify(data));
W
wusongqing 已提交
1671
  }).catch((error) => {
W
wusongqing 已提交
1672
      console.log('failed:' + JSON.stringify(error));
W
wusongqing 已提交
1673
  });
W
wusongqing 已提交
1674

W
wusongqing 已提交
1675 1676 1677
  ```


W
wusongqing 已提交
1678
## AbilityContext.setMissionLabel
W
wusongqing 已提交
1679 1680 1681

setMissionLabel(label: string, callback:AsyncCallback&lt;void&gt;): void;

1682
Sets a label for this ability in the mission. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1683

W
wusongqing 已提交
1684
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1685

W
wusongqing 已提交
1686
**Parameters**
W
wusongqing 已提交
1687

W
wusongqing 已提交
1688 1689 1690
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|
1691
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
1692

W
wusongqing 已提交
1693
**Example**
1694

G
Gloria 已提交
1695
  ```ts
W
wusongqing 已提交
1696
  this.context.setMissionLabel("test",(result) => {
W
wusongqing 已提交
1697
      console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
W
wusongqing 已提交
1698 1699 1700 1701
  });
  ```


W
wusongqing 已提交
1702
## AbilityContext.setMissionLabel
W
wusongqing 已提交
1703

G
Gloria 已提交
1704
setMissionLabel(label: string): Promise&lt;void&gt;;
W
wusongqing 已提交
1705

1706
Sets a label for this ability in the mission. This API uses a promise to return the result.
W
wusongqing 已提交
1707

W
wusongqing 已提交
1708
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1709

W
wusongqing 已提交
1710
**Parameters**
W
wusongqing 已提交
1711

W
wusongqing 已提交
1712 1713 1714
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|
W
wusongqing 已提交
1715

W
wusongqing 已提交
1716 1717
**Return value**

W
wusongqing 已提交
1718 1719
| Type| Description|
| -------- | -------- |
1720
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1721

W
wusongqing 已提交
1722
**Example**
1723

G
Gloria 已提交
1724
  ```ts
1725 1726
  this.context.setMissionLabel("test").then(() => {
      console.log('success');
W
wusongqing 已提交
1727
  }).catch((error) => {
W
wusongqing 已提交
1728
      console.log('failed:' + JSON.stringify(error));
W
wusongqing 已提交
1729 1730
  });
  ```
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
## AbilityContext.setMissionIcon

setMissionIcon(icon: image.PixelMap, callback:AsyncCallback\<void>): void;

Sets an icon for this ability in the mission. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | Yes| Icon of the ability to set.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**
1749

G
Gloria 已提交
1750
  ```ts
G
Gloria 已提交
1751
    import image from '@ohos.multimedia.image';
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
    var imagePixelMap;
    var color = new ArrayBuffer(0);
    var initializationOptions = {
       size: {
           height: 100,
           width: 100
       }
    };
    image.createPixelMap(color, initializationOptions)
       .then((data) => {
           imagePixelMap = data;
       })
       .catch((err) => {
           console.log('--------- createPixelMap fail, err: ---------', err)
       });
    this.context.setMissionIcon(imagePixelMap, (err) => {
       console.log('---------- setMissionIcon fail, err: -----------', err);
    })
  ```


## AbilityContext.setMissionIcon

setMissionIcon(icon: image.PixelMap): Promise\<void>;

Sets an icon for this ability in the mission. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | Yes| Icon of the ability to set.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**
1796

G
Gloria 已提交
1797
  ```ts
G
Gloria 已提交
1798
    import image from '@ohos.multimedia.image';
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
    var imagePixelMap;
    var color = new ArrayBuffer(0);
    var initializationOptions = {
      size: {
          height: 100,
          width: 100
      }
    };
    image.createPixelMap(color, initializationOptions)
      .then((data) => {
          imagePixelMap = data;
      })
      .catch((err) => {
          console.log('--------- createPixelMap fail, err: ---------', err)
      });
    this.context.setMissionIcon(imagePixelMap)
1815 1816
      .then(() => {
          console.log('-------------- setMissionIcon success -------------');
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
      })
      .catch((err) => {
          console.log('-------------- setMissionIcon fail, err: -------------', err);
      });
  ```
## AbilityContext.restoreWindowStage

restoreWindowStage(localStorage: LocalStorage) : void;

Restores the window stage data for this ability.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
1834
| localStorage | LocalStorage | Yes| Storage used to store the restored window stage.|
1835 1836 1837

**Example**

G
Gloria 已提交
1838
  ```ts
1839 1840 1841
    var storage = new LocalStorage();
    this.context.restoreWindowStage(storage);
  ```
W
wusongqing 已提交
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854

## AbilityContext.isTerminating

isTerminating(): boolean;

Checks whether this ability is in the terminating state.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
1855
| boolean| The value **true** means that the ability is in the terminating state, and **false** means the opposite.|
W
wusongqing 已提交
1856 1857 1858

**Example**

G
Gloria 已提交
1859
  ```ts
W
wusongqing 已提交
1860 1861 1862
  var isTerminating = this.context.isTerminating();
  console.log('ability state :' + isTerminating);
  ```