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

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
W
wusongqing 已提交
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.
W
wusongqing 已提交
5 6


W
wusongqing 已提交
7
Provides APIs related to **ServiceExtension**.
W
wusongqing 已提交
8 9


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

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


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

W
wusongqing 已提交
19
None.
W
wusongqing 已提交
20 21


W
wusongqing 已提交
22
## Attributes
W
wusongqing 已提交
23

W
wusongqing 已提交
24
| Name| Type| Readable| Writable| Description| 
W
wusongqing 已提交
25
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
26
| context | [ServiceExtensionContext](js-apis-service-extension-context.md)  | Yes| No| Service extension context, which is inherited from **ExtensionContext**.<br>**System capability**: SystemCapability.Ability.AbilityRuntime.Core| 
W
wusongqing 已提交
27 28


W
wusongqing 已提交
29
## ServiceExtensionAbility.onCreate
W
wusongqing 已提交
30 31 32

onCreate(want: Want): void;

W
wusongqing 已提交
33
Called when an extension is created to initialize the service logic.
W
wusongqing 已提交
34

W
wusongqing 已提交
35
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
36

W
wusongqing 已提交
37
**Parameters**
W
wusongqing 已提交
38

W
wusongqing 已提交
39
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
40
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
41
  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.| 
W
wusongqing 已提交
42

W
wusongqing 已提交
43 44 45
**Example**

  ```js
W
wusongqing 已提交
46 47
  class ServiceExt extends ServiceExtension {
    onCreate(want) {
W
wusongqing 已提交
48
      console.log('onCreate, want:' + want.abilityName);
W
wusongqing 已提交
49
    }
W
wusongqing 已提交
50 51 52 53
  }
  ```


W
wusongqing 已提交
54
## ServiceExtensionAbility.onDestroy
W
wusongqing 已提交
55 56 57

onDestroy(): void;

W
wusongqing 已提交
58
Called when this extension is destroyed to clear resources.
W
wusongqing 已提交
59

W
wusongqing 已提交
60
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Example**
W
wusongqing 已提交
63

W
wusongqing 已提交
64
  ```js
W
wusongqing 已提交
65 66
  class ServiceExt extends ServiceExtension {
    onDestroy() {
W
wusongqing 已提交
67
      console.log('onDestroy');
W
wusongqing 已提交
68
    }
W
wusongqing 已提交
69 70 71 72
  }
  ```


W
wusongqing 已提交
73
## ServiceExtensionAbility.onRequest
W
wusongqing 已提交
74 75 76

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

W
wusongqing 已提交
77
Called after **onCreate** is invoked when an ability is started by calling **startAbility**. The value of **startId** is incremented for each ability that is started.
W
wusongqing 已提交
78

W
wusongqing 已提交
79
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
80

W
wusongqing 已提交
81
**Parameters**
W
wusongqing 已提交
82

W
wusongqing 已提交
83
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
84
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
85 86
  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, 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 已提交
87

W
wusongqing 已提交
88 89 90
**Example**

  ```js
W
wusongqing 已提交
91 92
  class ServiceExt extends ServiceExtension {
    onRequest(want, startId) {
W
wusongqing 已提交
93
      console.log('onRequest, want:' + want.abilityName);
W
wusongqing 已提交
94
    }
W
wusongqing 已提交
95 96 97 98
  }
  ```


W
wusongqing 已提交
99
## ServiceExtensionAbility.onConnect
W
wusongqing 已提交
100 101 102

onConnect(want: Want): rpc.RemoteObject;

W
wusongqing 已提交
103
Called after **onCreate** is invoked when an ability is started by calling **connectAbility**. A **RemoteObject** object is returned for communication with the client.
W
wusongqing 已提交
104

W
wusongqing 已提交
105
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
106

W
wusongqing 已提交
107
**Parameters**
W
wusongqing 已提交
108

W
wusongqing 已提交
109
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
110
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
111
  | want |  [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.| 
W
wusongqing 已提交
112

W
wusongqing 已提交
113 114
**Return value**

W
wusongqing 已提交
115
  | Type| Description| 
W
wusongqing 已提交
116
  | -------- | -------- |
W
wusongqing 已提交
117
  | rpc.RemoteObject | A **RemoteObject** object used for communication with the client.| 
W
wusongqing 已提交
118

W
wusongqing 已提交
119 120 121
**Example**

  ```js
W
wusongqing 已提交
122 123 124 125 126 127 128 129
  import rpc from '@ohos.rpc'
  class StubTest extends rpc.RemoteObject{
      constructor(des) {
          super(des);
      }
      onRemoteRequest(code, data, reply, option) {
      }
  }
W
wusongqing 已提交
130 131
  class ServiceExt extends ServiceExtension {
    onConnect(want) {
W
wusongqing 已提交
132 133
      console.log('onConnect , want:' + want.abilityName);
      return new StubTest("test");
W
wusongqing 已提交
134
    }
W
wusongqing 已提交
135 136 137 138
  }
  ```


W
wusongqing 已提交
139
## ServiceExtensionAbility.onDisconnect
W
wusongqing 已提交
140 141 142

onDisconnect(want: Want): void;

W
wusongqing 已提交
143
Called when the ability is disconnected.
W
wusongqing 已提交
144

W
wusongqing 已提交
145
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**Parameters**
W
wusongqing 已提交
148

W
wusongqing 已提交
149
  | Name| Type| Mandatory| Description| 
W
wusongqing 已提交
150
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
151
  | want |[Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.| 
W
wusongqing 已提交
152

W
wusongqing 已提交
153 154 155
**Example**

  ```js
W
wusongqing 已提交
156 157
  class ServiceExt extends ServiceExtension {
    onDisconnect(want) {
W
wusongqing 已提交
158
      console.log('onDisconnect, want:' + want.abilityName);
W
wusongqing 已提交
159
    }
W
wusongqing 已提交
160 161
  }
  ```