js-apis-service-extension-ability.md 7.0 KB
Newer Older
W
wusongqing 已提交
1
# ServiceExtensionAbility
W
wusongqing 已提交
2

3 4
The **ServiceExtensionAbility** module provides APIs for Service Extension abilities.

W
wusongqing 已提交
5 6 7 8
> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
9

W
wusongqing 已提交
10
## Modules to Import
W
wusongqing 已提交
11 12

```
W
wusongqing 已提交
13
import ServiceExtension from '@ohos.application.ServiceExtensionAbility';
W
wusongqing 已提交
14 15
```

W
wusongqing 已提交
16
## Required Permissions
W
wusongqing 已提交
17

W
wusongqing 已提交
18
None.
W
wusongqing 已提交
19

W
wusongqing 已提交
20
## Attributes
W
wusongqing 已提交
21

W
wusongqing 已提交
22 23
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

24 25
**System API**: This is a system API and cannot be called by third-party applications.

26
| Name| Type| Readable| Writable| Description| 
W
wusongqing 已提交
27
| -------- | -------- | -------- | -------- | -------- |
28
| context | [ServiceExtensionContext](js-apis-service-extension-context.md)  | Yes| No| Service Extension context, which is inherited from **ExtensionContext**.| 
W
wusongqing 已提交
29 30


W
wusongqing 已提交
31
## ServiceExtensionAbility.onCreate
W
wusongqing 已提交
32 33 34

onCreate(want: Want): void;

35
Called when a Service Extension ability is created to initialize the service logic.
W
wusongqing 已提交
36

W
wusongqing 已提交
37
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
38

39 40
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
41
**Parameters**
W
wusongqing 已提交
42

43 44 45
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want |  [Want](js-apis-application-Want.md) | Yes| Information related to this Service Extension ability, including the ability name and bundle name.| 
W
wusongqing 已提交
46

W
wusongqing 已提交
47 48 49
**Example**

  ```js
W
wusongqing 已提交
50 51
  class ServiceExt extends ServiceExtension {
    onCreate(want) {
W
wusongqing 已提交
52
      console.log('onCreate, want:' + want.abilityName);
W
wusongqing 已提交
53
    }
W
wusongqing 已提交
54 55 56 57
  }
  ```


W
wusongqing 已提交
58
## ServiceExtensionAbility.onDestroy
W
wusongqing 已提交
59 60 61

onDestroy(): void;

62
Called when this Service Extension ability is destroyed to clear resources.
W
wusongqing 已提交
63

W
wusongqing 已提交
64
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
65

66 67
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
68
**Example**
W
wusongqing 已提交
69

W
wusongqing 已提交
70
  ```js
W
wusongqing 已提交
71 72
  class ServiceExt extends ServiceExtension {
    onDestroy() {
W
wusongqing 已提交
73
      console.log('onDestroy');
W
wusongqing 已提交
74
    }
W
wusongqing 已提交
75 76 77 78
  }
  ```


W
wusongqing 已提交
79
## ServiceExtensionAbility.onRequest
W
wusongqing 已提交
80 81 82

onRequest(want: Want, startId: number): void;

83
Called after **onCreate** is invoked when a Service Extension ability is started by calling **startAbility**. The value of **startId** is incremented for each ability that is started.
W
wusongqing 已提交
84

W
wusongqing 已提交
85
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
86

87 88
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
89
**Parameters**
W
wusongqing 已提交
90

91 92 93 94
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want |  [Want](js-apis-application-Want.md) | Yes| Information related to this Service Extension ability, including the ability name and bundle name.| 
  | startId | number | Yes| Number of ability start times. The initial value is **1**, and the value is automatically incremented for each ability started.| 
W
wusongqing 已提交
95

W
wusongqing 已提交
96 97 98
**Example**

  ```js
W
wusongqing 已提交
99 100
  class ServiceExt extends ServiceExtension {
    onRequest(want, startId) {
W
wusongqing 已提交
101
      console.log('onRequest, want:' + want.abilityName);
W
wusongqing 已提交
102
    }
W
wusongqing 已提交
103 104 105 106
  }
  ```


W
wusongqing 已提交
107
## ServiceExtensionAbility.onConnect
W
wusongqing 已提交
108 109 110

onConnect(want: Want): rpc.RemoteObject;

111
Called after **onCreate** is invoked when a Service Extension ability is started by calling **connectAbility**. A **RemoteObject** object is returned for communication with the client.
W
wusongqing 已提交
112

W
wusongqing 已提交
113
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
114

115 116
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
117
**Parameters**
W
wusongqing 已提交
118

119 120 121
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want |  [Want](js-apis-application-Want.md)| Yes| Information related to this Service Extension ability, including the ability name and bundle name.| 
W
wusongqing 已提交
122

W
wusongqing 已提交
123 124
**Return value**

125 126 127
  | Type| Description| 
  | -------- | -------- |
  | rpc.RemoteObject | A **RemoteObject** object used for communication with the client.| 
W
wusongqing 已提交
128

W
wusongqing 已提交
129 130 131
**Example**

  ```js
W
wusongqing 已提交
132 133 134 135 136
  import rpc from '@ohos.rpc'
  class StubTest extends rpc.RemoteObject{
      constructor(des) {
          super(des);
      }
W
wusongqing 已提交
137
      onConnect(code, data, reply, option) {
W
wusongqing 已提交
138 139
      }
  }
W
wusongqing 已提交
140 141
  class ServiceExt extends ServiceExtension {
    onConnect(want) {
W
wusongqing 已提交
142 143
      console.log('onConnect , want:' + want.abilityName);
      return new StubTest("test");
W
wusongqing 已提交
144
    }
W
wusongqing 已提交
145 146 147 148
  }
  ```


W
wusongqing 已提交
149
## ServiceExtensionAbility.onDisconnect
W
wusongqing 已提交
150 151 152

onDisconnect(want: Want): void;

153
Called when this Service Extension ability is disconnected.
W
wusongqing 已提交
154

W
wusongqing 已提交
155
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
156

157 158
**System API**: This is a system API and cannot be called by third-party applications.

W
wusongqing 已提交
159
**Parameters**
W
wusongqing 已提交
160

161 162 163
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want |[Want](js-apis-application-Want.md)| Yes| Information related to this Service Extension ability, including the ability name and bundle name.| 
W
wusongqing 已提交
164

W
wusongqing 已提交
165 166 167
**Example**

  ```js
W
wusongqing 已提交
168 169
  class ServiceExt extends ServiceExtension {
    onDisconnect(want) {
W
wusongqing 已提交
170
      console.log('onDisconnect, want:' + want.abilityName);
W
wusongqing 已提交
171
    }
W
wusongqing 已提交
172 173
  }
  ```
174 175 176 177 178 179 180 181 182 183 184 185 186

## ServiceExtensionAbility.onReconnect

onReconnect(want: Want): void;

Called when this Service Extension ability is reconnected.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

187 188 189
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | want |[Want](js-apis-application-Want.md)| Yes| Information related to this Service Extension ability, including the ability name and bundle name.| 
190 191 192 193 194

**Example**

  ```js
  class ServiceExt extends ServiceExtension {
195 196
    onReconnect(want) {
      console.log('onReconnect, want:' + want.abilityName);
197 198 199 200 201 202 203 204
    }
  }
  ```

## ServiceExtensionAbility.onConfigurationUpdated

onConfigurationUpdated(config: Configuration): void;

205
 Called when the configuration of this Service Extension ability is updated.
206 207 208 209 210 211 212

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

213 214 215
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | config | [Configuration](js-apis-configuration.md) | Yes| New configuration.| 
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

**Example**
    
  ```js
  class ServiceExt extends ServiceExtension {
      onConfigurationUpdated(config) {
          console.log('onConfigurationUpdated, config:' + JSON.stringify(config));
      }
  }
  ```

## ServiceExtensionAbility.dump

dump(params: Array\<string>): Array\<string>;

Dumps the client information.

**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

239 240 241
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | params | Array\<string> | Yes| Parameters in the form of a command.| 
242 243 244 245 246 247 248 249 250 251 252

**Example**
    
  ```js
  class ServiceExt extends ServiceExtension {
      dump(params) {
          console.log('dump, params:' + JSON.stringify(params));
          return ["params"]
      }
  }
  ```