js-apis-ability-context.md 25.8 KB
Newer Older
W
wusongqing 已提交
1
# Ability Context
2

W
wusongqing 已提交
3 4
> **NOTE**
> The initial APIs of this module are supported since API version 9. API version 9 is a canary version for trial use. The APIs of this version may be unstable.
5 6 7

Implements the ability context. This module is inherited from **Context**.

W
wusongqing 已提交
8
# Modules to Import
9

W
wusongqing 已提交
10 11 12
```js
import Ability from '@ohos.application.Ability'
```
13

W
wusongqing 已提交
14
## Usage
15 16 17 18 19 20 21 22 23 24 25 26 27 28
Before using the **AbilityContext** module, you must define a child class that inherits from **Ability**.
```js
import Ability from '@ohos.application.Ability'
class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        let context = this.context;
    }
}
```

## Attributes

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

W
wusongqing 已提交
29 30 31 32 33
| Name                 | Type       | Readable   | Writable   | Description                                |
| --------------------- | --------------- | ------ | ------- | ----------------------------------- |
| config                | Configuration   | Yes    | No     | Configuration.                     |
| abilityInfo           | AbilityInfo   | Yes    | No     | Ability information.                 |
| currentHapModuleInfo  | HapModuleInfo   | Yes    | No     | Information about the current HAP.                   |
34 35


W
wusongqing 已提交
36
## startAbility
37 38 39

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

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

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

**Parameters**

W
wusongqing 已提交
46 47 48 49
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.| 
| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 
50 51 52

**Example**

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

W
wusongqing 已提交
64
## startAbility
65 66 67

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

W
wusongqing 已提交
68
Starts an ability with **options** specified. This API uses an asynchronous callback to return the result.
69 70 71 72 73

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

**Parameters**

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

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


W
wusongqing 已提交
97
## startAbility
98

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

W
wusongqing 已提交
101
Starts an ability with **options** specified. This API uses a promise to return the result.
102 103 104 105 106

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

**Parameters**

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

**Return value**

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

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

## startAbilityByCall

startAbilityByCall(want: Want): Promise<Caller>

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

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

**Parameters**
145

W
wusongqing 已提交
146 147 148
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the ability to start, including the ability name, bundle name, and device ID. If the device ID is left blank or the default value is used, the local ability will be started.| 
149

W
wusongqing 已提交
150
**Return value**
151

W
wusongqing 已提交
152 153 154
| Type| Description| 
| -------- | -------- |
| Promise<Caller> | Promise used to return the caller object to communicate with.| 
155

W
wusongqing 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
**Example**
    
```js
import Ability from '@ohos.application.Ability';
var caller;
export default class MainAbility extends Ability {
    onWindowStageCreate(windowStage) {
        this.context.startAbilityByCall({
            bundleName: "com.example.myservice",
            abilityName: "com.example.myservice.MainAbility",
            deviceId: ""
        }).then((obj) => {
            caller = obj;
            console.log('Caller GetCaller Get ' + call);
        }).catch((e) => {
            console.log('Caller GetCaller error ' + e);
        });
    }
}
```

## startAbilityWithAccount

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

W
wusongqing 已提交
181
Starts an ability with **accountId** specified. This API uses an asynchronous callback to return the result.
182 183 184 185 186

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

**Parameters**

W
wusongqing 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.| 
| accountId | number                   | Yes| Account ID.                  | 
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| 

**Example**

```js
var want = {
  "deviceId": "",
  "bundleName": "com.extreme.test",
  "abilityName": "com.extreme.test.MainAbility"
};
var accountId = 11;
this.context.startAbility(want, accountId, (error) => {
    console.log("error.code = " + error.code)
})
```

## startAbilityWithAccount

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

W
wusongqing 已提交
211 212 213 214 215 216 217 218 219 220 221 222
Starts an ability with **accountId** and **options** specified. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want)  | Yes| Information about the **Want** used for starting an ability.| 
| accountId | number                   | Yes| Account ID.                  | 
| options | StartOptions | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.| 
223 224

**Example**
W
wusongqing 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    
```js
var want = {
  "deviceId": "",
  "bundleName": "com.extreme.test",
  "abilityName": "com.extreme.test.MainAbility"
};
var options = {
  windowMode: 0,
};
var accountId = 11;
this.context.startAbility(want, accountId, options, (error) => {
    console.log("error.code = " + error.code)
})
```
240 241


W
wusongqing 已提交
242
## startAbilityWithAccount
243

W
wusongqing 已提交
244
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>
245

W
wusongqing 已提交
246
Starts an ability with **accountId** and **options** specified. This API uses a promise to return the result.
247 248 249 250 251

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

**Parameters**

W
wusongqing 已提交
252 253 254 255 256 257 258
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.| 
| accountId | number                   | Yes| Account ID.                  |
| options | StartOptions | No| Parameters used for starting the ability.|

**Return value**
259

W
wusongqing 已提交
260 261 262
| Type| Description| 
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| 
263 264

**Example**
W
wusongqing 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
```js
var want = {
  "deviceId": "",
  "bundleName": "com.extreme.test",
  "abilityName": "com.extreme.test.MainAbility"
};
var options = {
  windowMode: 0,
};
var accountId = 11;
this.context.startAbility(want, accountId, options)
.then((data) => {
    console.log('Operation successful.')
}).catch((error) => {
    console.log('Operation failed.');
})
```

## startAbilityForResult
284

W
wusongqing 已提交
285 286 287 288 289
startAbilityForResult(want: Want, callback: AsyncCallback&lt;AbilityResult&gt;): void

Starts an ability. This API uses an asynchronous callback to return the result when the ability is terminated.

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

W
wusongqing 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| callback | AsyncCallback&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult)&gt; | Yes| Callback used to return the result.|

**Example**

```js
this.context.startAbilityForResult(
    {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"},
    (error, result) => {
      console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
      console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
    }
);
```
309

W
wusongqing 已提交
310
## startAbilityForResult
311

W
wusongqing 已提交
312
startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback&lt;AbilityResult&gt;): void
313

W
wusongqing 已提交
314
Starts an ability with **options** specified. This API uses an asynchronous callback to return the result when the ability is terminated.
315 316 317 318 319

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

**Parameters**

W
wusongqing 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| options | StartOptions | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult)&gt; | Yes| Callback used to return the result.|

**Example**

```js
var options = {
  windowMode: 0,
};
this.context.startAbilityForResult(
    {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options,
    (error, result) => {
      console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
      console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
    }
);
```


## startAbilityForResult

startAbilityForResult(want: Want, options: StartOptions): Promise&lt;AbilityResult&gt;;

Starts an ability with **options** specified. This API uses a promise to return the result when the ability is terminated.

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

**Parameters**
351

W
wusongqing 已提交
352 353 354 355
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| options | StartOptions | Yes| Parameters used for starting the ability.|
356 357 358

**Return value**

W
wusongqing 已提交
359 360 361
| Type| Description|
| -------- | -------- |
| Promise&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult)&gt; | Promise used to return the result.|
362 363

**Example**
W
wusongqing 已提交
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
```js
var options = {
  windowMode: 0,
};
this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options).then((result) => {
    console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode)
}, (error) => {
    console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code)
})
```

## startAbilityForResultWithAccount

startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void

Starts an ability with **accountId** specified. This API uses an asynchronous callback to return the result when the ability is terminated.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| accountId | number                   | Yes| Account ID.                  |
| callback | AsyncCallback&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult)&gt; | Yes| Callback used to return the result.|
390

W
wusongqing 已提交
391
**Example**
392

W
wusongqing 已提交
393 394 395 396 397 398 399 400 401 402 403
```js
var accountId = 111;
this.context.startAbilityForResult(
    {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"},
    accountId, 
    (error, result) => {
      console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
      console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
    }
);
```
404

W
wusongqing 已提交
405
## startAbilityForResultWithAccount
406

W
wusongqing 已提交
407
startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void>): void
408

W
wusongqing 已提交
409
Starts an ability with **accountId** and **options** specified. This API uses an asynchronous callback to return the result when the ability is terminated.
410 411 412 413 414

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

**Parameters**

W
wusongqing 已提交
415 416 417 418 419 420
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| accountId | number                   | Yes| Account ID.                  |
| options | StartOptions | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
421 422 423

**Example**

W
wusongqing 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437
```js
var options = {
  windowMode: 0,
};
var accountId = 111;
this.context.startAbilityForResult(
    {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, 
    accountId, 
    options,
    () => {
      console.log("startAbilityForResult AsyncCallback is called")
    }
);
```
438

W
wusongqing 已提交
439
## startAbilityForResultWithAccount
440

W
wusongqing 已提交
441
startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult>;
442

W
wusongqing 已提交
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 488 489 490 491 492 493 494 495 496 497 498 499
Starts an ability with **accountId** and **options** specified. This API uses a promise to return the result when the ability is terminated.

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

**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| accountId | number                   | Yes| Account ID.                  |
| options | StartOptions | Yes| Parameters used for starting the ability.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult)&gt; | Promise used to return the result.|

**Example**

```js
var accountId = 111;
var options = {
  windowMode: 0,
};
this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, accountId, options).then((result) => {
    console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode)
}, (error) => {
    console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code)
})
```

## terminateSelf

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

Terminates this ability. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result indicating whether the API is successfully called.| 

**Example**

```js
this.context.terminateSelf((err) => {
    console.log('terminateSelf result:' + JSON.stringfy(err));
});
```

## terminateSelf

terminateSelf(): Promise&lt;void&gt;
500 501 502 503 504 505 506

Terminates this ability. This API uses a promise to return the result.

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

**Return value**

W
wusongqing 已提交
507 508 509
| Type| Description| 
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result indicating whether the API is successfully called.| 
510 511 512

**Example**

W
wusongqing 已提交
513 514 515 516 517 518 519
```js
this.context.terminateSelf(want).then((data) => {
    console.log('success:' + JSON.stringfy(data));
}).catch((error) => {
    console.log('failed:' + JSON.stringfy(error));
});
```
520

W
wusongqing 已提交
521
## terminateSelfWithResult
522

W
wusongqing 已提交
523
terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void
524

W
wusongqing 已提交
525
Terminates this ability. This API uses an asynchronous callback to return the information to the caller of **startAbilityForResult**.
526 527 528 529 530

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

**Parameters**

W
wusongqing 已提交
531 532 533 534
| 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.|
535 536 537

**Example**

W
wusongqing 已提交
538 539 540 541 542 543 544 545 546 547
```js
this.context.terminateSelfWithResult(
    {
        want: {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo"},
        resultCode: 100
    }, (error) => {
        console.log("terminateSelfWithResult is called = " + error.code)
    }
);
```
548 549


W
wusongqing 已提交
550
## terminateSelfWithResult
551

W
wusongqing 已提交
552
terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;
553 554 555 556 557

Terminates this ability. This API uses a promise to return information to the caller of **startAbilityForResult**.

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

W
wusongqing 已提交
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
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#AbilityResult) | Yes| Information returned to the caller.|

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

**Example**
```js
this.context.terminateSelfWithResult(
{
    want: {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo"},
    resultCode: 100
}).then((result) => {
    console.log("terminateSelfWithResult")
})
```

## 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

587 588
**Parameters**

W
wusongqing 已提交
589 590 591 592
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| options | ConnectOptions | Yes| Connection channel.|
593 594 595

**Return value**

W
wusongqing 已提交
596 597 598
| Type| Description| 
| -------- | -------- |
| number | ID of the connection between the two abilities.|
599 600

**Example**
W
wusongqing 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
```js
var want = {
  "deviceId": "",
  "bundleName": "com.extreme.test",
  "abilityName": "com.extreme.test.MainAbility"
}
var options = {
  onConnect: (elementName, remote) => {
    console.log('connectAbility onConnect, elementName: ' + elementName + ', remote: ' remote)
  },
  onDisconnect: (elementName) => {
    console.log('connectAbility onDisconnect, elementName: ' + elementName)
  },
  onFailed: (code) => {
    console.log('connectAbility onFailed, code: ' + code)
  }
}
this.context.connectAbility(want, options) {
  console.log('code: ' + code)
}
```

## connectAbilityWithAccount

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

Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to another ability based on an account.

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

W
wusongqing 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want) | Yes| Information about the **Want** used for starting an ability.|
| accountId | number | Yes| Account ID.|
| options | ConnectOptions | Yes| Connection channel.|

**Return value**

| Type| Description| 
| -------- | -------- |
| number | ID of the connection between the two abilities.|

**Example**
```js
var want = {
  "deviceId": "",
  "bundleName": "com.extreme.test",
  "abilityName": "com.extreme.test.MainAbility"
}
var accountId = 111;
var options = {
  onConnect: (elementName, remote) => {
    console.log('connectAbility onConnect, elementName: ' + elementName + ', remote: ' remote)
  },
  onDisconnect: (elementName) => {
    console.log('connectAbility onDisconnect, elementName: ' + elementName)
  },
  onFailed: (code) => {
    console.log('connectAbility onFailed, code: ' + code)
662
  }
W
wusongqing 已提交
663 664 665 666 667
}
this.context.connectAbility(want, accountId, options) {
  console.log('code: ' + code)
}
```
668

W
wusongqing 已提交
669
## disconnectAbility
670

W
wusongqing 已提交
671
disconnectAbility(connection: number, callback:AsyncCallback\<void>): void
672

W
wusongqing 已提交
673
Disconnects this ability from another ability. This API uses an asynchronous callback to return the result.
674

W
wusongqing 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| connection | number | Yes| ID of the connection to be disconnected.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Example**

```js
var connection = 111;
this.context.disconnectAbility(connection, () => {
  console.log('disconnection')
})
```

## disconnectAbility

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

Disconnects this ability from another ability. This API uses a promise to return the result.
698 699 700 701 702

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

**Parameters**

W
wusongqing 已提交
703 704 705
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| connection | number | Yes| ID of the connection to be disconnected.|
706 707 708

**Return value**

W
wusongqing 已提交
709 710 711
| Type| Description| 
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.| 
712 713 714

**Example**

W
wusongqing 已提交
715 716 717 718 719 720 721 722
```js
var connection = 111;
this.context.disconnectAbility(connection).then(() => {
  console.log('disconnect success')
}).catch((err) => {
  console.log('disconnect filed')
})
```
723

W
wusongqing 已提交
724
## setMissionLabel
725

W
wusongqing 已提交
726
setMissionLabel(label: string, callback:AsyncCallback&lt;void&gt;): void
727

W
wusongqing 已提交
728
Sets the label of the ability in the mission. This API uses an asynchronous callback to return the result.
729 730 731 732 733

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

**Parameters**

W
wusongqing 已提交
734 735 736 737
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.| 
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result indicating whether the API is successfully called.| 
738 739 740

**Example**
    
W
wusongqing 已提交
741 742 743 744 745
```js
this.context.setMissionLabel("test",(result) => {
    console.log('requestPermissionsFromUserresult:' + JSON.stringfy(result));
});
```
746 747


W
wusongqing 已提交
748
## setMissionLabel
749

W
wusongqing 已提交
750
setMissionLabel(label: string): Promise\<void>
751

W
wusongqing 已提交
752
Sets the label of the ability in the mission. This API uses a promise to return the result.
753 754 755 756 757

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

**Parameters**

W
wusongqing 已提交
758 759 760
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| label | string | Yes| Label of the ability to set.| 
761 762 763

**Return value**

W
wusongqing 已提交
764 765 766
| Type| Description| 
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result indicating whether the API is successfully called.| 
767 768 769

**Example**
    
W
wusongqing 已提交
770 771 772 773 774 775 776
```js
this.context.setMissionLabel("test").then((data) => {
    console.log('success:' + JSON.stringfy(data));
}).catch((error) => {
    console.log('failed:' + JSON.stringfy(error));
});
```
777

W
wusongqing 已提交
778
## requestPermissionsFromUser
779

W
wusongqing 已提交
780
requestPermissionsFromUser(permissions: Array&lt;string&gt;, requestCallback: AsyncCallback&lt;PermissionRequestResult&gt;) : void
781

W
wusongqing 已提交
782
Requests permissions from end users in the form of a dialog box. This API uses an asynchronous callback to return the result.
783 784 785 786 787

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

**Parameters**

W
wusongqing 已提交
788 789 790 791
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.| 
| callback | AsyncCallback&lt;PermissionRequestResult&gt; | Yes| Callback used to return the result indicating whether the API is successfully called.| 
792 793 794

**Example**
    
W
wusongqing 已提交
795 796 797 798 799
```js
this.context.requestPermissionsFromUser(permissions,(result) => {
    console.log('requestPermissionsFromUserresult:' + JSON.stringfy(result));
});
```
800 801


W
wusongqing 已提交
802
## requestPermissionsFromUser
803

W
wusongqing 已提交
804
requestPermissionsFromUser(permissions: Array&lt;string&gt;) : Promise&lt;PermissionRequestResult&gt;
805

W
wusongqing 已提交
806
Requests permissions from end users in the form of a dialog box. This API uses a promise to return the result.
807 808 809 810 811

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

**Parameters**

W
wusongqing 已提交
812 813 814
| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| permissions | Array&lt;string&gt; | Yes| Permissions to request.| 
815 816 817

**Return value**

W
wusongqing 已提交
818 819 820
| Type| Description| 
| -------- | -------- |
| Promise&lt;PermissionRequestResult&gt; | Promise used to return the result indicating whether the API is successfully called.| 
821 822 823

**Example**
    
W
wusongqing 已提交
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
```js
this.context.requestPermissionsFromUser(permissions).then((data) => {
    console.log('success:' + JSON.stringfy(data));
}).catch((error) => {
    console.log('failed:' + JSON.stringfy(error));
});
```

## restoreWindowStage

restoreWindowStage(contentStorage: ContentStorage) : void

Restores the window stage data during ability continuation.

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

**Parameters**

| Name| Type| Mandatory| Description| 
| -------- | -------- | -------- | -------- |
| contentStorage | ContentStorage | Yes| Window stage data to restore.| 

**Example**

```js
var contentStorage = {
  "link": 'link',
};
this.context.restoreWindowStage(contentStorage);
```