• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.ability.featureAbility (FeatureAbility)
2
3The **FeatureAbility** module provides APIs that enable user interaction. You can use the APIs to start or terminate an ability, obtain a **dataAbilityHelper** object, obtain the window corresponding to the current ability, and connect to or disconnect from a ServiceAbility.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8> The APIs of this module can be used only in the FA model. In the stage model, use the APIs provided by the [UIAbility](js-apis-app-ability-uiAbility.md) and [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md) modules instead.
9
10## Constraints
11
12The APIs of the **FeatureAbility** module can be called only by PageAbilities.
13
14## Modules to Import
15
16```ts
17import featureAbility from '@ohos.ability.featureAbility';
18```
19
20## featureAbility.startAbility
21
22startAbility(parameter: StartAbilityParameter, callback: AsyncCallback\<number>): void
23
24Starts an ability. This API uses an asynchronous callback to return the result.
25
26Observe the following when using this API:
27 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
28 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
29 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
30
31**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
32
33**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) instead.
34
35**Parameters**
36
37| Name       | Type                                      | Mandatory  | Description            |
38| --------- | ---------------------------------------- | ---- | -------------- |
39| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes   | Ability to start.|
40| callback  | AsyncCallback\<number>                   | Yes   | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is **0**; otherwise, **err** is a non-zero value.     |
41
42**Example**
43
44```ts
45import featureAbility from '@ohos.ability.featureAbility';
46import wantConstant from '@ohos.app.ability.wantConstant';
47featureAbility.startAbility(
48    {
49        want:
50        {
51            action: '',
52            entities: [''],
53            type: '',
54            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
55            deviceId: '',
56            bundleName: 'com.example.myapplication',
57            /* In the FA model, abilityName consists of package and ability names. */
58            abilityName: 'com.example.myapplication.secondAbility',
59            uri: ''
60        },
61    },
62    (error, data) => {
63        if (error && error.code !== 0) {
64            console.error(`startAbility fail, error: ${JSON.stringify(error)}`);
65        } else {
66            console.log(`startAbility success, data: ${JSON.stringify(data)}`);
67        }
68    }
69);
70```
71
72
73
74## featureAbility.startAbility
75
76startAbility(parameter: StartAbilityParameter): Promise\<number>
77
78Starts an ability. This API uses a promise to return the result.
79
80Observe the following when using this API:
81 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
82 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
83 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
84
85**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
86
87**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.startAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability-2) instead.
88
89**Parameters**
90
91| Name       | Type                                      | Mandatory  | Description            |
92| --------- | ---------------------------------------- | ---- | -------------- |
93| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes   | Ability to start.|
94
95**Return value**
96
97| Type                                      | Description     |
98| ---------------------------------------- | ------- |
99| Promise\<number> | Promise used to return the result. If the operation is successful, **0** is returned; otherwise, a non-zero value is returned.|
100
101**Example**
102
103```ts
104import featureAbility from '@ohos.ability.featureAbility';
105import wantConstant from '@ohos.app.ability.wantConstant';
106featureAbility.startAbility(
107    {
108        want:
109        {
110            action: 'ohos.want.action.home',
111            entities: ['entity.system.home'],
112            type: 'MIMETYPE',
113            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
114            deviceId: '',
115            bundleName: 'com.example.myapplication',
116            /* In the FA model, abilityName consists of package and ability names. */
117            abilityName: 'com.example.myapplication.secondAbility',
118            uri: ''
119        },
120    }
121).then((data) => {
122    console.info(`startAbility data: ${JSON.stringify(data)}`);
123});
124```
125
126## featureAbility.acquireDataAbilityHelper<sup>7+</sup>
127
128acquireDataAbilityHelper(uri: string): DataAbilityHelper
129
130Obtains a **dataAbilityHelper** object.
131
132Observe the following when using this API:
133 - To access a DataAbility of another application, the target application must be configured with associated startup (**AssociateWakeUp** set to **true**).
134 - If an application running in the background needs to call this API to access a DataAbility, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. (Applications developed using the SDK of API version 8 or earlier are not restricted by this restriction when accessing the DataAbility.)
135 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
136 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
137
138**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
139
140**Note**: This API can be used only in the FA model. In the stage model, use [dataShare.createDataShareHelper](js-apis-data-dataShare.md#datasharecreatedatasharehelper) instead.
141
142**Parameters**
143
144| Name  | Type    | Mandatory  | Description          |
145| ---- | ------ | ---- | ------------ |
146| uri  | string | Yes   | URI of the file to open.|
147
148**Return value**
149
150| Type               | Description                             |
151| ----------------- | ------------------------------- |
152| [DataAbilityHelper](js-apis-inner-ability-dataAbilityHelper.md) | A utility class used to help other abilities access the Data ability.|
153
154**Example**
155
156```ts
157import featureAbility from '@ohos.ability.featureAbility';
158let dataAbilityHelper = featureAbility.acquireDataAbilityHelper(
159    'dataability:///com.example.DataAbility'
160);
161```
162
163## featureAbility.startAbilityForResult<sup>7+</sup>
164
165startAbilityForResult(parameter: StartAbilityParameter, callback: AsyncCallback\<AbilityResult>): void
166
167Starts an ability. This API uses an asynchronous callback to return the result when the ability is terminated. The following situations may be possible for a started ability:
168 - Normally, you can call [terminateSelfWithResult](#featureabilityterminateselfwithresult7) to terminate the ability. The result is returned to the caller.
169 - If an exception occurs, for example, the ability is killed, an exception message, in which **resultCode** is **-1**, is returned to the caller.
170 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#featureabilityterminateselfwithresult7) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others.
171
172Observe the following when using this API:
173 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
174 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
175 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
176
177**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
178
179**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.startAbilityForResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilityforresult) instead.
180
181
182**Parameters**
183
184| Name       | Type                                      | Mandatory  | Description            |
185| --------- | ---------------------------------------- | ---- | -------------- |
186| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes   | Ability to start.|
187| callback  | AsyncCallback\<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes   | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is an **AbilityResult** object; otherwise, err is an error object.     |
188
189**Example**
190
191```ts
192import featureAbility from '@ohos.ability.featureAbility';
193import wantConstant from '@ohos.app.ability.wantConstant';
194featureAbility.startAbilityForResult(
195   {
196        want:
197        {
198            action: 'ohos.want.action.home',
199            entities: ['entity.system.home'],
200            type: 'MIMETYPE',
201            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
202            deviceId: '',
203            bundleName: 'com.example.myapplication',
204            /* In the FA model, abilityName consists of package and ability names. */
205            abilityName: 'com.example.myapplication.secondAbility',
206            uri:''
207        },
208    },
209    (error, data) => {
210        if (error && error.code !== 0) {
211            console.error(`startAbilityForResult fail, error: ${JSON.stringify(error)}`);
212        } else {
213            console.log(`startAbilityForResult success, data: ${JSON.stringify(data)}`);
214        }
215    }
216);
217```
218
219## featureAbility.startAbilityForResult<sup>7+</sup>
220
221startAbilityForResult(parameter: StartAbilityParameter): Promise\<AbilityResult>
222
223Starts an ability. This API uses a promise to return the result when the ability is terminated. The following situations may be possible to an ability after it is started:
224 - Normally, you can call [terminateSelfWithResult](#featureabilityterminateselfwithresult7) to terminate the ability. The result is returned to the caller.
225 - If an exception occurs, for example, the ability is killed, an exception message, in which **resultCode** is **-1**, is returned to the caller.
226 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#featureabilityterminateselfwithresult7) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others.
227
228Observe the following when using this API:
229 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission.
230 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
231 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
232
233**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
234
235**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.startAbilityForResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartabilityforresult-2) instead.
236
237**Parameters**
238
239| Name       | Type                                      | Mandatory  | Description           |
240| --------- | ---------------------------------------- | ---- | ------------- |
241| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes   | Ability to start.|
242
243**Return value**
244
245| Type                                      | Description     |
246| ---------------------------------------- | ------- |
247| Promise\<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.|
248
249**Example**
250
251```ts
252import featureAbility from '@ohos.ability.featureAbility';
253import wantConstant from '@ohos.app.ability.wantConstant';
254featureAbility.startAbilityForResult(
255    {
256        want:
257        {
258            action: 'ohos.want.action.home',
259            entities: ['entity.system.home'],
260            type: 'MIMETYPE',
261            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
262            deviceId: '',
263            bundleName: 'com.example.myapplication',
264            /* In the FA model, abilityName consists of package and ability names. */
265            abilityName: 'com.example.myapplication.secondAbility',
266            uri:'',
267            parameters:
268            {
269                mykey0: 1111,
270                mykey1: [1, 2, 3],
271                mykey2: '[1, 2, 3]',
272                mykey3: 'xxxxxxxxxxxxxxxxxxxxxx',
273                mykey4: [1, 15],
274                mykey5: [false, true, false],
275                mykey6: ['aaaaaa', 'bbbbb', 'ccccccccccc'],
276                mykey7: true,
277            },
278        },
279    },
280).then((data) => {
281    console.info(`startAbilityForResult data: ${JSON.stringify(data)}`);
282});
283```
284
285## featureAbility.terminateSelfWithResult<sup>7+</sup>
286
287terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback\<void>): void
288
289Terminates this ability. If the ability is started by calling [startAbilityForResult](#featureabilitystartabilityforresult7), the result is returned to the caller in the form of a callback when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called.
290
291**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
292
293**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) instead.
294
295**Parameters**
296
297| Name       | Type                             | Mandatory  | Description            |
298| --------- | ------------------------------- | ---- | -------------- |
299| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes   | Result returned after the ability is terminated.|
300| callback  | AsyncCallback\<void>            | Yes   | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.     |
301
302**Example**
303
304```ts
305import featureAbility from '@ohos.ability.featureAbility';
306import wantConstant from '@ohos.app.ability.wantConstant';
307featureAbility.terminateSelfWithResult(
308    {
309        resultCode: 1,
310        want:
311        {
312            action: 'ohos.want.action.home',
313            entities: ['entity.system.home'],
314            type: 'MIMETYPE',
315            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
316            deviceId: '',
317            bundleName: 'com.example.myapplication',
318            /* In the FA model, abilityName consists of package and ability names. */
319            abilityName: 'com.example.myapplication.secondAbility',
320            uri:'',
321            parameters: {
322                mykey0: 2222,
323                mykey1: [1, 2, 3],
324                mykey2: '[1, 2, 3]',
325                mykey3: 'ssssssssssssssssssssssssss',
326                mykey4: [1, 15],
327                mykey5: [false, true, false],
328                mykey6: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
329                mykey7: true,
330            }
331        },
332    },
333    (error) => {
334        console.error(`error: ${JSON.stringify(error)}`);
335    }
336);
337```
338
339## featureAbility.terminateSelfWithResult<sup>7+</sup>
340
341terminateSelfWithResult(parameter: AbilityResult): Promise\<void>
342
343Terminates this ability. If the ability is started by calling [startAbilityForResult](#featureabilitystartabilityforresult7), the result is returned to the caller in the form of a promise when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called.
344
345**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
346
347**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult-1) instead.
348
349**Parameters**
350
351| Name       | Type                             | Mandatory  | Description           |
352| --------- | ------------------------------- | ---- | ------------- |
353| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes   | Result returned after the ability is terminated.|
354
355**Return value**
356
357| Type            | Description             |
358| -------------- | --------------- |
359| Promise\<void> | Promise that returns no value.|
360
361**Example**
362
363```ts
364import featureAbility from '@ohos.ability.featureAbility';
365import wantConstant from '@ohos.app.ability.wantConstant';
366featureAbility.terminateSelfWithResult(
367    {
368        resultCode: 1,
369        want:
370        {
371            action: 'ohos.want.action.home',
372            entities: ['entity.system.home'],
373            type: 'MIMETYPE',
374            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
375            deviceId: '',
376            bundleName: 'com.example.myapplication',
377            /* In the FA model, abilityName consists of package and ability names. */
378            abilityName: 'com.example.myapplication.secondAbility',
379            uri:'',
380            parameters: {
381                mykey0: 2222,
382                mykey1: [1, 2, 3],
383                mykey2: '[1, 2, 3]',
384                mykey3: 'ssssssssssssssssssssssssss',
385                mykey4: [1, 15],
386                mykey5: [false, true, false],
387                mykey6: ['qqqqq', 'wwwwww', 'aaaaaaaaaaaaaaaaa'],
388                mykey7: true,
389            }
390        },
391    }
392).then(() => {
393    console.info('==========================>terminateSelfWithResult=======================>');
394});
395```
396
397## featureAbility.hasWindowFocus<sup>7+<sup>
398
399hasWindowFocus(callback: AsyncCallback\<boolean>): void
400
401Checks whether the main window of this ability has the focus. This API uses an asynchronous callback to return the result.
402
403**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
404
405**Note**: This API can be used only in the FA model. In the stage model, use [on('windowEvent')](js-apis-window.md#onwindowevent10) instead.
406
407**Parameters**
408
409| Name      | Type                     | Mandatory  | Description                                      |
410| -------- | ----------------------- | ---- | ---------------------------------------- |
411| callback | AsyncCallback\<boolean> | Yes   | Callback used to return the result.<br>If the main window has the focus, **true** is returned. Otherwise, **false** is returned.|
412
413**Example**
414
415```ts
416import featureAbility from '@ohos.ability.featureAbility';
417featureAbility.hasWindowFocus((error, data) => {
418    if (error && error.code !== 0) {
419        console.error(`hasWindowFocus fail, error: ${JSON.stringify(error)}`);
420    } else {
421        console.log(`hasWindowFocus success, data: ${JSON.stringify(data)}`);
422    }
423});
424```
425
426## featureAbility.hasWindowFocus<sup>7+<sup>
427
428hasWindowFocus(): Promise\<boolean>
429
430Checks whether the main window of this ability has the focus. This API uses a promise to return the result.
431
432**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
433
434**Return value**
435
436| Type               | Description                                   |
437| ----------------- | ------------------------------------- |
438| Promise\<boolean> | Promise used to return the result. If the main window has the focus, **true** is returned. Otherwise, **false** is returned.|
439
440**Example**
441
442```ts
443import featureAbility from '@ohos.ability.featureAbility';
444featureAbility.hasWindowFocus().then((data) => {
445    console.info(`hasWindowFocus data: ${JSON.stringify(data)}`);
446});
447```
448
449## featureAbility.getWant
450
451getWant(callback: AsyncCallback\<Want>): void
452
453Obtains the Want corresponding to the ability to start. This API uses an asynchronous callback to return the result.
454
455**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
456
457**Note**: This API can be used only in the FA model. In the stage model, use [UIAbility.launchWant](js-apis-app-ability-uiAbility.md#attributes) instead.
458
459**Parameters**
460
461| Name      | Type                           | Mandatory  | Description       |
462| -------- | ----------------------------- | ---- | --------- |
463| callback | AsyncCallback\<[Want](js-apis-application-want.md)> | Yes   | Callback used to return the Want.|
464
465**Example**
466
467```ts
468import featureAbility from '@ohos.ability.featureAbility';
469featureAbility.getWant((error, data) => {
470    if (error && error.code !== 0) {
471        console.error(`getWant fail, error: ${JSON.stringify(error)}`);
472    } else {
473        console.log(`getWant success, data: ${JSON.stringify(data)}`);
474    }
475});
476```
477
478## featureAbility.getWant
479
480getWant(): Promise\<Want>
481
482Obtains the Want corresponding to the ability to start. This API uses a promise to return the result.
483
484**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
485
486**Note**: This API can be used only in the FA model. In the stage model, use [UIAbility.launchWant](js-apis-app-ability-uiAbility.md#attributes) instead.
487
488**Return value**
489
490| Type                     | Description              |
491| ----------------------- | ---------------- |
492| Promise\<[Want](js-apis-application-want.md)> | Promise used to return the Want.|
493
494**Example**
495
496```ts
497import featureAbility from '@ohos.ability.featureAbility';
498featureAbility.getWant().then((data) => {
499    console.info(`getWant data: ${JSON.stringify(data)}`);
500});
501```
502
503## featureAbility.getContext
504
505getContext(): Context
506
507Obtains the application context.
508
509**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
510
511**Note**: This API can be used only in the FA model. In the stage model, use [UIAbility.context](js-apis-app-ability-uiAbility.md#attributes) instead.
512
513**Return value**
514
515| Type     | Description        |
516| ------- | ---------- |
517| Context | Application context.|
518
519**Example**
520
521```ts
522import featureAbility from '@ohos.ability.featureAbility';
523let context = featureAbility.getContext();
524context.getBundleName((error, data) => {
525    if (error && error.code !== 0) {
526        console.error(`getBundleName fail, error: ${JSON.stringify(error)}`);
527    } else {
528        console.log(`getBundleName success, data: ${JSON.stringify(data)}`);
529    }
530});
531```
532
533## featureAbility.terminateSelf<sup>7+</sup>
534
535terminateSelf(callback: AsyncCallback\<void>): void
536
537Terminates this ability. This API uses an asynchronous callback to return the result.
538
539**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
540
541**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.terminateSelf](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself) instead.
542
543**Parameters**
544
545| Name      | Type                  | Mandatory  | Description      |
546| -------- | -------------------- | ---- | -------- |
547| callback | AsyncCallback\<void> | Yes   | Callback used to return the result.|
548
549**Example**
550
551```ts
552import featureAbility from '@ohos.ability.featureAbility';
553featureAbility.terminateSelf(
554    (error) => {
555        console.error(`error: ${JSON.stringify(error)}`);
556    }
557)
558```
559
560## featureAbility.terminateSelf<sup>7+</sup>
561
562terminateSelf(): Promise\<void>
563
564Terminates this ability. This API uses a promise to return the result.
565
566**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
567
568**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.terminateSelf](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateself-1) instead.
569
570**Return value**
571
572| Type            | Description              |
573| -------------- | ---------------- |
574| Promise\<void> | Promise that returns no value.|
575
576**Example**
577
578```ts
579import featureAbility from '@ohos.ability.featureAbility';
580featureAbility.terminateSelf().then(() => {
581    console.info('==========================>terminateSelf=======================>');
582});
583```
584
585## featureAbility.connectAbility<sup>7+</sup>
586
587connectAbility(request: Want, options:ConnectOptions): number
588
589Connects this ability to a ServiceAbility.
590
591Observe the following when using this API:
592 - To connect to a ServiceAbility of another application, the target application must be configured with associated startup (**AssociateWakeUp** set to **true**)..
593 - If an application running in the background needs to call this API to connect to a ServiceAbility, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. (Applications developed using the SDK of API version 8 or earlier are not restricted by this restriction when connecting to the ServiceAbility.)
594 - If **visible** of the target ability is **false** in cross-application scenarios, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission.
595 - For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md).
596
597**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
598
599**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.connectServiceExtensionAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextconnectserviceextensionability) instead.
600
601**Parameters**
602
603| Name     | Type            | Mandatory  | Description                   |
604| ------- | -------------- | ---- | --------------------- |
605| request | [Want](js-apis-application-want.md)  | Yes   | ServiceAbility to connect.|
606| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes   | Connection options.            |
607
608**Return value**
609
610| Type    | Description                  |
611| ------ | -------------------- |
612| number | ID of the connected ServiceAbility. The ID starts from 0 and is incremented by 1 each time a connection is set up.|
613
614**Example**
615
616```ts
617import rpc from '@ohos.rpc';
618import featureAbility from '@ohos.ability.featureAbility';
619
620let connectId = featureAbility.connectAbility(
621    {
622        deviceId: '',
623        bundleName: 'com.ix.ServiceAbility',
624        abilityName: 'com.ix.ServiceAbility.ServiceAbilityA',
625    },
626    {
627        onConnect: (element, remote) => {
628            console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`);
629        },
630        onDisconnect: (element) => {
631            console.log(`ConnectAbility onDisconnect element.deviceId : ${element.deviceId}`)
632        },
633        onFailed: (code) => {
634            console.error(`featureAbilityTest ConnectAbility onFailed errCode : ${code}`)
635        },
636    },
637);
638```
639
640## featureAbility.disconnectAbility<sup>7+</sup>
641
642disconnectAbility(connection: number, callback:AsyncCallback\<void>): void
643
644Disconnects this ability from a specific ServiceAbility. This API uses an asynchronous callback to return the result.
645
646**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
647
648**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.disconnectAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextdisconnectserviceextensionability-1) instead.
649
650**Parameters**
651
652| Name        | Type                  | Mandatory  | Description                     |
653| ---------- | -------------------- | ---- | ----------------------- |
654| connection | number               | Yes   | ID of the ServiceAbility to disconnect.|
655| callback   | AsyncCallback\<void> | Yes   | Callback used to return the result.               |
656
657**Example**
658
659```ts
660import rpc from '@ohos.rpc';
661import featureAbility from '@ohos.ability.featureAbility';
662
663let connectId = featureAbility.connectAbility(
664    {
665        bundleName: 'com.ix.ServiceAbility',
666        abilityName: 'com.ix.ServiceAbility.ServiceAbilityA',
667    },
668    {
669        onConnect: (element, remote) => {
670            console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`);
671        },
672        onDisconnect: (element) => {
673            console.log(`ConnectAbility onDisconnect element.deviceId : ${element.deviceId}`);
674        },
675        onFailed: (code) => {
676            console.error(`featureAbilityTest ConnectAbility onFailed errCode : ${code}`);
677        },
678    },
679);
680
681featureAbility.disconnectAbility(connectId, (error) => {
682    if (error && error.code !== 0) {
683        console.error(`disconnectAbility fail, connectId: ${connectId}, error: ${JSON.stringify(error)}`);
684    } else {
685        console.log(`disconnectAbility success, connectId: ${connectId}`);
686    }
687});
688```
689
690## featureAbility.disconnectAbility<sup>7+</sup>
691
692disconnectAbility(connection: number): Promise\<void>
693
694Disconnects this ability from a specific ServiceAbility. This API uses a promise to return the result.
695
696**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
697
698**Note**: This API can be used only in the FA model. In the stage model, use [UIAbilityContext.disconnectAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextdisconnectserviceextensionability) instead.
699
700**Parameters**
701
702| Name        | Type    | Mandatory  | Description                     |
703| ---------- | ------ | ---- | ----------------------- |
704| connection | number | Yes   | ID of the ServiceAbility to disconnect.|
705
706**Return value**
707
708| Type            | Description             |
709| -------------- | --------------- |
710| Promise\<void> | Promise that returns no value.|
711
712**Example**
713
714```ts
715import rpc from '@ohos.rpc';
716import featureAbility from '@ohos.ability.featureAbility';
717import { BusinessError } from '@ohos.base';
718
719let connectId = featureAbility.connectAbility(
720    {
721        bundleName: 'com.ix.ServiceAbility',
722        abilityName: 'com.ix.ServiceAbility.ServiceAbilityA',
723    },
724    {
725        onConnect: (element, remote) => {
726            console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`);
727        },
728        onDisconnect: (element) => {
729            console.log(`ConnectAbility onDisconnect element.deviceId : ${element.deviceId}`);
730        },
731        onFailed: (code) => {
732            console.error(`featureAbilityTest ConnectAbility onFailed errCode : ${code}`);
733        },
734    },
735);
736
737featureAbility.disconnectAbility(connectId).then(() => {
738    console.log('disconnectAbility success')
739}).catch((error: BusinessError)=>{
740    console.error(`featureAbilityTest result errCode : ${error.code}`);
741});
742```
743
744
745## featureAbility.getWindow<sup>7+</sup>
746
747getWindow(callback: AsyncCallback\<window.Window>): void
748
749Obtains the window corresponding to this ability. This API uses an asynchronous callback to return the result.
750
751**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
752
753**Note**: This API can be used only in the FA model. In the stage model, use [window.getLastWindow](js-apis-window.md#windowgetlastwindow9) instead.
754
755**Parameters**
756
757| Name    | Type                         | Mandatory| Description                         |
758| -------- | ----------------------------- | ---- | ----------------------------- |
759| callback | AsyncCallback\<[window.Window](js-apis-window.md#window)> | Yes  | Callback used to return the window.|
760
761**Example**
762
763```ts
764import featureAbility from '@ohos.ability.featureAbility';
765import { BusinessError } from '@ohos.base';
766import window from '@ohos.window';
767
768class FeatureAbilityClass {
769    onActive: () => void
770}
771
772let featureAbilityObj: FeatureAbilityClass =  {
773  onActive() {
774    console.info("onActive");
775    featureAbility.getWindow((error: BusinessError, data: window.Window) => {
776      if (error && error.code !== 0) {
777        console.error(`getWindow fail, error: ${JSON.stringify(error)}`);
778      } else {
779        console.log(`getWindow success, data: ${typeof(data)}`);
780      }
781    });
782  }
783}
784
785export default featureAbilityObj
786```
787
788## featureAbility.getWindow<sup>7+</sup>
789
790getWindow(): Promise\<window.Window>
791
792Obtains the window corresponding to this ability. This API uses a promise to return the result.
793
794**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
795
796**Note**: This API can be used only in the FA model. In the stage model, use [window.getLastWindow](js-apis-window.md#windowgetlastwindow9-1) instead.
797
798**Return value**
799
800| Type                   | Description                         |
801| ----------------------- | ----------------------------- |
802| Promise\<[window.Window](js-apis-window.md#window)> | Promise used to return the window.|
803
804**Example**
805
806```ts
807import featureAbility from '@ohos.ability.featureAbility';
808import { BusinessError } from '@ohos.base';
809import window from '@ohos.window';
810
811export default {
812
813  onActive() {
814    console.info("onActive");
815    featureAbility.getWindow().then((data: window.Window) => {
816        console.log(`getWindow success, data: ${typeof(data)}`);
817    }).catch((error: BusinessError)=>{
818        console.error(`getWindow fail, error: ${JSON.stringify(error)}`);
819    });
820  }
821}
822```
823
824## AbilityWindowConfiguration
825
826Defines the window configuration corresponding to this ability. The configuration is obtained through **featureAbility.AbilityWindowConfiguration**.
827
828**Example**
829
830```
831featureAbility.AbilityWindowConfiguration.WINDOW_MODE_UNDEFINED
832```
833
834**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
835
836| Name                                    | Value  | Description                                      |
837| ---------------------------------------- | ---- | ---------------------------------------- |
838| WINDOW_MODE_UNDEFINED<sup>7+</sup>       | 0    | The PageAbility is in an undefined window display mode.|
839| WINDOW_MODE_FULLSCREEN<sup>7+</sup>      | 1    | The PageAbility is in full screen mode.   |
840| WINDOW_MODE_SPLIT_PRIMARY<sup>7+</sup>   | 100  | The left screen in horizontal direction or the upper screen in vertical direction is the primary window.|
841| WINDOW_MODE_SPLIT_SECONDARY<sup>7+</sup> | 101  | The right screen in horizontal direction or the lower screen in vertical direction is the secondary window.|
842| WINDOW_MODE_FLOATING<sup>7+</sup>        | 102  | The PageAbility is displayed in floating window mode.|
843
844
845## AbilityStartSetting
846
847Defines the window attribute corresponding to this ability. The **abilityStartSetting** attribute is an object defined in the format of [**key: string]: any**, where **key** is an enumerated value of **AbilityStartSetting** and **value** is an enumerated value of **AbilityWindowConfiguration**.
848
849The value is obtained through **featureAbility.AbilityStartSetting**.
850
851**Example**
852
853```
854featureAbility.AbilityStartSetting.BOUNDS_KEY
855```
856
857**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
858
859| Name                          | Value             | Description                                      |
860| ---------------------------- | --------------- | ---------------------------------------- |
861| BOUNDS_KEY<sup>7+</sup>      | 'abilityBounds' | Ability window size.|
862| WINDOW_MODE_KEY<sup>7+</sup> | 'windowMode'    | Ability window display mode.|
863| DISPLAY_ID_KEY<sup>7+</sup>  | 'displayId'     | Display device ID.|
864
865## ErrorCode
866
867Enumerates the error codes that may be returned when an ability is started.
868
869**System capability**: SystemCapability.Ability.AbilityRuntime.Core
870
871| Name                            | Value   | Description                                      |
872| ------------------------------ | ---- | ---------------------------------------- |
873| NO_ERROR         | 0    | No error.  |
874| INVALID_PARAMETER | -1   | Invalid parameter.|
875| ABILITY_NOT_FOUND | -2   | The ability is not found.|
876| PERMISSION_DENY   | -3   | Permission denied.  |
877
878## DataAbilityOperationType
879
880Enumerates the operation types of a DataAbility. The DataAbility can use an enumerated value to specify the operation type when operating data in batches.
881
882**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
883
884| Name                      | Value   | Description                                      |
885| ------------------------ | ---- | ---------------------------------------- |
886| TYPE_INSERT<sup>7+</sup> | 1    | Insert operation.|
887| TYPE_UPDATE<sup>7+</sup> | 2    | Update operation.|
888| TYPE_DELETE<sup>7+</sup> | 3    | Deletion operation.|
889| TYPE_ASSERT<sup>7+</sup> | 4    | Assert operation.|
890