js-apis-app-ability-serviceExtensionAbility.md 6.9 KB
Newer Older
D
donglin 已提交
1 2 3 4 5 6
# ServiceExtensionAbility

ServiceExtensionAbility模块提供ServiceExtension服务扩展相关接口的能力。

> **说明:**
> 
M
m00512953 已提交
7
> 本模块首批接口从API version 9开始支持,从API version 9废弃,替换模块为[@ohos.app.ability.ServiceExtensionAbility](js-apis-app-ability-serviceExtensionAbility.md)。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
D
donglin 已提交
8 9 10 11
> 本模块接口仅可在Stage模型下使用。

## 导入模块

M
m00512953 已提交
12
```ts
D
donglin 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
```

## 权限



## 属性

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

D
merge  
donglin 已提交
26
| 名称 | 类型 | 可读 | 可写 | 说明 | 
D
donglin 已提交
27
| -------- | -------- | -------- | -------- | -------- |
M
m00512953 已提交
28
| context | [ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md)  | 是 | 否 | ServiceExtension的上下文环境,继承自ExtensionContext。 | 
D
donglin 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44


## ServiceExtensionAbility.onCreate

onCreate(want: Want): void;

Extension生命周期回调,在创建时回调,执行初始化业务逻辑操作。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
45
  | want |  [Want](js-apis-app-ability-want.md) | 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | 
D
donglin 已提交
46 47 48

**示例:**

M
m00512953 已提交
49
  ```ts
D
donglin 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  class ServiceExt extends ServiceExtension {
    onCreate(want) {
      console.log('onCreate, want:' + want.abilityName);
    }
  }
  ```


## ServiceExtensionAbility.onDestroy

onDestroy(): void;

Extension生命周期回调,在销毁时回调,执行资源清理等操作。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**示例:**

M
m00512953 已提交
70
  ```ts
D
donglin 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  class ServiceExt extends ServiceExtension {
    onDestroy() {
      console.log('onDestroy');
    }
  }
  ```


## ServiceExtensionAbility.onRequest

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

Extension生命周期回调,如果是startAbility拉起的服务,会在onCreate之后回调。每次拉起服务都会回调,startId会递增。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
93
  | want |  [Want](js-apis-app-ability-want.md) | 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | 
D
donglin 已提交
94 95 96 97
  | startId | number | 是 | 返回拉起次数。首次拉起初始值返回1,多次之后自动递增。 | 

**示例:**

M
m00512953 已提交
98
  ```ts
D
donglin 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  class ServiceExt extends ServiceExtension {
    onRequest(want, startId) {
      console.log('onRequest, want:' + want.abilityName);
    }
  }
  ```


## ServiceExtensionAbility.onConnect

onConnect(want: Want): rpc.RemoteObject;

Extension生命周期回调,如果是connectAbility拉起的服务,会在onCreate之后回调。返回一个RemoteObject对象,用于和客户端进行通信。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
121
  | want |  [Want](js-apis-app-ability-want.md)| 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | 
D
donglin 已提交
122 123 124 125 126 127 128 129 130

**返回值:**

  | 类型 | 说明 | 
  | -------- | -------- |
  | rpc.RemoteObject | 一个RemoteObject对象,用于和客户端进行通信。 | 

**示例:**

M
m00512953 已提交
131
  ```ts
D
donglin 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  import rpc from '@ohos.rpc'
  class StubTest extends rpc.RemoteObject{
      constructor(des) {
          super(des);
      }
      onConnect(code, data, reply, option) {
      }
  }
  class ServiceExt extends ServiceExtension {
    onConnect(want) {
      console.log('onConnect , want:' + want.abilityName);
      return new StubTest("test");
    }
  }
  ```


## ServiceExtensionAbility.onDisconnect

onDisconnect(want: Want): void;

Extension的生命周期,断开服务连接时回调。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
163
  | want |[Want](js-apis-app-ability-want.md)| 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | 
D
donglin 已提交
164 165 166

**示例:**

M
m00512953 已提交
167
  ```ts
D
donglin 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  class ServiceExt extends ServiceExtension {
    onDisconnect(want) {
      console.log('onDisconnect, want:' + want.abilityName);
    }
  }
  ```

## ServiceExtensionAbility.onReconnect

onReconnect(want: Want): void;

当新客户端在所有以前的客户端连接之后尝试连接到服务扩展时调用

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
189
  | want |[Want](js-apis-app-ability-want.md)| 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | 
D
donglin 已提交
190 191 192

**示例:**

M
m00512953 已提交
193
  ```ts
D
donglin 已提交
194 195 196 197 198 199 200
  class ServiceExt extends ServiceExtension {
    onReconnect(want) {
      console.log('onReconnect, want:' + want.abilityName);
    }
  }
  ```

D
fix  
donglin 已提交
201
## ServiceExtensionAbility.onConfigurationUpdate
D
donglin 已提交
202

D
merge  
donglin 已提交
203
onConfigurationUpdate(config: Configuration): void;
D
donglin 已提交
204 205 206 207 208 209 210 211 212 213 214

当Extension更新配置信息时调用。

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
M
m00512953 已提交
215
  | config | [Configuration](js-apis-app-ability-configuration.md) | 是 | 表示需要更新的配置信息。 | 
D
donglin 已提交
216 217 218

**示例:**
    
M
m00512953 已提交
219
  ```ts
D
donglin 已提交
220
  class ServiceExt extends ServiceExtension {
D
fix  
donglin 已提交
221 222
      onConfigurationUpdate(config) {
          console.log('onConfigurationUpdate, config:' + JSON.stringify(config));
D
donglin 已提交
223 224 225 226 227 228 229 230 231 232
      }
  }
  ```

## ServiceExtensionAbility.onDump

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

转储客户端信息时调用。

233
**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
D
donglin 已提交
234 235 236 237 238 239 240 241 242 243 244

**系统API**: 此接口为系统接口,三方应用不支持调用。

**参数:**

  | 参数名 | 类型 | 必填 | 说明 | 
  | -------- | -------- | -------- | -------- |
  | params | Array\<string> | 是 | 表示命令形式的参数。| 

**示例:**
    
M
m00512953 已提交
245
  ```ts
D
donglin 已提交
246 247 248 249 250 251 252 253
  class ServiceExt extends ServiceExtension {
      onDump(params) {
          console.log('dump, params:' + JSON.stringify(params));
          return ["params"]
      }
  }
  ```