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

W
wusongqing 已提交
3
> **NOTE**<br/>
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 25
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

W
wusongqing 已提交
26
| Name| Type| Readable| Writable| Description|
W
wusongqing 已提交
27
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
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;

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

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

W
wusongqing 已提交
39
**Parameters**
W
wusongqing 已提交
40

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

W
wusongqing 已提交
45 46 47
**Example**

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


W
wusongqing 已提交
56
## ServiceExtensionAbility.onDestroy
W
wusongqing 已提交
57 58 59

onDestroy(): void;

W
wusongqing 已提交
60
Called when this extension is destroyed to clear resources.
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
63

W
wusongqing 已提交
64
**Example**
W
wusongqing 已提交
65

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


W
wusongqing 已提交
75
## ServiceExtensionAbility.onRequest
W
wusongqing 已提交
76 77 78

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

W
wusongqing 已提交
79
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 已提交
80

W
wusongqing 已提交
81
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
82

W
wusongqing 已提交
83
**Parameters**
W
wusongqing 已提交
84

W
wusongqing 已提交
85 86 87 88
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |  [Want](js-apis-application-Want.md) | 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 已提交
89

W
wusongqing 已提交
90 91 92
**Example**

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


W
wusongqing 已提交
101
## ServiceExtensionAbility.onConnect
W
wusongqing 已提交
102 103 104

onConnect(want: Want): rpc.RemoteObject;

W
wusongqing 已提交
105
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 已提交
106

W
wusongqing 已提交
107
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
108

W
wusongqing 已提交
109
**Parameters**
W
wusongqing 已提交
110

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

W
wusongqing 已提交
115 116
**Return value**

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

W
wusongqing 已提交
121 122 123
**Example**

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


W
wusongqing 已提交
141
## ServiceExtensionAbility.onDisconnect
W
wusongqing 已提交
142 143 144

onDisconnect(want: Want): void;

W
wusongqing 已提交
145
Called when the ability is disconnected.
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
148

W
wusongqing 已提交
149
**Parameters**
W
wusongqing 已提交
150

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

W
wusongqing 已提交
155 156 157
**Example**

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