js-apis-inner-application-serviceExtensionContext.md 51.0 KB
Newer Older
1 2
# ServiceExtensionContext

3
ServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。
Y
yuyaozhi 已提交
4

U
unknown 已提交
5
ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力,包括启动、停止、绑定、解绑Ability。
Y
yuyaozhi 已提交
6

Y
yuyaozhi 已提交
7 8
> **说明:**
> 
H
HuangXW 已提交
9 10
>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>  - 本模块接口仅可在Stage模型下使用。
11

12
## 使用说明
Y
yuyaozhi 已提交
13

14
在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。
15

16
```ts
H
HuangXW 已提交
17 18
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

M
mingxihua 已提交
19
  let context;
M
mingxihua 已提交
20
  let commRemote; // 断开连接时需要释放
21
  class EntryAbility extends ServiceExtensionAbility {
H
HuangXW 已提交
22
    onCreate() {
Z
zhongjianfei 已提交
23
      context = this.context; // 获取ServiceExtensionContext
H
HuangXW 已提交
24
    }
25 26
  }
```
Y
yuyaozhi 已提交
27

H
HuangXW 已提交
28
## ServiceExtensionContext.startAbility
29

30
startAbility(want: Want, callback: AsyncCallback<void>): void;
31 32 33

启动Ability。

Y
yuyaozhi 已提交
34
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
35

Y
yuyaozhi 已提交
36 37
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
38
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
39

Z
zhongjianfei 已提交
40 41
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
42
| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
Z
zhongjianfei 已提交
43
| callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 |
44

H
HuangXW 已提交
45 46 47 48
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
49
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
50
| 401 | Invalid input parameter. |
M
m00512953 已提交
51 52 53 54 55 56 57 58
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
59
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
60 61 62 63 64 65 66 67 68
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
69

Y
yuyaozhi 已提交
70 71
**示例:**

72
  ```ts
M
mingxihua 已提交
73
  let want = {
M
mingxihua 已提交
74 75
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
H
HuangXW 已提交
76 77 78 79 80 81
  };

  try {
    this.context.startAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
82
        console.error('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
83 84 85 86
        return;
      }
      // 执行正常业务
      console.log('startAbility succeed');
87
    });
H
HuangXW 已提交
88 89
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
90
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
91
  }
92 93
  ```

H
HuangXW 已提交
94
## ServiceExtensionContext.startAbility
95

Y
yuyaozhi 已提交
96
startAbility(want: Want, options?: StartOptions): Promise\<void>;
97

U
unknown 已提交
98
启动Ability,结果以Promise的形式返回。
99

Y
yuyaozhi 已提交
100
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
101

Y
yuyaozhi 已提交
102 103
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
104
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
105

Z
zhongjianfei 已提交
106 107
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
108
| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
Z
zhongjianfei 已提交
109
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
110

Y
yuyaozhi 已提交
111 112
**返回值:**

Z
zhongjianfei 已提交
113 114
| 类型 | 说明 |
| -------- | -------- |
U
unknown 已提交
115
| Promise&lt;void&gt; | 返回一个Promise,包含启动的结果。 |
116

H
HuangXW 已提交
117 118 119 120
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
121
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
122
| 401 | Invalid input parameter. |
M
m00512953 已提交
123 124 125 126 127 128 129 130
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
131
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
132 133 134 135 136 137 138 139 140
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
141

Y
yuyaozhi 已提交
142 143
**示例:**

144
  ```ts
M
mingxihua 已提交
145
  let want = {
M
mingxihua 已提交
146 147
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
H
HuangXW 已提交
148
  };
M
mingxihua 已提交
149
  let options = {
H
HuangXW 已提交
150 151
  	windowMode: 0,
  };
152

H
HuangXW 已提交
153 154 155 156 157 158 159 160
  try {
    this.context.startAbility(want, options)
      .then((data) => {
        // 执行正常业务
        console.log('startAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
161
        console.error('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
162 163 164
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
165
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
166
  }
167
  ```
Y
yuyaozhi 已提交
168

H
HuangXW 已提交
169
## ServiceExtensionContext.startAbility
170 171 172

startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void

U
unknown 已提交
173
启动Ability,结果以Callback的形式返回。
174 175 176 177 178 179 180 181 182

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
183 184
| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
185 186
| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回启动结果。 |

H
HuangXW 已提交
187 188 189 190
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
191
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
192
| 401 | Invalid input parameter. |
M
m00512953 已提交
193 194 195 196 197 198 199 200
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
201
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
202 203 204 205 206 207 208 209 210
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
211

212
**示例:**
H
HuangXW 已提交
213

214
  ```ts
M
mingxihua 已提交
215
  let want = {
M
mingxihua 已提交
216 217 218
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
219
  };
M
mingxihua 已提交
220
  let options = {
H
HuangXW 已提交
221
    windowMode: 0
222
  };
H
HuangXW 已提交
223 224 225 226 227

  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
228
        console.error('startAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
229 230 231 232 233 234 235
        return;
      }
      // 执行正常业务
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
236
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
237
  }
238 239 240 241
  ```

## ServiceExtensionContext.startAbilityWithAccount

242
startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
243 244 245

根据account启动Ability(callback形式)。

M
m00512953 已提交
246 247
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
M
m00512953 已提交
248
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
249 250
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

251 252 253 254 255 256 257 258
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
259
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
260
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
261 262
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

H
HuangXW 已提交
263 264 265 266
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
267
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
268
| 401 | Invalid input parameter. |
M
m00512953 已提交
269 270 271 272 273 274 275 276 277
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
278
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
279 280 281 282 283 284 285 286 287
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
288

289 290
**示例:**

291
  ```ts
M
mingxihua 已提交
292
  let want = {
M
mingxihua 已提交
293 294 295
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
296
  };
M
mingxihua 已提交
297
  let accountId = 100;
298

H
HuangXW 已提交
299 300 301 302
  try {
    this.context.startAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
303
        console.error('startAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
304 305 306 307 308 309 310
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
311
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
312 313
  }
  ```
314 315 316 317 318 319 320

## ServiceExtensionContext.startAbilityWithAccount

startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;

根据account启动Ability(callback形式)。

M
m00512953 已提交
321 322
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
M
m00512953 已提交
323
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
324 325
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

326 327 328 329 330 331 332 333
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
334
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
335
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
M
m00512953 已提交
336
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
337 338
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

H
HuangXW 已提交
339 340 341 342
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
343
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
344
| 401 | Invalid input parameter. |
M
m00512953 已提交
345 346 347 348 349 350 351 352 353
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
354
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
355 356 357 358 359 360 361 362 363
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
364

365 366
**示例:**

367
  ```ts
M
mingxihua 已提交
368
  let want = {
M
mingxihua 已提交
369 370 371
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
372
  };
M
mingxihua 已提交
373 374
  let accountId = 100;
  let options = {
H
HuangXW 已提交
375
    windowMode: 0
376
  };
H
HuangXW 已提交
377 378 379 380 381

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
382
        console.error('startAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
383 384 385 386 387 388 389
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
390
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
391
  }
392 393 394 395 396
  ```


## ServiceExtensionContext.startAbilityWithAccount

397
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
398 399 400

根据account启动Ability(Promise形式)。

M
m00512953 已提交
401 402
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
M
m00512953 已提交
403
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
M
m00512953 已提交
404 405
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)

406 407 408 409 410 411 412 413
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
414
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
415
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。。 |
M
m00512953 已提交
416
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
417

418 419
**返回值:**

Z
zhongjianfei 已提交
420 421 422
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
423

H
HuangXW 已提交
424 425 426 427
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
428
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
429
| 401 | Invalid input parameter. |
M
m00512953 已提交
430 431 432 433 434 435 436 437 438
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000010 | Can not operation with continue flag.        |
| 16000011 | Context does not exist.        |
U
unknown 已提交
439
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
440 441 442 443 444 445 446 447 448
| 16000051 | Network error. The network is abnormal. |
| 16000052 | Free install not support. The application does not support freeinstall |
| 16000053 | Not top ability. The application is not top ability. |
| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
| 16000055 | Free install timeout. |
| 16000056 | Can not free install other ability. |
| 16000057 | Not support cross device free install. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
449

450 451
**示例:**

452
  ```ts
M
mingxihua 已提交
453
  let want = {
M
mingxihua 已提交
454 455 456
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
457
  };
M
mingxihua 已提交
458 459
  let accountId = 100;
  let options = {
H
HuangXW 已提交
460
    windowMode: 0
461
  };
H
HuangXW 已提交
462 463 464 465 466 467 468 469 470

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // 执行正常业务
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
471
        console.error('startAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
472 473 474
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
475
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
476
  }
477 478
  ```

479 480 481 482 483 484 485 486 487 488 489 490 491 492
## ServiceExtensionContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;

启动一个新的ServiceExtensionAbility(callback形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
493
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
494 495
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

H
HuangXW 已提交
496 497 498 499
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
500
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
501
| 401 | Invalid input parameter. |
M
m00512953 已提交
502 503 504 505 506 507 508 509 510 511
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
512

513 514
**示例:**

515
  ```ts
M
mingxihua 已提交
516
  let want = {
M
mingxihua 已提交
517 518 519
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
520
  };
H
HuangXW 已提交
521 522 523 524 525

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
526
        console.error('startServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
527 528 529 530 531 532 533
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
534
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
535
  }
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  ```

## ServiceExtensionContext.startServiceExtensionAbility

startServiceExtensionAbility(want: Want): Promise\<void>;

启动一个新的ServiceExtensionAbility(Promise形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
552
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
553

554 555
**返回值:**

Z
zhongjianfei 已提交
556 557 558
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
559

H
HuangXW 已提交
560 561 562 563
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
564
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
565
| 401 | Invalid input parameter. |
M
m00512953 已提交
566 567 568 569 570 571 572 573 574 575
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
576

577 578
**示例:**

579
  ```ts
M
mingxihua 已提交
580
  let want = {
M
mingxihua 已提交
581 582 583
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
584
  };
H
HuangXW 已提交
585 586 587 588 589 590 591 592 593

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
594
        console.error('startServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
595 596 597
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
598
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
599
  }
600 601 602 603 604 605 606 607
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

启动一个新的ServiceExtensionAbility(callback形式)。

D
donglin 已提交
608
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
609 610 611 612 613 614 615 616 617

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
618
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
619
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
620 621
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

H
HuangXW 已提交
622 623 624 625
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
626
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
627
| 401 | Invalid input parameter. |
M
m00512953 已提交
628 629 630 631 632 633 634 635 636 637 638
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
639 640


641 642
**示例:**

643
  ```ts
M
mingxihua 已提交
644
  let want = {
M
mingxihua 已提交
645 646 647
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
648
  };
M
mingxihua 已提交
649
  let accountId = 100;
H
HuangXW 已提交
650 651 652 653 654

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
655
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
656 657 658 659 660 661 662
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
663
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
664
  }
665 666 667 668 669 670 671 672
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

启动一个新的ServiceExtensionAbility(Promise形式)。

D
donglin 已提交
673
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
674 675 676 677 678 679 680 681 682

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
683
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
684
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
685

686 687
**返回值:**

Z
zhongjianfei 已提交
688 689 690
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
691

H
HuangXW 已提交
692 693 694 695
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
696
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
697
| 401 | Invalid input parameter. |
M
m00512953 已提交
698 699 700 701 702 703 704 705 706 707 708
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
709

710 711
**示例:**

712
  ```ts
M
mingxihua 已提交
713
  let want = {
M
mingxihua 已提交
714 715 716
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
717
  };
M
mingxihua 已提交
718
  let accountId = 100;
H
HuangXW 已提交
719 720 721 722 723 724 725 726 727

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
728
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
729 730 731
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
732
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
733
  }
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;

停止同一应用程序内的服务(callback形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
750
| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
751
| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
752

H
HuangXW 已提交
753 754 755 756
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
757
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
758
| 401 | Invalid input parameter. |
M
m00512953 已提交
759 760 761 762 763 764 765
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
766

767 768
**示例:**

769
  ```ts
M
mingxihua 已提交
770
  let want = {
M
mingxihua 已提交
771 772 773
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
774
  };
H
HuangXW 已提交
775 776 777 778 779

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
780
        console.error('stopServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
781 782 783 784 785 786 787
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
788
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
789
  }
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

stopServiceExtensionAbility(want: Want): Promise\<void>;

停止同一应用程序内的服务(Promise形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
806
| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
807 808 809

**返回值:**

Z
zhongjianfei 已提交
810 811 812
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
813

H
HuangXW 已提交
814 815 816 817
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
818
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
819
| 401 | Invalid input parameter. |
M
m00512953 已提交
820 821 822 823 824 825 826
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
827

828 829
**示例:**

830
  ```ts
M
mingxihua 已提交
831
  let want = {
M
mingxihua 已提交
832 833 834
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
835
  };
H
HuangXW 已提交
836 837 838 839 840 841 842 843 844

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
845
        console.error('stopServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
846 847 848
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
849
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
850
  }
851 852 853 854 855 856 857 858
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;

使用帐户停止同一应用程序内的服务(callback形式)。

D
donglin 已提交
859
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
860 861 862 863 864 865 866 867 868

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
869
| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
870
| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
871
| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
872

H
HuangXW 已提交
873 874 875 876
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
877
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
878
| 401 | Invalid input parameter. |
M
m00512953 已提交
879 880 881 882 883 884 885 886
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
887

888 889
**示例:**

890
  ```ts
M
mingxihua 已提交
891
  let want = {
M
mingxihua 已提交
892 893 894
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
895
  };
M
mingxihua 已提交
896
  let accountId = 100;
H
HuangXW 已提交
897 898 899 900 901

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
902
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code), error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
903 904 905 906 907 908 909
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
910
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
911
  }
912 913 914 915 916 917 918 919
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;

使用帐户停止同一应用程序内的服务(Promise形式)。

D
donglin 已提交
920
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
921 922 923 924 925 926 927 928 929

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
930
| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
931
| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
932 933 934

**返回值:**

Z
zhongjianfei 已提交
935 936 937
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
938

H
HuangXW 已提交
939 940 941 942
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
943
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
944
| 401 | Invalid input parameter. |
M
m00512953 已提交
945 946 947 948 949 950 951 952
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
953

954 955
**示例:**

956
  ```ts
M
mingxihua 已提交
957
  let want = {
M
mingxihua 已提交
958 959 960
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
961
  };
M
mingxihua 已提交
962
  let accountId = 100;
H
HuangXW 已提交
963 964 965 966 967 968 969 970 971

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
972
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
973 974 975
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
976
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
977
  }
978
  ```
979

Y
yuyaozhi 已提交
980
## ServiceExtensionContext.terminateSelf
981

982
terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
983

984
停止Ability自身。
985

Y
yuyaozhi 已提交
986
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
987

Y
yuyaozhi 已提交
988 989
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
990
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
991

Z
zhongjianfei 已提交
992 993 994
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 |
995

H
HuangXW 已提交
996 997 998 999
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1000
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1001
| 401 | Invalid input parameter. |
M
m00512953 已提交
1002 1003 1004 1005
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1006

Y
yuyaozhi 已提交
1007 1008
**示例:**

1009
  ```ts
H
HuangXW 已提交
1010 1011 1012
  this.context.terminateSelf((error) => {
    if (error.code) {
      // 处理业务逻辑错误
M
mingxihua 已提交
1013
      console.error('terminateSelf failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
1014 1015 1016 1017
      return;
    }
    // 执行正常业务
    console.log('terminateSelf succeed');
1018
  });
1019 1020
  ```

Y
yuyaozhi 已提交
1021
## ServiceExtensionContext.terminateSelf
1022

1023
terminateSelf(): Promise&lt;void&gt;;
1024

1025
停止自身。通过Promise返回结果。
1026

Y
yuyaozhi 已提交
1027
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
1028

Y
yuyaozhi 已提交
1029 1030
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
1031
**返回值:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1032

Z
zhongjianfei 已提交
1033 1034 1035
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1036

H
HuangXW 已提交
1037 1038 1039 1040
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1041
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1042
| 401 | Invalid input parameter. |
M
m00512953 已提交
1043 1044 1045 1046
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1047

Y
yuyaozhi 已提交
1048 1049
**示例:**

1050
  ```ts
1051
  this.context.terminateSelf().then((data) => {
H
HuangXW 已提交
1052 1053
    // 执行正常业务
    console.log('terminateSelf succeed');
1054
  }).catch((error) => {
H
HuangXW 已提交
1055
    // 处理业务逻辑错误
M
mingxihua 已提交
1056
    console.error('terminateSelf failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
1057
  });
1058 1059
  ```

H
HuangXW 已提交
1060
## ServiceExtensionContext.connectServiceExtensionAbility
1061

H
HuangXW 已提交
1062
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1063

1064
将一个Ability与服务类型的Ability绑定。
1065

Y
yuyaozhi 已提交
1066
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
1067

Y
yuyaozhi 已提交
1068 1069
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
1070
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1071

Z
zhongjianfei 已提交
1072 1073
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1074
| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
Z
zhongjianfei 已提交
1075
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 |
1076

Y
yuyaozhi 已提交
1077 1078
**返回值:**

Z
zhongjianfei 已提交
1079 1080 1081
| 类型 | 说明 |
| -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 |
1082

H
HuangXW 已提交
1083 1084 1085 1086
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1087
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1088
| 401 | Invalid input parameter. |
M
m00512953 已提交
1089 1090 1091 1092 1093
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1094

Y
yuyaozhi 已提交
1095 1096
**示例:**

1097
  ```ts
M
mingxihua 已提交
1098
  let want = {
M
mingxihua 已提交
1099 1100
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
1101
  };
M
mingxihua 已提交
1102
  let options = {
M
mingxihua 已提交
1103
    onConnect(elementName, remote) { 
M
mingxihua 已提交
1104
      commRemote = remote;
M
mingxihua 已提交
1105 1106
      console.log('----------- onConnect -----------'); 
    },
1107
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
M
mingxihua 已提交
1108
    onFailed(code) { console.error('----------- onFailed -----------') }
M
mingxihua 已提交
1109
  };
H
HuangXW 已提交
1110

M
mingxihua 已提交
1111
  let connection = null;
H
HuangXW 已提交
1112 1113 1114 1115
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
1116
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1117
  }
1118 1119
  ```

H
HuangXW 已提交
1120
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1121

H
HuangXW 已提交
1122
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

使用AbilityInfo.AbilityType.SERVICE模板和account将当前能力连接到一个能力。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1134
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1135
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
M
m00512953 已提交
1136
| options | ConnectOptions | 否 | 远端对象实例。 |
1137 1138 1139 1140 1141 1142 1143

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 返回Ability连接的结果code。 |

H
HuangXW 已提交
1144 1145 1146 1147
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1148
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1149
| 401 | Invalid input parameter. |
M
m00512953 已提交
1150 1151 1152 1153 1154 1155
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1156

1157 1158
**示例:**

1159
  ```ts
M
mingxihua 已提交
1160
  let want = {
M
mingxihua 已提交
1161 1162 1163
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1164
  };
M
mingxihua 已提交
1165 1166
  let accountId = 100;
  let options = {
M
mingxihua 已提交
1167
    onConnect(elementName, remote) { 
M
mingxihua 已提交
1168
      commRemote = remote;
M
mingxihua 已提交
1169 1170
      console.log('----------- onConnect -----------');
    },
M
mingxihua 已提交
1171 1172 1173
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
    onFailed(code) { console.log('----------- onFailed -----------'); }
  };
H
HuangXW 已提交
1174

M
mingxihua 已提交
1175
  let connection = null;
H
HuangXW 已提交
1176 1177 1178 1179
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
1180
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1181
  }
1182
  ```
1183

H
HuangXW 已提交
1184
## ServiceExtensionContext.disconnectServiceExtensionAbility
1185

H
HuangXW 已提交
1186
disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1187

M
mingxihua 已提交
1188
将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。
1189

Y
yuyaozhi 已提交
1190
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
1191

Y
yuyaozhi 已提交
1192 1193
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
1194
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1195

Z
zhongjianfei 已提交
1196 1197
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1198
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
Z
zhongjianfei 已提交
1199
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 |
1200

H
HuangXW 已提交
1201 1202 1203 1204
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1205
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1206
| 401 | Invalid input parameter. |
M
m00512953 已提交
1207 1208 1209 1210
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1211

Y
yuyaozhi 已提交
1212 1213
**示例:**

1214
  ```ts
H
HuangXW 已提交
1215
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1216
  let connection = 1;
H
HuangXW 已提交
1217 1218 1219

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
M
mingxihua 已提交
1220
      commRemote = null;
H
HuangXW 已提交
1221 1222
      if (error.code) {
        // 处理业务逻辑错误
M
mingxihua 已提交
1223
        console.error('disconnectServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
1224 1225 1226 1227
        return;
      }
      // 执行正常业务
      console.log('disconnectServiceExtensionAbility succeed');
1228
    });
H
HuangXW 已提交
1229
  } catch (paramError) {
M
mingxihua 已提交
1230
    commRemote = null;
H
HuangXW 已提交
1231
    // 处理入参错误异常
M
mingxihua 已提交
1232
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1233
  }
1234 1235
  ```

H
HuangXW 已提交
1236
## ServiceExtensionContext.disconnectServiceExtensionAbility
1237

H
HuangXW 已提交
1238
disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1239

M
mingxihua 已提交
1240
将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空(Promise形式返回结果)。
1241

Y
yuyaozhi 已提交
1242
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
ahjxliubao2's avatar
ahjxliubao2 已提交
1243

Y
yuyaozhi 已提交
1244 1245
**系统API**: 此接口为系统接口,三方应用不支持调用。

Y
yuyaozhi 已提交
1246
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1247

Z
zhongjianfei 已提交
1248 1249
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1250
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1251

Y
yuyaozhi 已提交
1252 1253
**返回值:**

Z
zhongjianfei 已提交
1254 1255 1256
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
H
HuangXW 已提交
1257 1258 1259 1260 1261

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1262
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1263
| 401 | Invalid input parameter. |
M
m00512953 已提交
1264 1265 1266 1267
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1268

Y
yuyaozhi 已提交
1269 1270
**示例:**

1271
  ```ts
M
mingxihua 已提交
1272
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1273
  let connection = 1;
H
HuangXW 已提交
1274 1275 1276 1277

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
M
mingxihua 已提交
1278
        commRemote = null;
H
HuangXW 已提交
1279 1280 1281 1282
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
M
mingxihua 已提交
1283
        commRemote = null;
H
HuangXW 已提交
1284
        // 处理业务逻辑错误
M
mingxihua 已提交
1285
        console.error('disconnectServiceExtensionAbility failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
1286 1287
      });
  } catch (paramError) {
M
mingxihua 已提交
1288
    commRemote = null;
H
HuangXW 已提交
1289
    // 处理入参错误异常
M
mingxihua 已提交
1290
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1291
  }
1292 1293 1294 1295 1296 1297
  ```

## ServiceExtensionContext.startAbilityByCall

startAbilityByCall(want: Want): Promise&lt;Caller&gt;;

1298
启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。
1299

M
m00512953 已提交
1300 1301
使用规则:
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
M
m00512953 已提交
1302 1303
 - 跨应用场景下,目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
1304

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

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

1309 1310 1311 1312
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1313
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
1314 1315 1316 1317 1318 1319 1320

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |

H
HuangXW 已提交
1321 1322 1323 1324
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1325
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1326
| 401 | Invalid input parameter. |
M
m00512953 已提交
1327 1328 1329 1330 1331 1332
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
U
unknown 已提交
1333
| 16000017 | The previous ability is starting, wait start later.        |
M
m00512953 已提交
1334
| 16000050 | Internal Error. |
H
HuangXW 已提交
1335

1336 1337
**示例:**

1338
  后台启动:
H
HuangXW 已提交
1339

1340
  ```ts
M
mingxihua 已提交
1341
  let caller;
1342 1343

  // 后台启动Ability,不配置parameters
M
mingxihua 已提交
1344
  let wantBackground = {
M
mingxihua 已提交
1345 1346 1347 1348
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
1349
  };
H
HuangXW 已提交
1350 1351 1352 1353 1354

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // 执行正常业务
1355
        caller = obj;
H
HuangXW 已提交
1356 1357 1358
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
1359
        console.error('startAbilityByCall failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
1360 1361 1362
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
1363
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1364 1365 1366 1367 1368
  }
  ```

  前台启动:

1369
  ```ts
M
mingxihua 已提交
1370
  let caller;
1371

M
mingxihua 已提交
1372
  // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
M
mingxihua 已提交
1373
  let wantForeground = {
M
mingxihua 已提交
1374 1375 1376 1377
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
1378
      parameters: {
M
mingxihua 已提交
1379
        'ohos.aafwk.param.callAbilityToForeground': true
1380 1381
      }
  };
H
HuangXW 已提交
1382 1383

  try {
1384
    this.context.startAbilityByCall(wantForeground)
H
HuangXW 已提交
1385 1386
      .then((obj) => {
        // 执行正常业务
1387
        caller = obj;
H
HuangXW 已提交
1388 1389 1390
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
M
mingxihua 已提交
1391
        console.error('startAbilityByCall failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
H
HuangXW 已提交
1392 1393 1394
      });
  } catch (paramError) {
    // 处理入参错误异常
M
mingxihua 已提交
1395
    console.error('error.code: ${JSON.stringify(paramError.code)}, error.message: ${JSON.stringify(paramError.message)}');
H
HuangXW 已提交
1396
  }
M
m00512953 已提交
1397
  ```