• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.ServiceExtensionAbility (ServiceExtensionAbility)
2
3The **ServiceExtensionAbility** module provides lifecycle callbacks when a ServiceExtensionAbility (background service) is created, destroyed, connected, or disconnected.
4
5> **NOTE**
6>
7> The APIs of this module are supported since API version 9 and are deprecated in versions later than API version 9. You are advised to use [@ohos.app.ability.ServiceExtensionAbility](js-apis-app-ability-serviceExtensionAbility.md) instead. 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 stage model.
9
10## Modules to Import
11
12```ts
13import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
14```
15
16## Required Permissions
17
18None.
19
20## Attributes
21
22**System capability**: SystemCapability.Ability.AbilityRuntime.Core
23
24**System API**: This is a system API and cannot be called by third-party applications.
25
26| Name| Type| Readable| Writable| Description|
27| -------- | -------- | -------- | -------- | -------- |
28| context | [ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)  | Yes| No| ServiceExtensionContext, which is inherited from **ExtensionContext**.|
29
30
31## ServiceExtensionAbility.onCreate
32
33onCreate(want: Want): void;
34
35Called when a ServiceExtensionAbility is created to initialize the service logic.
36
37**System capability**: SystemCapability.Ability.AbilityRuntime.Core
38
39**System API**: This is a system API and cannot be called by third-party applications.
40
41**Parameters**
42
43| Name| Type| Mandatory| Description|
44| -------- | -------- | -------- | -------- |
45| want |  [Want](js-apis-app-ability-want.md) | Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.|
46
47**Example**
48
49  ```ts
50  class ServiceExt extends ServiceExtension {
51    onCreate(want) {
52      console.log('onCreate, want:' + want.abilityName);
53    }
54  }
55  ```
56
57
58## ServiceExtensionAbility.onDestroy
59
60onDestroy(): void;
61
62Called when this ServiceExtensionAbility is destroyed to clear resources.
63
64**System capability**: SystemCapability.Ability.AbilityRuntime.Core
65
66**System API**: This is a system API and cannot be called by third-party applications.
67
68**Example**
69
70  ```ts
71  class ServiceExt extends ServiceExtension {
72    onDestroy() {
73      console.log('onDestroy');
74    }
75  }
76  ```
77
78
79## ServiceExtensionAbility.onRequest
80
81onRequest(want: Want, startId: number): void;
82
83Called following **onCreate()** when a ServiceExtensionAbility is started by calling **startAbility()** or **startServiceExtensionAbility()**. The value of **startId** is incremented for each ability that is started.
84
85**System capability**: SystemCapability.Ability.AbilityRuntime.Core
86
87**System API**: This is a system API and cannot be called by third-party applications.
88
89**Parameters**
90
91| Name| Type| Mandatory| Description|
92| -------- | -------- | -------- | -------- |
93| want |  [Want](js-apis-app-ability-want.md) | Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.|
94| startId | number | Yes| Number of ability start times. The initial value is **1**, and the value is automatically incremented for each ability started.|
95
96**Example**
97
98  ```ts
99  class ServiceExt extends ServiceExtension {
100    onRequest(want, startId) {
101      console.log('onRequest, want:' + want.abilityName);
102    }
103  }
104  ```
105
106
107## ServiceExtensionAbility.onConnect
108
109onConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject>;
110
111Called following **onCreate()** when a ServiceExtensionAbility is started by calling **connectAbility()**. A **RemoteObject** object is returned for communication between the server and client.
112
113**System capability**: SystemCapability.Ability.AbilityRuntime.Core
114
115**System API**: This is a system API and cannot be called by third-party applications.
116
117**Parameters**
118
119| Name| Type| Mandatory| Description|
120| -------- | -------- | -------- | -------- |
121| want |  [Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.|
122
123**Return value**
124
125| Type| Description|
126| -------- | -------- |
127| rpc.RemoteObject | A **RemoteObject** object used for communication between the server and client.|
128
129**Example**
130
131  ```ts
132  import rpc from '@ohos.rpc';
133  class StubTest extends rpc.RemoteObject{
134      constructor(des) {
135          super(des);
136      }
137      onConnect(code, data, reply, option) {
138      }
139  }
140  class ServiceExt extends ServiceExtension {
141    onConnect(want) {
142      console.log('onConnect , want:' + want.abilityName);
143      return new StubTest('test');
144    }
145  }
146  ```
147
148
149## ServiceExtensionAbility.onDisconnect
150
151onDisconnect(want: Want): void | Promise\<void>;
152
153Called when a client is disconnected from this ServiceExtensionAbility.
154
155**System capability**: SystemCapability.Ability.AbilityRuntime.Core
156
157**System API**: This is a system API and cannot be called by third-party applications.
158
159**Parameters**
160
161| Name| Type| Mandatory| Description|
162| -------- | -------- | -------- | -------- |
163| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.|
164
165**Example**
166
167  ```ts
168  class ServiceExt extends ServiceExtension {
169    onDisconnect(want) {
170      console.log('onDisconnect, want:' + want.abilityName);
171    }
172  }
173  ```
174
175## ServiceExtensionAbility.onReconnect
176
177onReconnect(want: Want): void;
178
179Called when a new client attempts to connect to this ServiceExtensionAbility after all previous clients are disconnected. This capability is reserved.
180
181**System capability**: SystemCapability.Ability.AbilityRuntime.Core
182
183**System API**: This is a system API and cannot be called by third-party applications.
184
185**Parameters**
186
187| Name| Type| Mandatory| Description|
188| -------- | -------- | -------- | -------- |
189| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.|
190
191**Example**
192
193  ```ts
194  class ServiceExt extends ServiceExtension {
195    onReconnect(want) {
196      console.log('onReconnect, want:' + want.abilityName);
197    }
198  }
199  ```
200
201## ServiceExtensionAbility.onConfigurationUpdate
202
203onConfigurationUpdate(newConfig: Configuration): void;
204
205Called when the configuration of this ServiceExtensionAbility is updated.
206
207**System capability**: SystemCapability.Ability.AbilityRuntime.Core
208
209**System API**: This is a system API and cannot be called by third-party applications.
210
211**Parameters**
212
213| Name| Type| Mandatory| Description|
214| -------- | -------- | -------- | -------- |
215| newConfig | [Configuration](js-apis-app-ability-configuration.md) | Yes| New configuration.|
216
217**Example**
218
219  ```ts
220  class ServiceExt extends ServiceExtension {
221      onConfigurationUpdate(config) {
222          console.log('onConfigurationUpdate, config:' + JSON.stringify(config));
223      }
224  }
225  ```
226
227## ServiceExtensionAbility.onDump
228
229onDump(params: Array\<string>): Array\<string>;
230
231Dumps the client information.
232
233**System capability**: SystemCapability.Ability.AbilityRuntime.Core
234
235**System API**: This is a system API and cannot be called by third-party applications.
236
237**Parameters**
238
239| Name| Type| Mandatory| Description|
240| -------- | -------- | -------- | -------- |
241| params | Array\<string> | Yes| Parameters in the form of a command.|
242
243**Example**
244
245  ```ts
246  class ServiceExt extends ServiceExtension {
247      onDump(params) {
248          console.log('dump, params:' + JSON.stringify(params));
249          return ['params'];
250      }
251  }
252  ```
253
254