js-apis-featureAbility.md 27.8 KB
Newer Older
W
wusongqing 已提交
1 2
# FeatureAbility Module (JavaScript)

W
wusongqing 已提交
3
## Constraints
W
wusongqing 已提交
4 5 6

APIs of the **FeatureAbility** module can be called only by Page abilities.

W
wusongqing 已提交
7
## Modules to Import
W
wusongqing 已提交
8 9 10 11 12

```
import featureAbility from '@ohos.ability.featureAbility'
```

W
wusongqing 已提交
13
## featureAbility.startAbility
W
wusongqing 已提交
14

W
wusongqing 已提交
15
startAbility(parameter: StartAbilityParameter, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
16

W
wusongqing 已提交
17
Starts an ability. This method uses a callback to return the result.
W
wusongqing 已提交
18

W
wusongqing 已提交
19
**Parameters**
W
wusongqing 已提交
20

W
wusongqing 已提交
21 22 23 24
| Name| Type| Mandatory| Description|
| --------- | --------------------- | ---- | ------------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
| callback  | AsyncCallback\<number>         | Yes| Callback used to return the result.|
W
wusongqing 已提交
25

W
wusongqing 已提交
26
**Example**
W
wusongqing 已提交
27 28 29

```javascript
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
30
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
31 32 33 34 35 36 37
featureAbility.startAbility(
        {
        want:
        {
            action: "",
            entities: [""],
            type: "",
W
wusongqing 已提交
38
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
39 40 41 42 43 44
            deviceId: "",
            bundleName: "com.example.startability",
            abilityName: "com.example.startability.MainAbility",
            uri: ""
        },
    },
W
wusongqing 已提交
45
);
W
wusongqing 已提交
46 47 48 49
```



W
wusongqing 已提交
50
## featureAbility.startAbility
W
wusongqing 已提交
51

W
wusongqing 已提交
52
startAbility(parameter: StartAbilityParameter): Promise\<number>
W
wusongqing 已提交
53

W
wusongqing 已提交
54
Starts an ability. This method uses a promise to return the result.
W
wusongqing 已提交
55

W
wusongqing 已提交
56
**Parameters**
W
wusongqing 已提交
57

W
wusongqing 已提交
58 59 60
| Name| Type| Mandatory| Description|
| --------- | ----------------------------------------------- | ---- | --------------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Example**
W
wusongqing 已提交
63 64 65

```javascript
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
66
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
67 68 69 70 71 72 73
featureAbility.startAbility(
    {
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
74 75
			flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
            deviceId: "",
W
wusongqing 已提交
76 77 78 79 80
            bundleName: "com.example.startability",
            abilityName: "com.example.startability.MainAbility",
            uri: ""
        },
    }
W
wusongqing 已提交
81
   ).then((data) => {
W
wusongqing 已提交
82 83 84 85
	console.info("==========================>startAbility=======================>");
});
```

W
wusongqing 已提交
86
## featureAbility.acquireDataAbilityHelper
W
wusongqing 已提交
87

W
wusongqing 已提交
88
acquireDataAbilityHelper(uri: string): DataAbilityHelper
W
wusongqing 已提交
89

W
wusongqing 已提交
90
Obtains a **dataAbilityHelper** object.
W
wusongqing 已提交
91

W
wusongqing 已提交
92
**Parameters**
W
wusongqing 已提交
93

W
wusongqing 已提交
94 95 96
| Name| Type| Mandatory| Description|
| ---- | ------ | ---- | ------------------------ |
| uri  | string | Yes| URI of the file to open.|
W
wusongqing 已提交
97

W
wusongqing 已提交
98
**Return value**
W
wusongqing 已提交
99 100

| Type| Description|
W
wusongqing 已提交
101
| ----------------- | -------------------------------------------- |
W
wusongqing 已提交
102 103
| DataAbilityHelper | A utility class used to help other abilities access the Data ability.|

W
wusongqing 已提交
104
**Example**
W
wusongqing 已提交
105 106 107 108 109 110 111 112

```javascript
import featureAbility from '@ohos.ability.featureAbility'
featureAbility.acquireDataAbilityHelper(
    "dataability:///com.exmaple.DataAbility"
)
```

W
wusongqing 已提交
113
## featureAbility.startAbilityForResult
W
wusongqing 已提交
114

W
wusongqing 已提交
115
startAbilityForResult(parameter: StartAbilityParameter, callback: AsyncCallback\<AbilityResult>): void
W
wusongqing 已提交
116

W
wusongqing 已提交
117
Starts an ability. This method uses a callback to return the execution result when the ability is destroyed.
W
wusongqing 已提交
118

W
wusongqing 已提交
119
**Parameters**
W
wusongqing 已提交
120

W
wusongqing 已提交
121 122 123 124
| Name| Type| Mandatory| Description|
| --------- | ----------------------------------------------- | ---- | --------------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
| callback  | AsyncCallback\<[AbilityResult](#abilityresult)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
125

W
wusongqing 已提交
126
**Example**
W
wusongqing 已提交
127 128 129

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
130
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
131 132 133 134 135 136 137
featureAbility.startAbilityForResult(
   {
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
138
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
139 140 141 142 143 144 145 146 147
            deviceId: "",
            bundleName: "com.example.featureabilitytest",
            abilityName: "com.example.featureabilitytest.MainAbility",
            uri:""
        },
    },
)
```

W
wusongqing 已提交
148
## featureAbility.startAbilityForResult
W
wusongqing 已提交
149

W
wusongqing 已提交
150
startAbilityForResult(parameter: StartAbilityParameter): Promise\<AbilityResult>
W
wusongqing 已提交
151

W
wusongqing 已提交
152
Starts an ability. This method uses a promise to return the execution result when the ability is destroyed.
W
wusongqing 已提交
153

W
wusongqing 已提交
154
**Parameters**
W
wusongqing 已提交
155

W
wusongqing 已提交
156 157 158
| Name| Type| Mandatory| Description|
| --------- | ----------------------------------------------- | ---- | ------------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
W
wusongqing 已提交
159

W
wusongqing 已提交
160 161 162 163
**Return value**
| Type| Description|
| ----------------------------------------- | -------------- |
| Promise\<[AbilityResult](#abilityresult)> | Promised returned with the execution result.|
W
wusongqing 已提交
164

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

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
169
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
170 171 172 173 174 175 176
featureAbility.startAbilityForResult(
    {
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
177
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            deviceId: "",
            bundleName: "com.example.featureabilitytest",
            abilityName: "com.example.featureabilitytest.MainAbility",
            uri:"",
            parameters:
            {
                mykey0: 1111,
                mykey1: [1, 2, 3],
                mykey2: "[1, 2, 3]",
                mykey3: "xxxxxxxxxxxxxxxxxxxxxx",
                mykey4: [1, 15],
                mykey5: [false, true, false],
                mykey6: ["aaaaaa", "bbbbb", "ccccccccccc"],
                mykey7: true,
            },
        },
        requestCode: 2,
    },
W
wusongqing 已提交
196
).then((data) => {
W
wusongqing 已提交
197 198 199 200
    console.info("==========================>startAbilityForResult=======================>");
});
```

W
wusongqing 已提交
201
## featureAbility.terminateSelfWithResult
W
wusongqing 已提交
202

W
wusongqing 已提交
203
terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback\<void>): void
W
wusongqing 已提交
204

W
wusongqing 已提交
205
Destroys this Page ability, with the result code and data sent to the caller. This method uses a callback to return the result.
W
wusongqing 已提交
206

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

W
wusongqing 已提交
209 210 211 212
| Name| Type| Mandatory| Description|
| --------- | ------------- | ---- | ------------------- |
| parameter | [AbilityResult](#abilityresult) | Yes| Ability to start.|
| callback  | AsyncCallback\<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
213

W
wusongqing 已提交
214
**Example**
W
wusongqing 已提交
215 216 217

```javascript
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
218
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
219 220 221 222 223 224 225 226
featureAbility.terminateSelfWithResult(
    {
        resultCode: 1,
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
227
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            deviceId: "",
            bundleName: "com.example.featureabilitytest",
            abilityName: "com.example.featureabilitytest.MainAbility",
            uri:"",
            parameters: {
                mykey0: 2222,
                mykey1: [1, 2, 3],
                mykey2: "[1, 2, 3]",
                mykey3: "ssssssssssssssssssssssssss",
                mykey4: [1, 15],
                mykey5: [false, true, false],
                mykey6: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
                mykey7: true,
            }
        },
    },
);
```

W
wusongqing 已提交
247
## featureAbility.terminateSelfWithResult
W
wusongqing 已提交
248

W
wusongqing 已提交
249
terminateSelfWithResult(parameter: AbilityResult): Promise\<void>
W
wusongqing 已提交
250

W
wusongqing 已提交
251
Destroys this Page ability, with the result code and data sent to the caller. This method uses a promise to return the result.
W
wusongqing 已提交
252

W
wusongqing 已提交
253
**Parameters**
W
wusongqing 已提交
254

W
wusongqing 已提交
255 256 257
| Name| Type| Mandatory| Description|
| --------- | ------------------------------- | ---- | ------------------- |
| parameter | [AbilityResult](#abilityresult) | Yes| Ability to start.|
W
wusongqing 已提交
258

W
wusongqing 已提交
259 260 261
**Return value**
| Type| Description|
| -------------- | ----------------------- |
W
wusongqing 已提交
262
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
263

W
wusongqing 已提交
264
**Example**
W
wusongqing 已提交
265 266 267

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
268
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
269 270 271 272 273 274 275 276
featureAbility.terminateSelfWithResult(
    {
        resultCode: 1,
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
277
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            deviceId: "",
            bundleName: "com.example.featureabilitytest",
            abilityName: "com.example.featureabilitytest.MainAbility",
            uri:"",
            parameters: {
                mykey0: 2222,
                mykey1: [1, 2, 3],
                mykey2: "[1, 2, 3]",
                mykey3: "ssssssssssssssssssssssssss",
                mykey4: [1, 15],
                mykey5: [false, true, false],
                mykey6: ["qqqqq", "wwwwww", "aaaaaaaaaaaaaaaaa"],
                mykey7: true,
            }
        },
    }
W
wusongqing 已提交
294
).then((data) => {
W
wusongqing 已提交
295 296 297 298 299 300
    console.info("==========================>terminateSelfWithResult=======================>");
});
```



W
wusongqing 已提交
301
## featureAbility.hasWindowFocus
W
wusongqing 已提交
302

W
wusongqing 已提交
303
hasWindowFocus(callback: AsyncCallback\<boolean>): void
W
wusongqing 已提交
304

W
wusongqing 已提交
305
Checks whether the main window of this ability has the focus. This method uses a callback to return the result.
W
wusongqing 已提交
306

W
wusongqing 已提交
307
**Parameters**
W
wusongqing 已提交
308

W
wusongqing 已提交
309 310 311
| Name| Type| Mandatory| Description|
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback\<boolean> | Yes| Callback used to return the result. <br>Returns **true** if the main window of this ability has the focus; returns **false** otherwise.|
W
wusongqing 已提交
312

W
wusongqing 已提交
313
**Example**
W
wusongqing 已提交
314 315 316 317 318 319 320 321

```javascript
import featureAbility from '@ohos.ability.featureability';
featureAbility.hasWindowFocus()
```



W
wusongqing 已提交
322
## featureAbility.hasWindowFocus
W
wusongqing 已提交
323

W
wusongqing 已提交
324
hasWindowFocus(): Promise\<boolean>
W
wusongqing 已提交
325

W
wusongqing 已提交
326
Checks whether the main window of this ability has the focus. This method uses a promise to return the result.
W
wusongqing 已提交
327

W
wusongqing 已提交
328
**Return value**
W
wusongqing 已提交
329

W
wusongqing 已提交
330 331 332
| Type| Description|
| ----------------- | ---------------------------------------------------------- |
| Promise\<boolean> | Returns **true** if the main window of this ability has the focus; returns **false** otherwise.|
W
wusongqing 已提交
333

W
wusongqing 已提交
334
**Example**
W
wusongqing 已提交
335 336 337

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
338
featureAbility.hasWindowFocus().then((data) => {
W
wusongqing 已提交
339 340 341 342 343 344
    console.info("==========================>hasWindowFocus=======================>");
});
```



W
wusongqing 已提交
345
## featureAbility.getWant
W
wusongqing 已提交
346

W
wusongqing 已提交
347
getWant(callback: AsyncCallback\<Want>): void
W
wusongqing 已提交
348

W
wusongqing 已提交
349
Obtains the **Want** object sent from this ability. This method uses a callback to return the result.
W
wusongqing 已提交
350

W
wusongqing 已提交
351
**Parameters**
W
wusongqing 已提交
352

W
wusongqing 已提交
353 354 355
| Name| Type| Mandatory| Description|
| -------- | ----------------------------- | ---- | ------------------ |
| callback | AsyncCallback\<[Want](#want)> | Yes| Callback used to return the result.|
W
wusongqing 已提交
356

W
wusongqing 已提交
357
**Example**
W
wusongqing 已提交
358 359 360 361 362 363 364 365

```javascript
import featureAbility from '@ohos.ability.featureability';
featureAbility.getWant()
```



W
wusongqing 已提交
366
## featureAbility.getWant
W
wusongqing 已提交
367

W
wusongqing 已提交
368
getWant(): Promise\<Want>
W
wusongqing 已提交
369

W
wusongqing 已提交
370
Obtains the **Want** object sent from this ability. This method uses a promise to return the result.
W
wusongqing 已提交
371

W
wusongqing 已提交
372 373 374
**Return value**
| Type| Description|
| ----------------------- | ------------------------- |
W
wusongqing 已提交
375
| Promise\<[Want](#want)> | Promise used to return the result.|
W
wusongqing 已提交
376

W
wusongqing 已提交
377
**Example**
W
wusongqing 已提交
378 379 380

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
381
featureAbility.getWant().then((data) => {
W
wusongqing 已提交
382 383 384 385
		console.info("==========================>getWantCallBack=======================>");
});
```

W
wusongqing 已提交
386
## featureAbility.getContext
W
wusongqing 已提交
387

W
wusongqing 已提交
388
getContext(): Context
W
wusongqing 已提交
389

W
wusongqing 已提交
390
Obtains the application context.
W
wusongqing 已提交
391

W
wusongqing 已提交
392 393 394 395
**Return value**
| Type| Description|
| ------- | -------------------- |
| Context | Application context returned.|
W
wusongqing 已提交
396

W
wusongqing 已提交
397
**Example**
W
wusongqing 已提交
398 399 400 401 402 403 404 405 406

```javascript
import featureAbility from '@ohos.ability.featureability';
var context = featureAbility.getContext()
context.getBundleName()
```



W
wusongqing 已提交
407
## featureAbility.terminateSelf
W
wusongqing 已提交
408

W
wusongqing 已提交
409
terminateSelf(callback: AsyncCallback\<void>): void
W
wusongqing 已提交
410

W
wusongqing 已提交
411
Destroys this Page ability, with the result code and data sent to the caller. This method uses a callback to return the result.
W
wusongqing 已提交
412

W
wusongqing 已提交
413
**Parameters**
W
wusongqing 已提交
414

W
wusongqing 已提交
415 416 417
| Name| Type| Mandatory| Description|
| -------- | -------------------- | ---- | ---------------- |
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
418

W
wusongqing 已提交
419
**Example**
W
wusongqing 已提交
420 421 422 423 424 425 426 427

```javascript
import featureAbility from '@ohos.ability.featureability';
featureAbility.terminateSelf()
```



W
wusongqing 已提交
428
## featureAbility.terminateSelf
W
wusongqing 已提交
429

W
wusongqing 已提交
430
terminateSelf(): Promise\<void>
W
wusongqing 已提交
431

W
wusongqing 已提交
432
Destroys this Page ability, with the result code and data sent to the caller. This method uses a promise to return the result.
W
wusongqing 已提交
433

W
wusongqing 已提交
434 435 436
**Return value**
| Type| Description|
| -------------- | ------------------------- |
W
wusongqing 已提交
437
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
438

W
wusongqing 已提交
439
**Example**
W
wusongqing 已提交
440 441 442

```javascript
import featureAbility from '@ohos.ability.featureability';
W
wusongqing 已提交
443
featureAbility.terminateSelf().then((data) => {		    console.info("==========================>terminateSelfCallBack=======================>");
W
wusongqing 已提交
444 445 446
});
```

W
wusongqing 已提交
447
## featureAbility.connectAbility
W
wusongqing 已提交
448

W
wusongqing 已提交
449
connectAbility(request: Want, options:ConnectOptions): number
W
wusongqing 已提交
450

W
wusongqing 已提交
451
Connects this ability to a specific Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
452

W
wusongqing 已提交
453
**Parameters**
W
wusongqing 已提交
454

W
wusongqing 已提交
455 456 457 458
| Name| Type| Mandatory| Description|
| ------- | -------------- | ---- | ---------------------------- |
| request | [Want](#want)           | Yes| Service ability to connect.|
| options | ConnectOptions | Yes| Callback used to return the result.|
W
wusongqing 已提交
459

W
wusongqing 已提交
460
Want
W
wusongqing 已提交
461 462 463 464 465 466 467

| Name| Readable/Writable| Type| Mandatory| Description|
| ------------ | -------- | -------- | ---- | ----------------------------------                               |
| deviceId     | Read-only| string   | No| Device ID of the Service ability to connect. The default value is the local device ID.|
| bundleName   | Read-only| string   | Yes| Bundle name of the Service ability to connect.|
| abilityName  | Read-only| string   | Yes| Class name of the Service ability to connect.|

W
wusongqing 已提交
468
ConnectOptions
W
wusongqing 已提交
469 470 471 472 473 474 475

| Name| Readable/Writable| Type| Mandatory| Description|
| ------------ | -------- | -------- | ---- | ---------------------------------- |
| onConnect    | Read-only| function | Yes| Callback invoked when the connection is successful.|
| onDisconnect | Read-only| function | Yes| Callback invoked when the connection fails.|
| onFailed     | Read-only| function | Yes| Callback invoked when **connectAbility** fails to be called.|

W
wusongqing 已提交
476 477 478 479
**Return value**
| Type| Description|
| ------ | ------------------------ |
| number | Returns the ID of the Service ability connected.|
W
wusongqing 已提交
480

W
wusongqing 已提交
481
**Example**
W
wusongqing 已提交
482 483

```javascript
W
wusongqing 已提交
484
import rpc from '@ohos.rpc'
W
wusongqing 已提交
485 486 487 488 489 490 491 492 493 494 495 496
import featureAbility from '@ohos.ability.featureAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('featureAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = featureAbility.connectAbility(
    {
W
wusongqing 已提交
497
        deviceId: "",
W
wusongqing 已提交
498 499 500 501 502 503 504 505 506 507 508
        bundleName: "com.ix.ServiceAbility",
        abilityName: "ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
```

W
wusongqing 已提交
509
## featureAbility.disconnectAbility
W
wusongqing 已提交
510

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

W
wusongqing 已提交
513
Disconnects this ability from a specific Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
514

W
wusongqing 已提交
515
**Parameters**
W
wusongqing 已提交
516

W
wusongqing 已提交
517 518 519 520
| Name| Type| Mandatory| Description|
| ---------- | ------------- | ---- | ------------------------------ |
| connection | number        | Yes| ID of the Service ability to disconnect.|
| callback   | AsyncCallback\<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
521

W
wusongqing 已提交
522
**Example**
W
wusongqing 已提交
523 524

```javascript
W
wusongqing 已提交
525
import rpc from '@ohos.rpc'
W
wusongqing 已提交
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
import featureAbility from '@ohos.ability.featureAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('featureAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = featureAbility.connectAbility(
    {
        bundleName: "com.ix.ServiceAbility",
        abilityName: "ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
var result = featureAbility.disconnectAbility(connId,
    (error,data) => {
    console.log('featureAbilityTest DisConnectJsSameBundleName result errCode : ' + error.code + " data: " + data)
    },
);
```

W
wusongqing 已提交
554
## featureAbility.disconnectAbility
W
wusongqing 已提交
555

W
wusongqing 已提交
556
disconnectAbility(connection: number): Promise\<void>
W
wusongqing 已提交
557

W
wusongqing 已提交
558
Disconnects this ability from a specific Service ability. This method uses a promise to return the result.
W
wusongqing 已提交
559

W
wusongqing 已提交
560
**Parameters**
W
wusongqing 已提交
561

W
wusongqing 已提交
562 563 564 565 566 567 568
| Name| Type| Mandatory| Description|
| ---------- | ------ | ---- | ------------------------------ |
| connection | number | Yes| ID of the Service ability to disconnect.|

**Return value**
| Type| Description|
| -------------- | ----------------------- |
W
wusongqing 已提交
569
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
570

W
wusongqing 已提交
571
**Example**
W
wusongqing 已提交
572 573

```javascript
W
wusongqing 已提交
574
import rpc from '@ohos.rpc'
W
wusongqing 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
import featureAbility from '@ohos.ability.featureAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('featureAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = featureAbility.connectAbility(
    {
        bundleName: "com.ix.ServiceAbility",
        abilityName: "ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
var result = await featureAbility.disconnectAbility(connId);
```

W
wusongqing 已提交
599
## featureAbility.continueAbility
W
wusongqing 已提交
600

W
wusongqing 已提交
601
continueAbility(options: ContinueAbilityOptions, callback: AsyncCallback\<void>): void
W
wusongqing 已提交
602

W
wusongqing 已提交
603
Migrates an ability to another device. This method uses a callback to return the execution result.
W
wusongqing 已提交
604

W
wusongqing 已提交
605
**Parameters**
W
wusongqing 已提交
606

W
wusongqing 已提交
607 608 609 610
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | ------------------- |
| options  | ContinueAbilityOptions | Yes| Ability to migrate.|
| callback | AsyncCallback\<void>   | Yes| Callback used to return the result.|
W
wusongqing 已提交
611

W
wusongqing 已提交
612
ContinueAbilityOptions
W
wusongqing 已提交
613 614

| Name| Readable/Writable| Type| Mandatory| Description|
W
wusongqing 已提交
615
| ---------- | -------- | ------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
616 617 618
| deviceId   | Read-only| string  | Yes| Information about the ability to migrate.|
| reversible | Read-only| boolean | Yes| Whether migration back is supported. Currently, this feature is not supported. This parameter is reserved and can be set to **false**.|

W
wusongqing 已提交
619
**Example**
W
wusongqing 已提交
620 621

```javascript
W
wusongqing 已提交
622 623 624 625 626 627 628 629 630 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 662 663 664 665 666 667 668 669 670
import device from '@system.device'
import ability from '@ohos.ability.featureability'

var DEVICE_LIST_LOCALHOST
export default {
    data: {
        title: "",
        deviceId: '',
        deviceList: [],
        remoteDeviceModel : new RemoteDeviceModel()
    },
    onInit() {
        DEVICE_LIST_LOCALHOST = {
            id: 'localhost',
        };
        this.deviceList = [DEVICE_LIST_LOCALHOST];
    },
    onShow() {
        const core = Core.getInstance()
        const expectExtend = new ExpectExtend({
            'id': 'extend'
        })
        const reportExtend = new ReportExtend(file)
        core.addService('expect', expectExtend)
        core.addService('report', reportExtend)
        core.init()
        const configService = core.getDefaultService('config')
        configService.setConfig(this)
        core.execute()
    },
    onReady() {
    },
    GetNetWorkId(){
        let self = this;
        this.remoteDeviceModel.registerDeviceListCallback(() => {
            var list = [];
            list[0] = DEVICE_LIST_LOCALHOST;
            var deviceList = self.remoteDeviceModel.deviceList;
            for (var i = 0; i < deviceList.length; i++) {
                list[i] = {
                    id: deviceList[i].deviceId,
                };
            }
            self.deviceList = list;
        });
    },
    ShowNetWorkId(){
        this.deviceId = this.deviceList[0].id;
    },
W
wusongqing 已提交
671

W
wusongqing 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
    async ContinueAbility(){
        function ContinueAbilityCallback(err) {
            console.info("=============>ContinueAbilityCallback============>");
        }
        var continueAbilityOptions = {
            reversible: false,
            deviceId: this.deviceId,
        }
        //continueAbility callback
        await ability.continueAbility(continueAbilityOptions, ContinueAbilityCallback);
    },
}
```

## featureAbility.continueAbility

continueAbility(options: ContinueAbilityOptions): Promise\<void>;

Migrates an ability to another device. This method uses a promise to return the execution result.

**Parameters**

| Name| Type| Mandatory| Description|
| ------- | ---------------------- | ---- | ------------------- |
| options | ContinueAbilityOptions | Yes| Ability to migrate.|

ContinueAbilityOptions

| Name| Readable/Writable| Type| Mandatory| Description|
| ---------- | -------- | ------- | ---- | ------------------------------------------------------------ |
| deviceId   | Read-only| string  | Yes| Information about the ability to migrate.|
| reversible | Read-only| boolean | Yes| Whether migration back is supported. Currently, this feature is not supported. This parameter is reserved and can be set to **false**.|

**Example**

```javascript
import device from '@system.device'
import ability from '@ohos.ability.featureability'

var DEVICE_LIST_LOCALHOST
export default {
    data: {
        title: "",
        deviceId: '',
        deviceList: [],
        remoteDeviceModel : new RemoteDeviceModel()
    },
    onInit() {
        DEVICE_LIST_LOCALHOST = {
            id: 'localhost',
        };
        this.deviceList = [DEVICE_LIST_LOCALHOST];
    },
    onShow() {
        const core = Core.getInstance()
        const expectExtend = new ExpectExtend({
            'id': 'extend'
        })
        const reportExtend = new ReportExtend(file)
        core.addService('expect', expectExtend)
        core.addService('report', reportExtend)
        core.init()
        const configService = core.getDefaultService('config')
        configService.setConfig(this)
        core.execute()
    },
    onReady() {
    },
    GetNetWorkId(){
        let self = this;
        this.remoteDeviceModel.registerDeviceListCallback(() => {
            var list = [];
            list[0] = DEVICE_LIST_LOCALHOST;
            var deviceList = self.remoteDeviceModel.deviceList;
            for (var i = 0; i < deviceList.length; i++) {
                list[i] = {
                    id: deviceList[i].deviceId,
                };
            }
            self.deviceList = list;
        });
    },
    ShowNetWorkId(){
        this.deviceId = this.deviceList[0].id;
    },

    async ContinueAbility(){
        function ContinueAbilityCallback(err) {
            console.info("=============>ContinueAbilityCallback============>");
        }
        var continueAbilityOptions = {
            reversible: false,
            deviceId: this.deviceId,
        }
        //continueAbility promise
        await ability.continueAbility(continueAbilityOptions).then(() => {
            console.info("=======>continueAbilityCallback=========>");
        });
    },
W
wusongqing 已提交
771 772 773
}
```

W
wusongqing 已提交
774
## AbilityResult
W
wusongqing 已提交
775 776 777 778

| Name| Readable/Writable| Type| Mandatory| Description|
| ---------- | -------- | --------------------- | ---- | ------------------------------------------------------------ |
| resultCode | Read-only| number                | Yes| Result code returned after the ability is destroyed. The feature for defining error-specific result codes is coming soon.|
W
wusongqing 已提交
779
| want       | Read-only| [Want](#want) | No| Data returned after the ability is destroyed. You can define the data to be returned. This parameter can be **null**.|
W
wusongqing 已提交
780

W
wusongqing 已提交
781
## StartAbilityParameter
W
wusongqing 已提交
782 783

| Name| Readable/Writable| Type| Mandatory| Description|
W
wusongqing 已提交
784 785 786
| ------------------- | -------- | -------------------- | ---- | ------------------------------------------------------------ |
| want                | Read-only| [Want](#want)        | Yes| Information about the ability to start.|
| abilityStartSetting | Read-only| {[key: string]: any} | No| Special attribute of the ability to start. This attribute can be passed in the method call.|
W
wusongqing 已提交
787

W
wusongqing 已提交
788
## Want
W
wusongqing 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

| Name| Readable/Writable| Type| Mandatory| Description|
| ----------- | -------- | -------------------- | ---- | ------------------------------------------------------------ |
| deviceId    | Read-only| string               | No| ID of the device that runs the ability.|
| bundleName  | Read-only| string               | No| Bundle name of the ability to start. If both **bundleName** and **abilityName** are specified in a **Want** object, the **Want** object can directly match the specified ability.|
| abilityName | Read-only| string               | No| Name of the ability to start. If both **bundleName** and **abilityName** are specified in a **Want** object, the **Want** object can directly match the specified ability.|
| uri         | Read-only| string               | No| URI information to match. If **uri** is specified in a **Want** object, the **Want** object will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path**.|
| type        | Read-only| string               | No| MIME type, for example, text/plain or image/*.|
| flags       | Read-only| number               | No| How the **Want** object will be handled. By default, a number is passed. For details, see [flags](#flags).|
| action      | Read-only| string               | No| Action option.|
| parameters  | Read-only| {[key: string]: any} | No| List of parameters in a **Want** object.|
| entities    | Read-only| Array\<string>       | No| List of entities.|

## flags

| Name| Name| Description|
| ------------------------------------ | ---------- | ------------------------------------------------------------ |
W
wusongqing 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
| FLAG_AUTH_READ_URI_PERMISSION        | 0x00000001 | Indicates the permission to read the URI.|
| FLAG_AUTH_WRITE_URI_PERMISSION       | 0x00000002 | Indicates the permission to write the URI.|
| FLAG_ABILITY_FORWARD_RESULT          | 0x00000004 | Returns the result to the ability.|
| FLAG_ABILITY_CONTINUATION            | 0x00000008 | Indicates whether the ability on the local device can be migrated to a remote device.|
| FLAG_NOT_OHOS_COMPONENT              | 0x00000010 | Indicates that a component does not belong to OHOS.|
| FLAG_ABILITY_FORM_ENABLED            | 0x00000020 | Indicates whether to enable an ability.|
| FLAG_AUTH_PERSISTABLE_URI_PERMISSION | 0x00000040 | Indicates the permission to make the URI persistent.|
| FLAG_AUTH_PREFIX_URI_PERMISSION      | 0x00000080 | Indicates the permission to verify URIs by prefix matching.|
| FLAG_ABILITYSLICE_MULTI_DEVICE       | 0x00000100 | Supports cross-device startup in a distributed scheduler.|
| FLAG_START_FOREGROUND_ABILITY        | 0x00000200 | Indicates that the Service ability is started regardless of whether the host application has been started.|
| FLAG_ABILITY_CONTINUATION_REVERSIBLE | 0x00000400 | Indicates that the migration is reversible.|
| FLAG_INSTALL_ON_DEMAND               | 0x00000800 | Indicates that the specific ability will be installed if it has not been installed.|
| FLAG_INSTALL_WITH_BACKGROUND_MODE    | 0x80000000 | Indicates that the specific ability will be installed in the background if it has not been installed.|
| FLAG_ABILITY_CLEAR_MISSION           | 0x00008000 | Clears other operation missions. This flag can be set for the **Want** object passed to **ohos.app.Context#startAbility** and must be used together with **flag_ABILITY_NEW_MISSION**.|
| FLAG_ABILITY_NEW_MISSION             | 0x10000000 | Creates a mission on the historical mission stack.|
| FLAG_ABILITY_MISSION_TOP             | 0x20000000 | Starts the mission on the top of the existing mission stack; creates an ability instance if no mission exists.|