js-apis-ability-context.md 40.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 8 9 10
> **NOTE**
> 
> 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**.

W
wusongqing 已提交
16
```js
W
wusongqing 已提交
17 18 19 20 21 22 23 24
import Ability from '@ohos.application.Ability'
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
    }
}
```

W
wusongqing 已提交
25
## Attributes
W
wusongqing 已提交
26

W
wusongqing 已提交
27 28
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

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

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

startAbility(want: Want, callback: AsyncCallback<void>): void

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

W
wusongqing 已提交
41
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
42

W
wusongqing 已提交
43
**Parameters**
W
wusongqing 已提交
44

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

W
wusongqing 已提交
50 51 52
**Example**

  ```js
W
wusongqing 已提交
53 54 55
  var want = {
  	"deviceId": "",
  	"bundleName": "com.extreme.test",
W
wusongqing 已提交
56
  	"abilityName": "MainAbility"
W
wusongqing 已提交
57 58 59 60 61 62 63
  };
  this.context.startAbility(want, (error) => {
      console.log("error.code = " + error.code)
  })
  ```


W
wusongqing 已提交
64
## AbilityContext.startAbility
W
wusongqing 已提交
65

W
wusongqing 已提交
66 67
startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void

68
Starts an ability with start options specified. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
69

W
wusongqing 已提交
70
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
71

W
wusongqing 已提交
72
**Parameters**
W
wusongqing 已提交
73

W
wusongqing 已提交
74 75
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
76
| want | [Want](js-apis-application-Want.md)  | Yes| Information about the target ability.|
W
wusongqing 已提交
77
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
W
wusongqing 已提交
78
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
79

W
wusongqing 已提交
80
**Example**
W
wusongqing 已提交
81
    
W
wusongqing 已提交
82
  ```js
W
wusongqing 已提交
83 84 85
  var want = {
  	"deviceId": "",
  	"bundleName": "com.extreme.test",
W
wusongqing 已提交
86
  	"abilityName": "MainAbility"
W
wusongqing 已提交
87 88 89 90 91 92 93 94 95 96
  };
  var options = {
  	windowMode: 0,
  };
  this.context.startAbility(want, options, (error) => {
      console.log("error.code = " + error.code)
  })
  ```


W
wusongqing 已提交
97
## AbilityContext.startAbility
W
wusongqing 已提交
98

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

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

W
wusongqing 已提交
103
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
104

W
wusongqing 已提交
105
**Parameters**
W
wusongqing 已提交
106

W
wusongqing 已提交
107 108
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
109
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
W
wusongqing 已提交
110
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
111 112

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

W
wusongqing 已提交
114 115 116
| Type| Description|
| -------- | -------- |
| Promise<void> | Promise used to return the result.|
W
wusongqing 已提交
117

W
wusongqing 已提交
118 119 120
**Example**

  ```js
W
wusongqing 已提交
121 122 123
  var want = {
  	"deviceId": "",
  	"bundleName": "com.extreme.test",
W
wusongqing 已提交
124
  	"abilityName": "MainAbility"
W
wusongqing 已提交
125
  };
W
wusongqing 已提交
126 127 128 129
  var options = {
  	windowMode: 0,
  };
  this.context.startAbility(want, options)
W
wusongqing 已提交
130 131 132 133 134 135 136 137
  .then((data) => {
      console.log('Operation successful.')
  }).catch((error) => {
      console.log('Operation failed.');
  })
  ```


W
wusongqing 已提交
138
## AbilityContext.startAbilityForResult
W
wusongqing 已提交
139 140 141

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

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

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

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

W
wusongqing 已提交
148 149
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
150
| want |[Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
W
wusongqing 已提交
151
| callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
152 153


W
wusongqing 已提交
154 155 156
**Example**

  ```js
W
wusongqing 已提交
157
  this.context.startAbilityForResult(
W
wusongqing 已提交
158
      {deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"},
W
wusongqing 已提交
159 160 161 162 163 164 165
      (error, result) => {
          console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
          console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
       }
  );
  ```

W
wusongqing 已提交
166
## AbilityContext.startAbilityForResult
W
wusongqing 已提交
167 168 169

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

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

W
wusongqing 已提交
172
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
173

W
wusongqing 已提交
174
**Parameters**
W
wusongqing 已提交
175

W
wusongqing 已提交
176 177
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
178
| want |[Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
W
wusongqing 已提交
179
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
W
wusongqing 已提交
180
| callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
181 182


W
wusongqing 已提交
183 184 185
**Example**

  ```js
W
wusongqing 已提交
186 187 188 189
  var options = {
    windowMode: 0,
  };
  this.context.startAbilityForResult(
W
wusongqing 已提交
190
      {deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options,
W
wusongqing 已提交
191 192 193 194 195 196 197
      (error, result) => {
          console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
          console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
       }
  );
  ```

W
wusongqing 已提交
198

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

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

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

W
wusongqing 已提交
205
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
206

W
wusongqing 已提交
207
**Parameters**
W
wusongqing 已提交
208

W
wusongqing 已提交
209 210
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
211
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
W
wusongqing 已提交
212
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
W
wusongqing 已提交
213

W
wusongqing 已提交
214

W
wusongqing 已提交
215 216
**Return value**

W
wusongqing 已提交
217 218 219
| Type| Description|
| -------- | -------- |
| Promise<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Promise used to return the result.|
W
wusongqing 已提交
220

W
wusongqing 已提交
221 222 223
**Example**

  ```js
W
wusongqing 已提交
224 225 226
  var options = {
    windowMode: 0,
  };
W
wusongqing 已提交
227
  this.context.startAbilityForResult({deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options).then((result) => {
W
wusongqing 已提交
228 229 230 231 232 233
      console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode)
  }, (error) => {
      console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code)
  })
  ```

234 235
## AbilityContext.startAbilityForResultWithAccount

236
startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void;
237 238 239 240 241 242 243 244 245 246 247 248 249

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|
| -------- | -------- | -------- | -------- |
250 251
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| callback | AsyncCallback\<AbilityResult\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.startAbilityWithAccount(want, accountId, (err, data) => {
    console.log('---------- startAbilityWithAccount fail, err:  -----------', err);
    console.log('---------- startAbilityWithAccount success, data:  -----------', data);
  });
  ```


## 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|
| -------- | -------- | -------- | -------- |
286 287
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0,
  };
  this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => {
    console.log('---------- startAbilityForResultWithAccount fail, err:  -----------', err);
  });
  ```


309
## AbilityContext.startAbilityForResultWithAccount
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

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|
| -------- | -------- | -------- | -------- |
325 326
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
| 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.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0,
  };
  this.context.startAbilityForResultWithAccount(want, accountId, options)
    .then((data) => {
        console.log('---------- startAbilityForResultWithAccount success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- startAbilityForResultWithAccount fail, err:  -----------', err);
    })
  ```
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  this.context.startServiceExtensionAbility(want, (err) => {
    console.log('---------- startServiceExtensionAbility fail, err:  -----------', err);
  });
  ```

## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  this.context.startServiceExtensionAbility(want)
    .then((data) => {
        console.log('---------- startServiceExtensionAbility success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- startServiceExtensionAbility fail, err:  -----------', err);
    })
  ```
## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| 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.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.startServiceExtensionAbilityWithAccount(want,accountId, (err) => {
    console.log('---------- startServiceExtensionAbilityWithAccount fail, err:  -----------', err);
  });
  ```

## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.startServiceExtensionAbilityWithAccount(want,accountId)
    .then((data) => {
        console.log('---------- startServiceExtensionAbilityWithAccount success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- startServiceExtensionAbilityWithAccount fail, err:  -----------', err);
    })
  ```
## AbilityContext.stopServiceExtensionAbility
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  this.context.stopServiceExtensionAbility(want, (err) => {
    console.log('---------- stopServiceExtensionAbility fail, err:  -----------', err);
  });
  ```

## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  this.context.stopServiceExtensionAbility(want)
    .then((data) => {
        console.log('---------- stopServiceExtensionAbility success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- stopServiceExtensionAbility fail, err:  -----------', err);
    })
  ```

## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| 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.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.stopServiceExtensionAbilityWithAccount(want,accountId, (err) => {
    console.log('---------- stopServiceExtensionAbilityWithAccount fail, err:  -----------', err);
  });
  ```

## 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|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.stopServiceExtensionAbilityWithAccount(want,accountId)
    .then((data) => {
        console.log('---------- stopServiceExtensionAbilityWithAccount success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- stopServiceExtensionAbilityWithAccount fail, err:  -----------', err);
    })
  ```
W
wusongqing 已提交
620

W
wusongqing 已提交
621
## AbilityContext.terminateSelf
W
wusongqing 已提交
622 623 624

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

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

W
wusongqing 已提交
627
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
628

W
wusongqing 已提交
629
**Parameters**
W
wusongqing 已提交
630

W
wusongqing 已提交
631 632
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
633
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
634

W
wusongqing 已提交
635 636 637
**Example**

  ```js
W
wusongqing 已提交
638
  this.context.terminateSelf((err) => {
W
wusongqing 已提交
639
      console.log('terminateSelf result:' + JSON.stringify(err));
W
wusongqing 已提交
640
  });
W
wusongqing 已提交
641 642 643
  ```


W
wusongqing 已提交
644
## AbilityContext.terminateSelf
W
wusongqing 已提交
645 646 647

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

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

W
wusongqing 已提交
650
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
651

W
wusongqing 已提交
652
**Return value**
W
wusongqing 已提交
653

W
wusongqing 已提交
654 655
| Type| Description|
| -------- | -------- |
656
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
657

W
wusongqing 已提交
658 659 660
**Example**

  ```js
661
  this.context.terminateSelf().then((data) => {
W
wusongqing 已提交
662
      console.log('success:' + JSON.stringify(data));
W
wusongqing 已提交
663
  }).catch((error) => {
W
wusongqing 已提交
664
      console.log('failed:' + JSON.stringify(error));
W
wusongqing 已提交
665 666 667 668
  });
  ```


W
wusongqing 已提交
669
## AbilityContext.terminateSelfWithResult
W
wusongqing 已提交
670 671 672

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

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

W
wusongqing 已提交
675
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
676

W
wusongqing 已提交
677
**Parameters**
W
wusongqing 已提交
678

W
wusongqing 已提交
679 680 681 682
| 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 已提交
683

W
wusongqing 已提交
684 685 686
**Example**

  ```js
687 688 689 690 691 692 693 694 695 696 697
  var want = {
    "bundleName": "com.extreme.myapplication",
    "abilityName": "SecondAbility"
  }
  var resultCode = 100;
  // AbilityResult information returned to the caller.
  var abilityResult = {
    want,
    resultCode
  }
  this.context.terminateSelfWithResult(abilityResult, (error) => {
W
wusongqing 已提交
698 699 700 701 702 703
          console.log("terminateSelfWithResult is called = " + error.code)
      }
  );
  ```


W
wusongqing 已提交
704
## AbilityContext.terminateSelfWithResult
W
wusongqing 已提交
705 706 707

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

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

W
wusongqing 已提交
710
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
711

W
wusongqing 已提交
712
**Parameters**
W
wusongqing 已提交
713

W
wusongqing 已提交
714 715 716
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
W
wusongqing 已提交
717

W
wusongqing 已提交
718 719
**Return value**

W
wusongqing 已提交
720 721 722
| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
723

W
wusongqing 已提交
724 725 726
**Example**

  ```js
727 728 729 730 731 732 733 734 735 736 737
  var want = {
    "bundleName": "com.extreme.myapplication",
    "abilityName": "SecondAbility"
  }
  var resultCode = 100;
  // AbilityResult information returned to the caller.
  var abilityResult = {
    want,
    resultCode
  }
  this.context.terminateSelfWithResult(abilityResult).then((result) => {
W
wusongqing 已提交
738 739 740 741
      console.log("terminateSelfWithResult")
  }
  )
  ```
W
wusongqing 已提交
742

743 744 745 746 747 748 749 750 751 752 753 754 755 756
## AbilityContext.connectAbility

connectAbility(want: Want, options: ConnectOptions): number;

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

**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|
| -------- | -------- | -------- | -------- |
757 758
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | No| Parameters for the connection.|
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

**Return value**

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

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
  const result = this.context.connectAbility(want, options);
  console.log('----------- connectAbilityResult: ------------', result);
  ```


## AbilityContext.connectAbilityWithAccount

connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;

788
Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability with the account ID specified.
789 790 791 792 793 794 795 796 797 798 799

**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|
| -------- | -------- | -------- | -------- |
800 801
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | No| Parameters for the connection.|

**Return value**

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

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
  const result = this.context.connectAbilityWithAccount(want, accountId, options);
  console.log('----------- connectAbilityResult: ------------', result);
  ```

## AbilityContext.disconnectAbility

disconnectAbility(connection: number): Promise\<void>;

Disconnects a connection. 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|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Result code of the ability connection.|

**Return value**

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

**Example**
    
  ```js
  var connectionNumber = 0;
  this.context.disconnectAbility(connectionNumber).then((data) => {
       console.log('disconnectAbility success, data: ', data);
  }).catch((err) => {
       console.log('disconnectAbility fail, err: ', err);
  });
  ```

## AbilityContext.disconnectAbility

disconnectAbility(connection: number, callback:AsyncCallback\<void>): void;

Disconnects a connection. 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|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Result code of the ability connection.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**

  ```js
    var connectionNumber = 0;
    this.context.disconnectAbility(connectionNumber, (err) => {
       console.log('---------- disconnectAbility fail, err: -----------', err);
    });
  ```
W
wusongqing 已提交
886

W
wusongqing 已提交
887
## AbilityContext.startAbilityByCall
W
wusongqing 已提交
888 889 890 891 892

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

Obtains the caller interface of the specified ability, and if the specified ability is not started, starts the ability in the background.

W
wusongqing 已提交
893
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
894

W
wusongqing 已提交
895
**Parameters**
W
wusongqing 已提交
896

W
wusongqing 已提交
897 898
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
899
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability, including the ability name, bundle name, and device ID. If the device ID is left blank or the default value is used, it indicates the local device.|
W
wusongqing 已提交
900

W
wusongqing 已提交
901 902
**Return value**

W
wusongqing 已提交
903 904 905
| Type| Description|
| -------- | -------- |
| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
W
wusongqing 已提交
906

W
wusongqing 已提交
907
**Example**
W
wusongqing 已提交
908
    
W
wusongqing 已提交
909
  ```js
W
wusongqing 已提交
910 911 912 913 914 915
  import Ability from '@ohos.application.Ability';
  var caller;
  export default class MainAbility extends Ability {
      onWindowStageCreate(windowStage) {
          this.context.startAbilityByCall({
              bundleName: "com.example.myservice",
W
wusongqing 已提交
916
              abilityName: "MainAbility",
W
wusongqing 已提交
917 918 919
              deviceId: ""
          }).then((obj) => {
              caller = obj;
W
wusongqing 已提交
920
              console.log('Caller GetCaller Get ' + caller);
W
wusongqing 已提交
921 922 923 924 925 926 927
          }).catch((e) => {
              console.log('Caller GetCaller error ' + e);
          });
      }
  }
  ```

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
## 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|
| -------- | -------- | -------- | -------- |
944 945
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  this.context.startAbilityWithAccount(want, accountId, (err) => {
    console.log('---------- startAbilityWithAccount fail, err:  -----------', err);
  });
  ```


## 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|
| -------- | -------- | -------- | -------- |
979 980
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0,
  };
  this.context.startAbilityWithAccount(want, accountId, options, (err) => {
    console.log('---------- startAbilityWithAccount fail, err:  -----------', err);
  });
  ```


## 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|
| -------- | -------- | -------- | -------- |
1018 1019
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|

**Example**

  ```js
  var want = {
    "deviceId": "",
    "bundleName": "com.extreme.test",
    "abilityName": "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0,
  };
  this.context.startAbilityWithAccount(want, accountId, options)
    .then((data) => {
        console.log('---------- startAbilityWithAccount success, data:  -----------', data);
    })
    .catch((err) => {
        console.log('---------- startAbilityWithAccount fail, err:  -----------', err);
    })
  ```
W
wusongqing 已提交
1042

W
wusongqing 已提交
1043
## AbilityContext.requestPermissionsFromUser
W
wusongqing 已提交
1044 1045 1046

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

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

W
wusongqing 已提交
1049
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1050

W
wusongqing 已提交
1051
**Parameters**
W
wusongqing 已提交
1052

W
wusongqing 已提交
1053 1054 1055
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.|
1056
| callback | AsyncCallback&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
1057

W
wusongqing 已提交
1058
**Example**
W
wusongqing 已提交
1059
    
W
wusongqing 已提交
1060 1061 1062 1063
  ```js
       var permissions=['com.example.permission']
       this.context.requestPermissionsFromUser(permissions,(result) => {
       console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
W
wusongqing 已提交
1064
  });
W
wusongqing 已提交
1065
  
W
wusongqing 已提交
1066 1067 1068
  ```


W
wusongqing 已提交
1069
## AbilityContext.requestPermissionsFromUser
W
wusongqing 已提交
1070 1071 1072

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

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

W
wusongqing 已提交
1075
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1076

W
wusongqing 已提交
1077
**Parameters**
W
wusongqing 已提交
1078

W
wusongqing 已提交
1079 1080 1081
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.|
W
wusongqing 已提交
1082 1083

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

W
wusongqing 已提交
1085 1086
| Type| Description|
| -------- | -------- |
1087
| Promise&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | Promise used to return the result.|
W
wusongqing 已提交
1088

W
wusongqing 已提交
1089
**Example**
W
wusongqing 已提交
1090
    
W
wusongqing 已提交
1091 1092 1093
  ```js
   var permissions=['com.example.permission']
       this.context.requestPermissionsFromUser(permissions).then((data) => {
W
wusongqing 已提交
1094
      console.log('success:' + JSON.stringify(data));
W
wusongqing 已提交
1095
  }).catch((error) => {
W
wusongqing 已提交
1096
      console.log('failed:' + JSON.stringify(error));
W
wusongqing 已提交
1097
  });
W
wusongqing 已提交
1098

W
wusongqing 已提交
1099 1100 1101
  ```


W
wusongqing 已提交
1102
## AbilityContext.setMissionLabel
W
wusongqing 已提交
1103 1104 1105

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

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

W
wusongqing 已提交
1108
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1109

W
wusongqing 已提交
1110
**Parameters**
W
wusongqing 已提交
1111

W
wusongqing 已提交
1112 1113 1114
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|
1115
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
1116

W
wusongqing 已提交
1117
**Example**
W
wusongqing 已提交
1118
    
W
wusongqing 已提交
1119
  ```js
W
wusongqing 已提交
1120
  this.context.setMissionLabel("test",(result) => {
W
wusongqing 已提交
1121
      console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
W
wusongqing 已提交
1122 1123 1124 1125
  });
  ```


W
wusongqing 已提交
1126
## AbilityContext.setMissionLabel
W
wusongqing 已提交
1127 1128 1129

setMissionLabel(label: string): Promise&lt;void&gt;

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

W
wusongqing 已提交
1132
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
1133

W
wusongqing 已提交
1134
**Parameters**
W
wusongqing 已提交
1135

W
wusongqing 已提交
1136 1137 1138
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.|
W
wusongqing 已提交
1139

W
wusongqing 已提交
1140 1141
**Return value**

W
wusongqing 已提交
1142 1143
| Type| Description|
| -------- | -------- |
1144
| Promise&lt;void&gt; | Promise used to return the result.|
W
wusongqing 已提交
1145

W
wusongqing 已提交
1146
**Example**
W
wusongqing 已提交
1147
    
W
wusongqing 已提交
1148
  ```js
W
wusongqing 已提交
1149
  this.context.setMissionLabel("test").then((data) => {
W
wusongqing 已提交
1150
      console.log('success:' + JSON.stringify(data));
W
wusongqing 已提交
1151
  }).catch((error) => {
W
wusongqing 已提交
1152
      console.log('failed:' + JSON.stringify(error));
W
wusongqing 已提交
1153 1154
  });
  ```
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
## 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**
    
  ```js
    import image from '@ohos.multimedia.image'
    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**
    
  ```js
    import image from '@ohos.multimedia.image'
    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)
      .then((data) => {
          console.log('-------------- setMissionIcon success, data: -------------', data);
      })
      .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|
| -------- | -------- | -------- | -------- |
| localStorage | image.LocalStorage | Yes| Storage used to store the restored window stage.|

**Example**

  ```js
    var storage = new LocalStorage();
    this.context.restoreWindowStage(storage);
  ```
W
wusongqing 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278

## AbilityContext.isTerminating

isTerminating(): boolean;

Checks whether this ability is in the terminating state.

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

**Return value**

| Type| Description|
| -------- | -------- |
1279
| boolean| The value **true** means that the ability is in the terminating state, and **false** means the opposite.|
W
wusongqing 已提交
1280 1281 1282 1283 1284 1285 1286

**Example**

  ```js
  var isTerminating = this.context.isTerminating();
  console.log('ability state :' + isTerminating);
  ```