• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ConnectOptions
2
3**ConnectOptions** can be used as an input parameter to receive status changes during the connection to a background service. For example, it is used as an input parameter of [connectServiceExtensionAbility](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextconnectserviceextensionability) to connect to a ServiceExtensionAbility.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import common from '@ohos.app.ability.common';
13```
14
15## Attributes
16
17**System capability**: SystemCapability.Ability.AbilityRuntime.Core
18
19|    Name     | Type      | Mandatory  | Description                       |
20| ------------ | -------- | ---- | ------------------------- |
21| onConnect<sup>7+</sup>    | function | Yes   | Callback invoked when a connection is set up.     |
22| onDisconnect<sup>7+</sup> | function | Yes   | Callback invoked when a connection is interrupted.          |
23| onFailed<sup>7+</sup>     | function | Yes   | Callback invoked when a connection fails.|
24
25**Example**
26
27  ```ts
28  import common from '@ohos.app.ability.common';
29  import Want from '@ohos.app.ability.Want';
30
31  let want: Want = {
32    bundleName: 'com.example.myapp',
33    abilityName: 'MyAbility'
34  };
35
36  let connectOptions: common.ConnectOptions = {
37    onConnect(elementName, remote) {
38        console.log('onConnect elementName: ${elementName}');
39    },
40    onDisconnect(elementName) {
41        console.log('onDisconnect elementName: ${elementName}');
42    },
43    onFailed(code) {
44        console.error('onFailed code: ${code}');
45    }
46  };
47
48  let connection: number = this.context.connectAbility(want, connectOptions);
49  ```
50