1e41f4b71Sopenharmony_ci# @ohos.app.ability.ServiceExtensionAbility (ServiceExtensionAbility) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **ServiceExtensionAbility** module provides lifecycle callbacks when a ServiceExtensionAbility (background service) is created, destroyed, connected, or disconnected. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> The APIs provided by this module are system APIs. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Modules to Import 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci```ts 16e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 17e41f4b71Sopenharmony_ci``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Required Permissions 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciNone. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## Properties 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description| 30e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 31e41f4b71Sopenharmony_ci| context | [ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext-sys.md) | Yes| No| ServiceExtensionContext, which is inherited from **ExtensionContext**.| 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onCreate 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_cionCreate(want: Want): void; 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciCalled to initialize the service logic when a ServiceExtensionAbility is being created. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**Parameters** 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 47e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 48e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.| 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**Example** 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```ts 53e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 56e41f4b71Sopenharmony_ci onCreate(want: Want) { 57e41f4b71Sopenharmony_ci console.log(`onCreate, want: ${want.abilityName}`); 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci} 60e41f4b71Sopenharmony_ci``` 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onDestroy 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_cionDestroy(): void; 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciCalled to clear resources when this ServiceExtensionAbility is being destroyed. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**Example** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci```ts 76e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 79e41f4b71Sopenharmony_ci onDestroy() { 80e41f4b71Sopenharmony_ci console.log('onDestroy'); 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci} 83e41f4b71Sopenharmony_ci``` 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onRequest 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_cionRequest(want: Want, startId: number): void; 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciCalled following **onCreate()** when a ServiceExtensionAbility is started by calling **startAbility()** or **startServiceExtensionAbility()**. The value of **startId** is incremented for each ServiceExtensionAbility that is started. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Parameters** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 99e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 100e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.| 101e41f4b71Sopenharmony_ci| startId | number | Yes| Number of ServiceExtensionAbility start times. The initial value is **1**, and the value is automatically incremented for each ServiceExtensionAbility started.| 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Example** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci```ts 106e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 109e41f4b71Sopenharmony_ci onRequest(want: Want, startId: number) { 110e41f4b71Sopenharmony_ci console.log('onRequest, want: ${want.abilityName}'); 111e41f4b71Sopenharmony_ci } 112e41f4b71Sopenharmony_ci} 113e41f4b71Sopenharmony_ci``` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onConnect 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_cionConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject>; 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciCalled following **onCreate()** when a ServiceExtensionAbility is started by calling **connectAbility()**. A **RemoteObject** object is returned for communication between the server and client. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**Parameters** 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 129e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 130e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.| 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**Return value** 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci| Type| Description| 135e41f4b71Sopenharmony_ci| -------- | -------- | 136e41f4b71Sopenharmony_ci| rpc.RemoteObject | A **RemoteObject** object used for communication between the server and client.| 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**Example** 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci```ts 141e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 142e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciclass StubTest extends rpc.RemoteObject{ 145e41f4b71Sopenharmony_ci constructor(des: string) { 146e41f4b71Sopenharmony_ci super(des); 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci onConnect(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) { 149e41f4b71Sopenharmony_ci } 150e41f4b71Sopenharmony_ci} 151e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 152e41f4b71Sopenharmony_ci onConnect(want: Want) { 153e41f4b71Sopenharmony_ci console.log('onConnect , want: ${want.abilityName}'); 154e41f4b71Sopenharmony_ci return new StubTest('test'); 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci} 157e41f4b71Sopenharmony_ci``` 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ciIf the returned **RemoteObject** object depends on an asynchronous API, you can use the asynchronous lifecycle. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci```ts 162e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 163e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit'; 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ciclass StubTest extends rpc.RemoteObject{ 166e41f4b71Sopenharmony_ci constructor(des: string) { 167e41f4b71Sopenharmony_ci super(des); 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci onConnect(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) { 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci} 172e41f4b71Sopenharmony_ciasync function getDescriptor() { 173e41f4b71Sopenharmony_ci // Call the asynchronous function. 174e41f4b71Sopenharmony_ci return "asyncTest" 175e41f4b71Sopenharmony_ci} 176e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 177e41f4b71Sopenharmony_ci async onConnect(want: Want) { 178e41f4b71Sopenharmony_ci console.log(`onConnect , want: ${want.abilityName}`); 179e41f4b71Sopenharmony_ci let descriptor = await getDescriptor(); 180e41f4b71Sopenharmony_ci return new StubTest(descriptor); 181e41f4b71Sopenharmony_ci } 182e41f4b71Sopenharmony_ci} 183e41f4b71Sopenharmony_ci``` 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onDisconnect 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_cionDisconnect(want: Want): void | Promise\<void>; 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciCalled when a client is disconnected from this ServiceExtensionAbility. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**Parameters** 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 198e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 199e41f4b71Sopenharmony_ci| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.| 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Example** 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci```ts 204e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 207e41f4b71Sopenharmony_ci onDisconnect(want: Want) { 208e41f4b71Sopenharmony_ci console.log('onDisconnect, want: ${want.abilityName}'); 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci} 211e41f4b71Sopenharmony_ci``` 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ciAfter the **onDisconnect()** lifecycle callback is executed, the application may exit. Consequently, the asynchronous function (for example, asynchronously writing data to the database) in **onDisconnect()** may fail to be executed. The asynchronous lifecycle can be used to ensure that the subsequent lifecycle continues after the asynchronous **onDisconnect()** is complete. 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci```ts 216e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 219e41f4b71Sopenharmony_ci async onDisconnect(want: Want) { 220e41f4b71Sopenharmony_ci console.log('onDisconnect, want: ${want.abilityName}'); 221e41f4b71Sopenharmony_ci // Call the asynchronous function. 222e41f4b71Sopenharmony_ci } 223e41f4b71Sopenharmony_ci} 224e41f4b71Sopenharmony_ci``` 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onReconnect 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_cionReconnect(want: Want): void; 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ciCalled when a new client attempts to connect to this ServiceExtensionAbility after all previous clients are disconnected. This capability is reserved. 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci**Parameters** 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 239e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 240e41f4b71Sopenharmony_ci| want |[Want](js-apis-app-ability-want.md)| Yes| Want information related to this ServiceExtensionAbility, including the ability name and bundle name.| 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**Example** 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci```ts 245e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Want } from '@kit.AbilityKit'; 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 248e41f4b71Sopenharmony_ci onReconnect(want: Want) { 249e41f4b71Sopenharmony_ci console.log('onReconnect, want: ${want.abilityName}'); 250e41f4b71Sopenharmony_ci } 251e41f4b71Sopenharmony_ci} 252e41f4b71Sopenharmony_ci``` 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onConfigurationUpdate 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_cionConfigurationUpdate(newConfig: Configuration): void; 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ciCalled when the configuration of this ServiceExtensionAbility is updated. 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci**Parameters** 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 267e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 268e41f4b71Sopenharmony_ci| newConfig | [Configuration](js-apis-app-ability-configuration.md) | Yes| New configuration.| 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci**Example** 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci```ts 273e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility, Configuration } from '@kit.AbilityKit'; 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 276e41f4b71Sopenharmony_ci onConfigurationUpdate(newConfig: Configuration) { 277e41f4b71Sopenharmony_ci console.log(`onConfigurationUpdate, config: ${JSON.stringify(newConfig)}`); 278e41f4b71Sopenharmony_ci } 279e41f4b71Sopenharmony_ci} 280e41f4b71Sopenharmony_ci``` 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci## ServiceExtensionAbility.onDump 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_cionDump(params: Array\<string>): Array\<string>; 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ciDumps the client information. 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**System API**: This is a system API and cannot be called by third-party applications. 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**Parameters** 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 295e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 296e41f4b71Sopenharmony_ci| params | Array\<string> | Yes| Parameters in the form of a command.| 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci**Example** 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci```ts 301e41f4b71Sopenharmony_ciimport { ServiceExtensionAbility } from '@kit.AbilityKit'; 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ciclass ServiceExt extends ServiceExtensionAbility { 304e41f4b71Sopenharmony_ci onDump(params: Array<string>) { 305e41f4b71Sopenharmony_ci console.log(`dump, params: ${JSON.stringify(params)}`); 306e41f4b71Sopenharmony_ci return ['params']; 307e41f4b71Sopenharmony_ci } 308e41f4b71Sopenharmony_ci} 309e41f4b71Sopenharmony_ci``` 310