• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ServiceExtensionAbility
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
4> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
5
6
7Provides APIs related to **ServiceExtension**.
8
9
10## Modules to Import
11
12```
13import ServiceExtension from '@ohos.application.ServiceExtensionAbility';
14```
15
16
17## Required Permissions
18
19None.
20
21
22## Attributes
23
24**System capability**: SystemCapability.Ability.AbilityRuntime.Core
25
26| Name| Type| Readable| Writable| Description|
27| -------- | -------- | -------- | -------- | -------- |
28| context | [ServiceExtensionContext](js-apis-service-extension-context.md)  | Yes| No| Service extension context, which is inherited from **ExtensionContext**.|
29
30
31## ServiceExtensionAbility.onCreate
32
33onCreate(want: Want): void;
34
35Called when an extension is created to initialize the service logic.
36
37**System capability**: SystemCapability.Ability.AbilityRuntime.Core
38
39**Parameters**
40
41  | Name| Type| Mandatory| Description|
42  | -------- | -------- | -------- | -------- |
43  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
44
45**Example**
46
47  ```js
48  class ServiceExt extends ServiceExtension {
49    onCreate(want) {
50      console.log('onCreate, want:' + want.abilityName);
51    }
52  }
53  ```
54
55
56## ServiceExtensionAbility.onDestroy
57
58onDestroy(): void;
59
60Called when this extension is destroyed to clear resources.
61
62**System capability**: SystemCapability.Ability.AbilityRuntime.Core
63
64**Example**
65
66  ```js
67  class ServiceExt extends ServiceExtension {
68    onDestroy() {
69      console.log('onDestroy');
70    }
71  }
72  ```
73
74
75## ServiceExtensionAbility.onRequest
76
77onRequest(want: Want, startId: number): void;
78
79Called after **onCreate** is invoked when an ability is started by calling **startAbility**. The value of **startId** is incremented for each ability that is started.
80
81**System capability**: SystemCapability.Ability.AbilityRuntime.Core
82
83**Parameters**
84
85  | Name| Type| Mandatory| Description|
86  | -------- | -------- | -------- | -------- |
87  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
88  | startId | number | Yes| Number of ability start times. The initial value is **1**, and the value is automatically incremented for each ability started.|
89
90**Example**
91
92  ```js
93  class ServiceExt extends ServiceExtension {
94    onRequest(want, startId) {
95      console.log('onRequest, want:' + want.abilityName);
96    }
97  }
98  ```
99
100
101## ServiceExtensionAbility.onConnect
102
103onConnect(want: Want): rpc.RemoteObject;
104
105Called after **onCreate** is invoked when an ability is started by calling **connectAbility**. A **RemoteObject** object is returned for communication with the client.
106
107**System capability**: SystemCapability.Ability.AbilityRuntime.Core
108
109**Parameters**
110
111  | Name| Type| Mandatory| Description|
112  | -------- | -------- | -------- | -------- |
113  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
114
115**Return value**
116
117  | Type| Description|
118  | -------- | -------- |
119  | rpc.RemoteObject | A **RemoteObject** object used for communication with the client.|
120
121**Example**
122
123  ```js
124  import rpc from '@ohos.rpc'
125  class StubTest extends rpc.RemoteObject{
126      constructor(des) {
127          super(des);
128      }
129      onRemoteRequest(code, data, reply, option) {
130      }
131  }
132  class ServiceExt extends ServiceExtension {
133    onConnect(want) {
134      console.log('onConnect , want:' + want.abilityName);
135      return new StubTest("test");
136    }
137  }
138  ```
139
140
141## ServiceExtensionAbility.onDisconnect
142
143onDisconnect(want: Want): void;
144
145Called when the ability is disconnected.
146
147**System capability**: SystemCapability.Ability.AbilityRuntime.Core
148
149**Parameters**
150
151  | Name| Type| Mandatory| Description|
152  | -------- | -------- | -------- | -------- |
153  | want |[Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
154
155**Example**
156
157  ```js
158  class ServiceExt extends ServiceExtension {
159    onDisconnect(want) {
160      console.log('onDisconnect, want:' + want.abilityName);
161    }
162  }
163  ```
164