js-apis-inner-application-serviceExtensionContext.md 59.4 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 13 14 15 16 17
## 导入模块

```ts
import common from '@ohos.app.ability.common';
```

18
## 使用说明
Y
yuyaozhi 已提交
19

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

22
```ts
H
HuangXW 已提交
23 24
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';

M
mingxihua 已提交
25
  let context;
M
mingxihua 已提交
26
  let commRemote; // 断开连接时需要释放
27
  class EntryAbility extends ServiceExtensionAbility {
H
HuangXW 已提交
28
    onCreate() {
Z
zhongjianfei 已提交
29
      context = this.context; // 获取ServiceExtensionContext
H
HuangXW 已提交
30
    }
31 32
  }
```
Y
yuyaozhi 已提交
33

H
HuangXW 已提交
34
## ServiceExtensionContext.startAbility
35

36
startAbility(want: Want, callback: AsyncCallback<void>): void;
37 38 39

启动Ability。

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

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

Y
yuyaozhi 已提交
44
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
45

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

H
HuangXW 已提交
51 52 53 54
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
55 56 57 58 59 60 61 62 63
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
64 65
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
66 67 68 69 70 71
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
72

Y
yuyaozhi 已提交
73 74
**示例:**

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

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

H
HuangXW 已提交
97
## ServiceExtensionContext.startAbility
98

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

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

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

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

Y
yuyaozhi 已提交
107
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
108

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

Y
yuyaozhi 已提交
114 115
**返回值:**

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

H
HuangXW 已提交
120 121 122 123
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
124 125 126 127 128 129 130 131 132
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
133 134
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
135 136 137 138 139 140
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
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) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
161
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
162 163 164
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
165
    console.error('error.code: ${paramError.code}, error.message: ${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 | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
191 192 193 194 195 196 197 198 199
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
200 201
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
202 203 204 205 206 207
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
208

209
**示例:**
H
HuangXW 已提交
210

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

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

## ServiceExtensionContext.startAbilityWithAccount

239
startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
240 241 242

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

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

Y
yuyaozhi 已提交
248 249
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

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

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

**参数:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
266 267 268 269 270 271 272 273 274
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
275 276
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
277 278 279 280 281 282
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
283

284 285
**示例:**

286
  ```ts
M
mingxihua 已提交
287
  let want = {
M
mingxihua 已提交
288 289 290
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
291
  };
M
mingxihua 已提交
292
  let accountId = 100;
293

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

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

Y
yuyaozhi 已提交
321 322
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

323 324 325 326 327 328 329 330
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

H
HuangXW 已提交
336 337 338 339
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
340 341 342 343 344 345 346 347 348
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
349 350
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
351 352 353 354 355 356
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
357

358 359
**示例:**

360
  ```ts
M
mingxihua 已提交
361
  let want = {
M
mingxihua 已提交
362 363 364
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
365
  };
M
mingxihua 已提交
366 367
  let accountId = 100;
  let options = {
H
HuangXW 已提交
368
    windowMode: 0
369
  };
H
HuangXW 已提交
370 371 372 373 374

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
375
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
376 377 378 379 380 381 382
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
383
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
384
  }
385 386 387 388 389
  ```


## ServiceExtensionContext.startAbilityWithAccount

390
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
391 392 393

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

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

Y
yuyaozhi 已提交
399 400
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

401 402 403 404 405 406 407 408
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

413 414
**返回值:**

Z
zhongjianfei 已提交
415 416 417
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
418

H
HuangXW 已提交
419 420 421 422
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
423 424 425 426 427 428 429 430 431
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden.        |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
432 433
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
434 435 436 437 438 439
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
440

441 442
**示例:**

443
  ```ts
M
mingxihua 已提交
444
  let want = {
M
mingxihua 已提交
445 446 447
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
448
  };
M
mingxihua 已提交
449 450
  let accountId = 100;
  let options = {
H
HuangXW 已提交
451
    windowMode: 0
452
  };
H
HuangXW 已提交
453 454 455 456 457 458 459 460 461

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // 执行正常业务
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
462
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
463 464 465
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
466
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
467
  }
468 469
  ```

470 471 472 473 474 475 476 477 478 479 480 481 482 483
## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
487 488 489 490
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
491 492
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
493
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
494 495 496 497
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
498 499
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
500 501 502 503
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
504

505 506
**示例:**

507
  ```ts
M
mingxihua 已提交
508
  let want = {
M
mingxihua 已提交
509 510 511
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
512
  };
H
HuangXW 已提交
513 514 515 516 517

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
518
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
519 520 521 522 523 524 525
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
526
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
527
  }
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

546 547
**返回值:**

Z
zhongjianfei 已提交
548 549 550
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
551

H
HuangXW 已提交
552 553 554 555
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
556 557
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
558
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
559 560 561 562
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
563 564
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
565 566 567 568
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
569

570 571
**示例:**

572
  ```ts
M
mingxihua 已提交
573
  let want = {
M
mingxihua 已提交
574 575 576
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
577
  };
H
HuangXW 已提交
578 579 580 581 582 583 584 585 586

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
587
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
588 589 590
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
591
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
592
  }
593 594 595 596 597 598 599 600
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
601 602 603 604 605
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
606 607 608 609 610 611 612 613 614

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

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

**参数:**

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

H
HuangXW 已提交
619 620 621 622
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
623 624
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
625
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
626 627 628 629
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
630 631
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
632 633 634 635
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
636 637


638 639
**示例:**

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

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

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
670 671 672 673 674
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
675 676 677 678 679 680 681 682 683

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

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

**参数:**

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

687 688
**返回值:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
697 698
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
699
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
700 701 702 703
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
704 705
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
706 707 708 709
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
710

711 712
**示例:**

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

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

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
758 759
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
760
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
761 762 763 764 765 766 767
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
768

769 770
**示例:**

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

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

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

**返回值:**

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

H
HuangXW 已提交
816 817 818 819
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
820 821
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
822
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
823 824 825 826 827 828 829
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
830

831 832
**示例:**

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

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

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
862 863 864 865 866
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
867 868 869 870 871 872 873 874 875

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

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

**参数:**

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

H
HuangXW 已提交
880 881 882 883
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
884 885
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
886
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
887 888 889 890 891 892 893
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
894

895 896
**示例:**

897
  ```ts
M
mingxihua 已提交
898
  let want = {
M
mingxihua 已提交
899 900 901
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
902
  };
M
mingxihua 已提交
903
  let accountId = 100;
H
HuangXW 已提交
904 905 906 907 908

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
909
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code, error.message: ${error.message}');
H
HuangXW 已提交
910 911 912 913 914 915 916
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
917
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
918
  }
919 920 921 922 923 924 925 926
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
927 928 929 930 931
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
932 933 934 935 936 937 938 939 940

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
946 947 948
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
949

H
HuangXW 已提交
950 951 952 953
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
954 955
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
956
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
957 958 959 960 961 962 963
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
964

965 966
**示例:**

967
  ```ts
M
mingxihua 已提交
968
  let want = {
M
mingxihua 已提交
969 970 971
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
972
  };
M
mingxihua 已提交
973
  let accountId = 100;
H
HuangXW 已提交
974 975 976 977 978 979 980 981 982

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
983
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
984 985 986
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
987
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
988
  }
989
  ```
990

Y
yuyaozhi 已提交
991
## ServiceExtensionContext.terminateSelf
992

993
terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
994

995
停止Ability自身。
996

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

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

Y
yuyaozhi 已提交
1001
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1002

Z
zhongjianfei 已提交
1003 1004
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
Y
yuyaozhi 已提交
1005
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1006

H
HuangXW 已提交
1007 1008 1009 1010
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1011 1012 1013 1014 1015 1016 1017 1018
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
1019

Y
yuyaozhi 已提交
1020 1021
**示例:**

1022
  ```ts
H
HuangXW 已提交
1023 1024 1025
  this.context.terminateSelf((error) => {
    if (error.code) {
      // 处理业务逻辑错误
Z
zhoujun62 已提交
1026
      console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1027 1028 1029 1030
      return;
    }
    // 执行正常业务
    console.log('terminateSelf succeed');
1031
  });
1032 1033
  ```

Y
yuyaozhi 已提交
1034
## ServiceExtensionContext.terminateSelf
1035

1036
terminateSelf(): Promise&lt;void&gt;;
1037

Y
yuyaozhi 已提交
1038
停止ability自身。通过Promise返回结果。
1039

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

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

Y
yuyaozhi 已提交
1044
**返回值:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1045

Z
zhongjianfei 已提交
1046 1047 1048
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1049

H
HuangXW 已提交
1050 1051 1052 1053
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1054 1055 1056 1057 1058 1059 1060 1061
| 16000001 | The specified ability does not exist. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000011 | The context does not exist.        |
| 16000050 | Internal error. |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
1062

Y
yuyaozhi 已提交
1063 1064
**示例:**

1065
  ```ts
1066
  this.context.terminateSelf().then((data) => {
H
HuangXW 已提交
1067 1068
    // 执行正常业务
    console.log('terminateSelf succeed');
1069
  }).catch((error) => {
H
HuangXW 已提交
1070
    // 处理业务逻辑错误
Z
zhoujun62 已提交
1071
    console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
1072
  });
1073 1074
  ```

H
HuangXW 已提交
1075
## ServiceExtensionContext.connectServiceExtensionAbility
1076

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

1079
将一个Ability与服务类型的Ability绑定。
1080

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

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

Y
yuyaozhi 已提交
1085
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1086

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

Y
yuyaozhi 已提交
1092 1093
**返回值:**

Z
zhongjianfei 已提交
1094 1095 1096
| 类型 | 说明 |
| -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 |
1097

H
HuangXW 已提交
1098 1099 1100 1101
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1102 1103 1104
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1105
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1106 1107 1108 1109
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
H
huangshiwei 已提交
1110
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1111
| 16000050 | Internal error. |
H
HuangXW 已提交
1112

H
huangshiwei 已提交
1113 1114
以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1115 1116
**示例:**

1117
  ```ts
M
mingxihua 已提交
1118
  let want = {
M
mingxihua 已提交
1119 1120
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
1121
  };
M
mingxihua 已提交
1122
  let options = {
M
mingxihua 已提交
1123
    onConnect(elementName, remote) { 
M
mingxihua 已提交
1124
      commRemote = remote;
M
mingxihua 已提交
1125 1126
      console.log('----------- onConnect -----------'); 
    },
1127
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
M
mingxihua 已提交
1128
    onFailed(code) { console.error('----------- onFailed -----------') }
M
mingxihua 已提交
1129
  };
H
HuangXW 已提交
1130

M
mingxihua 已提交
1131
  let connection = null;
H
HuangXW 已提交
1132 1133 1134 1135
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1136
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1137
  }
1138 1139
  ```

H
HuangXW 已提交
1140
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1141

H
HuangXW 已提交
1142
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1143 1144 1145

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

Y
yuyaozhi 已提交
1146 1147
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

1148 1149 1150 1151 1152 1153 1154 1155
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1156
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1157
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
Y
yuyaozhi 已提交
1158
| options | ConnectOptions | 是 | 远端对象实例。 |
1159 1160 1161 1162 1163 1164 1165

**返回值:**

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

H
HuangXW 已提交
1166 1167 1168 1169
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1170 1171 1172
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1173
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1174 1175 1176 1177
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
H
huangshiwei 已提交
1178
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1179
| 16000050 | Internal error. |
H
HuangXW 已提交
1180

H
huangshiwei 已提交
1181 1182
以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

1183 1184
**示例:**

1185
  ```ts
M
mingxihua 已提交
1186
  let want = {
M
mingxihua 已提交
1187 1188 1189
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1190
  };
M
mingxihua 已提交
1191 1192
  let accountId = 100;
  let options = {
M
mingxihua 已提交
1193
    onConnect(elementName, remote) { 
M
mingxihua 已提交
1194
      commRemote = remote;
M
mingxihua 已提交
1195 1196
      console.log('----------- onConnect -----------');
    },
M
mingxihua 已提交
1197 1198 1199
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
    onFailed(code) { console.log('----------- onFailed -----------'); }
  };
H
HuangXW 已提交
1200

M
mingxihua 已提交
1201
  let connection = null;
H
HuangXW 已提交
1202 1203 1204 1205
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1206
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1207
  }
1208
  ```
1209

H
HuangXW 已提交
1210
## ServiceExtensionContext.disconnectServiceExtensionAbility
1211

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

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

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

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

Y
yuyaozhi 已提交
1220
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1221

Z
zhongjianfei 已提交
1222 1223
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1224
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
Y
yuyaozhi 已提交
1225
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1226

H
HuangXW 已提交
1227 1228 1229 1230
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1231
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1232
| 16000050 | Internal error. |
H
HuangXW 已提交
1233

H
huangshiwei 已提交
1234 1235
以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1236 1237
**示例:**

1238
  ```ts
H
HuangXW 已提交
1239
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1240
  let connection = 1;
H
HuangXW 已提交
1241 1242 1243

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
M
mingxihua 已提交
1244
      commRemote = null;
H
HuangXW 已提交
1245 1246
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1247
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1248 1249 1250 1251
        return;
      }
      // 执行正常业务
      console.log('disconnectServiceExtensionAbility succeed');
1252
    });
H
HuangXW 已提交
1253
  } catch (paramError) {
M
mingxihua 已提交
1254
    commRemote = null;
H
HuangXW 已提交
1255
    // 处理入参错误异常
Z
zhoujun62 已提交
1256
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1257
  }
1258 1259
  ```

H
HuangXW 已提交
1260
## ServiceExtensionContext.disconnectServiceExtensionAbility
1261

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

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

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

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

Y
yuyaozhi 已提交
1270
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1271

Z
zhongjianfei 已提交
1272 1273
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1274
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1275

Y
yuyaozhi 已提交
1276 1277
**返回值:**

Z
zhongjianfei 已提交
1278 1279 1280
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
H
HuangXW 已提交
1281 1282 1283 1284 1285

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1286
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1287
| 16000050 | Internal error. |
H
HuangXW 已提交
1288

H
huangshiwei 已提交
1289 1290
以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

Y
yuyaozhi 已提交
1291 1292
**示例:**

1293
  ```ts
M
mingxihua 已提交
1294
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1295
  let connection = 1;
H
HuangXW 已提交
1296 1297 1298 1299

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
M
mingxihua 已提交
1300
        commRemote = null;
H
HuangXW 已提交
1301 1302 1303 1304
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
M
mingxihua 已提交
1305
        commRemote = null;
H
HuangXW 已提交
1306
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1307
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1308 1309
      });
  } catch (paramError) {
M
mingxihua 已提交
1310
    commRemote = null;
H
HuangXW 已提交
1311
    // 处理入参错误异常
Z
zhoujun62 已提交
1312
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1313
  }
1314 1315 1316 1317 1318 1319
  ```

## ServiceExtensionContext.startAbilityByCall

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

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

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

Y
yuyaozhi 已提交
1327 1328
**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION

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

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

1333 1334 1335 1336
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1337
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
1338 1339 1340 1341 1342 1343 1344

**返回值:**

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

H
HuangXW 已提交
1345 1346 1347 1348
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1349
| 16000001 | The specified ability does not exist. |
H
huangshiwei 已提交
1350
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1351
| 16000004 | Can not start invisible component. |
M
m00512953 已提交
1352
| 16000005 | Static permission denied. The specified process does not have the permission. |
H
huangshiwei 已提交
1353
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
1354
| 16000008 | The crowdtesting application expires. |
H
huangshiwei 已提交
1355
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
1356
| 16000050 | Internal error. |
H
huangshiwei 已提交
1357 1358 1359
| 16200001 | The caller has been released.        |

以上错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
H
HuangXW 已提交
1360

1361 1362
**示例:**

1363
  后台启动:
H
HuangXW 已提交
1364

1365
  ```ts
M
mingxihua 已提交
1366
  let caller;
1367 1368

  // 后台启动Ability,不配置parameters
M
mingxihua 已提交
1369
  let wantBackground = {
M
mingxihua 已提交
1370 1371 1372 1373
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
1374
  };
H
HuangXW 已提交
1375 1376 1377 1378 1379

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // 执行正常业务
1380
        caller = obj;
H
HuangXW 已提交
1381 1382 1383
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1384
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1385 1386 1387
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1388
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1389 1390 1391 1392 1393
  }
  ```

  前台启动:

1394
  ```ts
M
mingxihua 已提交
1395
  let caller;
1396

M
mingxihua 已提交
1397
  // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
M
mingxihua 已提交
1398
  let wantForeground = {
M
mingxihua 已提交
1399 1400 1401 1402
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
1403
      parameters: {
M
mingxihua 已提交
1404
        'ohos.aafwk.param.callAbilityToForeground': true
1405 1406
      }
  };
H
HuangXW 已提交
1407 1408

  try {
1409
    this.context.startAbilityByCall(wantForeground)
H
HuangXW 已提交
1410 1411
      .then((obj) => {
        // 执行正常业务
1412
        caller = obj;
H
HuangXW 已提交
1413 1414 1415
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1416
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1417 1418 1419
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1420
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1421
  }
X
add doc  
xinking129 已提交
1422
  ```
1423
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1424

Y
yuyaozhi 已提交
1425
startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
U
unknown 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 需要启动Ability的want信息。 |
| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |

**错误码:**

U
unknown 已提交
1447 1448
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

**示例:**

  ```ts
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
  this.context.startRecentAbility(want, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
  ```
1488
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1489

Y
yuyaozhi 已提交
1490
startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
U
unknown 已提交
1491 1492

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
U
unknown 已提交
1493
当开发者需要携带启动参数时可以选择此API。
U
unknown 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 需要启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |

**错误码:**

U
unknown 已提交
1514 1515
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

**示例:**

  ```ts
let want = {
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0
};

try {
  this.context.startRecentAbility(want, options, (err) => {
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed failed, code is ${err.code}, message is ${err.message}`);
}
  ```
1559
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1560

Y
yuyaozhi 已提交
1561
startRecentAbility(want: Want, options?: StartOptions): Promise\<void>;
U
unknown 已提交
1562

U
unknown 已提交
1563 1564
启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。
当开发者期望启动结果以Promise形式返回时可以选择此API。
U
unknown 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 需要启动Ability的want信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |

**错误码:**

U
unknown 已提交
1584 1585
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
| 16000005 | The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
| 16000008 | The crowdtesting application expires. |
| 16000009 | An ability cannot be started or stopped in Wukong mode. |
| 16000010 | The call with the continuation flag is forbidden. |
| 16000011 | The context does not exist. |
| 16000050 | Internal error. |
| 16000053 | The ability is not on the top of the UI. |
| 16000055 | Installation-free timed out. |
| 16200001 | The caller has been released. |

**示例:**

  ```ts
let want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let options = {
  windowMode: 0,
};

try {
  this.context.startRecentAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startRecentAbility succeed');
    })
    .catch((err) => {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
  console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
}
X
xinking129 已提交
1627
  ```
X
add doc  
xinking129 已提交
1628

X
xinking129 已提交
1629
## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>
X
add doc  
xinking129 已提交
1630 1631 1632 1633 1634 1635 1636

startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;;

根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。

使用规则:
 - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION``ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限
X
xinking129 已提交
1637 1638 1639
 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
X
add doc  
xinking129 已提交
1640

Y
yuyaozhi 已提交
1641 1642
**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

X
add doc  
xinking129 已提交
1643 1644 1645 1646 1647 1648 1649 1650
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
X
xinking129 已提交
1651
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
X
xinking129 已提交
1652
| accountId | number | 是 | 系统帐号的帐号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
X
add doc  
xinking129 已提交
1653 1654 1655 1656 1657 1658 1659 1660 1661

**返回值:**

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

**错误码:**

X
xinking129 已提交
1662 1663
以下错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)

X
add doc  
xinking129 已提交
1664 1665
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1666
| 16000001 | The specified ability does not exist. |
X
add doc  
xinking129 已提交
1667
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1668
| 16000004 | Can not start invisible component. |
X
add doc  
xinking129 已提交
1669 1670
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
1671
| 16000008 | The crowdtesting application expires. |
X
add doc  
xinking129 已提交
1672
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
1673 1674 1675
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 16000050 | Internal error. |
X
add doc  
xinking129 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
| 16200001 | The caller has been released.        |

**示例:**

  ```ts
  let caller;

  // 系统账号的账号ID, -1表示当前激活用户
  let accountId = -1;

  // 指定启动的Ability
  let want = {
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
Y
yuyaozhi 已提交
1690 1691
      abilityName: 'EntryAbility',
      deviceId: '',
X
xinking129 已提交
1692 1693 1694 1695
      parameters: {
        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
        'ohos.aafwk.param.callAbilityToForeground': true
      }
X
add doc  
xinking129 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
  };

  try {
    this.context.startAbilityByCallWithAccount(want, accountId)
      .then((obj) => {
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCallWithAccount succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
        console.error('startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
  }
M
m00512953 已提交
1712
  ```