js-apis-inner-application-serviceExtensionContext.md 72.8 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
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
L
liuliu 已提交
24
  import rpc from '@ohos.rpc';
H
HuangXW 已提交
25

L
liuliu 已提交
26
  let commRemote: rpc.IRemoteObject; // 断开连接时需要释放
27
  class EntryAbility extends ServiceExtensionAbility {
H
HuangXW 已提交
28
    onCreate() {
L
liuliu 已提交
29
        let 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
L
liuliu 已提交
76 77 78 79
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
80 81
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
H
HuangXW 已提交
82 83 84
  };

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

H
HuangXW 已提交
100
## ServiceExtensionContext.startAbility
101

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

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

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

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

Y
yuyaozhi 已提交
110
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
111

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

Y
yuyaozhi 已提交
117 118
**返回值:**

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

H
HuangXW 已提交
123 124 125 126
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
127 128 129 130 131 132 133 134 135
| 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 已提交
136 137
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
138 139 140 141 142 143
| 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 已提交
144

Y
yuyaozhi 已提交
145 146
**示例:**

147
  ```ts
L
liuliu 已提交
148 149 150 151 152
  import Want from '@ohos.app.ability.Want';
  import StartOptions from '@ohos.app.ability.StartOptions';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
153 154
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
H
HuangXW 已提交
155
  };
L
liuliu 已提交
156
  let options: StartOptions = {
H
HuangXW 已提交
157 158
  	windowMode: 0,
  };
159

H
HuangXW 已提交
160 161
  try {
    this.context.startAbility(want, options)
L
liuliu 已提交
162
      .then((data: void) => {
H
HuangXW 已提交
163 164 165
        // 执行正常业务
        console.log('startAbility succeed');
      })
L
liuliu 已提交
166
      .catch((error: BusinessError) => {
H
HuangXW 已提交
167
        // 处理业务逻辑错误
Z
zhoujun62 已提交
168
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
169 170 171
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
172
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
173
  }
174
  ```
Y
yuyaozhi 已提交
175

H
HuangXW 已提交
176
## ServiceExtensionContext.startAbility
177 178 179

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

U
unknown 已提交
180
启动Ability,结果以Callback的形式返回。
181 182 183 184 185 186 187 188 189

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

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

**参数:**

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

H
HuangXW 已提交
194 195 196 197
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
198 199 200 201 202 203 204 205 206
| 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 已提交
207 208
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
209 210 211 212 213 214
| 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 已提交
215

216
**示例:**
H
HuangXW 已提交
217

218
  ```ts
L
liuliu 已提交
219 220 221 222 223
  import Want from '@ohos.app.ability.Want';
  import StartOptions from '@ohos.app.ability.StartOptions';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
224 225 226
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
227
  };
L
liuliu 已提交
228
  let options: StartOptions = {
H
HuangXW 已提交
229
    windowMode: 0
230
  };
H
HuangXW 已提交
231 232

  try {
L
liuliu 已提交
233
    this.context.startAbility(want, options, (error: BusinessError) => {
H
HuangXW 已提交
234 235
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
236
        console.error('startAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
237 238 239 240 241 242 243
        return;
      }
      // 执行正常业务
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
244
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
245
  }
246 247 248 249
  ```

## ServiceExtensionContext.startAbilityWithAccount

250
startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
251 252 253

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

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

Y
yuyaozhi 已提交
259 260
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

261 262 263 264 265 266 267 268
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

H
HuangXW 已提交
273 274 275 276
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
277 278 279 280 281 282 283 284 285
| 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 已提交
286 287
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
288 289 290 291 292 293
| 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 已提交
294

295 296
**示例:**

297
  ```ts
L
liuliu 已提交
298 299 300 301
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
302 303 304
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
305
  };
M
mingxihua 已提交
306
  let accountId = 100;
307

H
HuangXW 已提交
308
  try {
L
liuliu 已提交
309
    this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => {
H
HuangXW 已提交
310 311
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
312
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
313 314 315 316 317 318 319
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
320
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
321 322
  }
  ```
323 324 325 326 327 328 329

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

Y
yuyaozhi 已提交
335 336
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

337 338 339 340 341 342 343 344
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

H
HuangXW 已提交
350 351 352 353
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
354 355 356 357 358 359 360 361 362
| 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 已提交
363 364
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
365 366 367 368 369 370
| 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 已提交
371

372 373
**示例:**

374
  ```ts
L
liuliu 已提交
375 376 377 378 379
  import Want from '@ohos.app.ability.Want';
  import StartOptions from '@ohos.app.ability.StartOptions';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
380 381 382
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
383
  };
M
mingxihua 已提交
384
  let accountId = 100;
L
liuliu 已提交
385
  let options: StartOptions = {
H
HuangXW 已提交
386
    windowMode: 0
387
  };
H
HuangXW 已提交
388 389

  try {
L
liuliu 已提交
390
    this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => {
H
HuangXW 已提交
391 392
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
393
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
394 395 396 397 398 399 400
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
401
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
402
  }
403 404 405 406 407
  ```


## ServiceExtensionContext.startAbilityWithAccount

408
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
409 410 411

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

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

Y
yuyaozhi 已提交
417 418
**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

419 420 421 422 423 424 425 426
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

431 432
**返回值:**

Z
zhongjianfei 已提交
433 434 435
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
436

H
HuangXW 已提交
437 438 439 440
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
441 442 443 444 445 446 447 448 449
| 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 已提交
450 451
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
452 453 454 455 456 457
| 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 已提交
458

459 460
**示例:**

461
  ```ts
L
liuliu 已提交
462 463 464 465 466
  import Want from '@ohos.app.ability.Want';
  import StartOptions from '@ohos.app.ability.StartOptions';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
467 468 469
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
470
  };
M
mingxihua 已提交
471
  let accountId = 100;
L
liuliu 已提交
472
  let options: StartOptions = {
H
HuangXW 已提交
473
    windowMode: 0
474
  };
H
HuangXW 已提交
475 476 477

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
L
liuliu 已提交
478
      .then((data: void) => {
H
HuangXW 已提交
479 480 481
        // 执行正常业务
        console.log('startAbilityWithAccount succeed');
      })
L
liuliu 已提交
482
      .catch((error: BusinessError) => {
H
HuangXW 已提交
483
        // 处理业务逻辑错误
Z
zhoujun62 已提交
484
        console.error('startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
485 486 487
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
488
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
489
  }
490 491
  ```

492 493 494 495 496 497 498 499 500 501 502 503 504 505
## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
509 510 511 512
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
513 514
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
515
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
516 517 518 519
| 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 已提交
520 521
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
522 523 524 525
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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

527 528
**示例:**

529
  ```ts
L
liuliu 已提交
530 531 532 533
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
534 535 536
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
537
  };
H
HuangXW 已提交
538 539

  try {
L
liuliu 已提交
540
    this.context.startServiceExtensionAbility(want, (error: BusinessError) => {
H
HuangXW 已提交
541 542
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
543
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
544 545 546 547 548 549 550
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
551
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
552
  }
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

571 572
**返回值:**

Z
zhongjianfei 已提交
573 574 575
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
576

H
HuangXW 已提交
577 578 579 580
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
581 582
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
583
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
584 585 586 587
| 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 已提交
588 589
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
590 591 592 593
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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

595 596
**示例:**

597
  ```ts
L
liuliu 已提交
598 599 600 601
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
602 603 604
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
605
  };
H
HuangXW 已提交
606 607 608

  try {
    this.context.startServiceExtensionAbility(want)
L
liuliu 已提交
609
      .then((data: void) => {
H
HuangXW 已提交
610 611 612
        // 执行正常业务
        console.log('startServiceExtensionAbility succeed');
      })
L
liuliu 已提交
613
      .catch((error: BusinessError) => {
H
HuangXW 已提交
614
        // 处理业务逻辑错误
Z
zhoujun62 已提交
615
        console.error('startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
616 617 618
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
619
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
620
  }
621 622 623 624 625 626 627 628
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
629 630 631 632 633
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
634 635 636 637 638 639 640 641 642

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

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

**参数:**

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

H
HuangXW 已提交
647 648 649 650
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
651 652
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
653
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
654 655 656 657
| 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 已提交
658 659
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
660 661 662 663
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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


666 667
**示例:**

668
  ```ts
L
liuliu 已提交
669 670 671 672
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
673 674 675
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
676
  };
M
mingxihua 已提交
677
  let accountId = 100;
H
HuangXW 已提交
678 679

  try {
L
liuliu 已提交
680
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
H
HuangXW 已提交
681 682
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
683
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
684 685 686 687 688 689 690
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
691
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
692
  }
693 694 695 696 697 698 699 700
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
701 702 703 704 705
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
706 707 708 709 710 711 712 713 714

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

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

**参数:**

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

718 719
**返回值:**

Z
zhongjianfei 已提交
720 721 722
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
723

H
HuangXW 已提交
724 725 726 727
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
728 729
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
730
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
731 732 733 734
| 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 已提交
735 736
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
H
huangshiwei 已提交
737 738 739 740
| 16000050 | Internal error. |
| 16200001 | The caller has been released. |

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

742 743
**示例:**

744
  ```ts
L
liuliu 已提交
745 746 747 748
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
749 750 751
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
752
  };
M
mingxihua 已提交
753
  let accountId = 100;
H
HuangXW 已提交
754 755 756

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
L
liuliu 已提交
757
      .then((data: void) => {
H
HuangXW 已提交
758 759 760
        // 执行正常业务
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
L
liuliu 已提交
761
      .catch((error: BusinessError) => {
H
HuangXW 已提交
762
        // 处理业务逻辑错误
Z
zhoujun62 已提交
763
        console.error('startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
764 765 766
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
767
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
768
  }
769 770
  ```

D
donglin 已提交
771 772
## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>

Y
yuyaozhi 已提交
773
startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void;
D
donglin 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用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&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码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.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import extension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends extension {
L
liuliu 已提交
822
  onCreate(want: Want) {
D
donglin 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.log('startAbilityAsCaller success.');
      }
    })
  }
}

```

X
xieqiongyang 已提交
842
## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
D
donglin 已提交
843

1
Bugfix  
18870373690 已提交
844
startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
D
donglin 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用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信息。 |
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 16000001 | The specified ability does not exist. |
| 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. |
| 16000011 | The context does not exist.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import extension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';
L
liuliu 已提交
890
import StartOptions from '@ohos.app.ability.StartOptions';
D
donglin 已提交
891 892

export default class EntryAbility extends extension {
L
liuliu 已提交
893
  onCreate(want: Want) {
D
donglin 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    let option: StartOptions = {
      displayId: 0
    }

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option, (err) => {
      if (err && err.code != 0) {
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      } else {
        console.log('startAbilityAsCaller success.');
      }
    })
  }
}

```

## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>

1
Bugfix  
18870373690 已提交
919
startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>;
D
donglin 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936

使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。

使用规则:
 - 调用方应用位于后台时,使用该接口启动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信息。 |
1
Bugfix  
18870373690 已提交
937
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
D
donglin 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |

**错误码:**

| 错误码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.        |
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 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)

**示例:**

```ts
import extension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';
L
liuliu 已提交
972 973
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';
D
donglin 已提交
974 975

export default class EntryAbility extends extension {
L
liuliu 已提交
976
  onCreate(want: Want) {
D
donglin 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    // want包含启动该应用的Caller信息
    let localWant: Want = want;
    localWant.bundleName = 'com.example.demo';
    localWant.moduleName = 'entry';
    localWant.abilityName = 'TestAbility';

    let option: StartOptions = {
      displayId: 0
    }

    // 使用启动方的Caller身份信息启动新Ability
    this.context.startAbilityAsCaller(localWant, option)
      .then(() => {
        console.log('startAbilityAsCaller success.');
      })
L
liuliu 已提交
992
      .catch((err: BusinessError) => {
D
donglin 已提交
993 994 995 996 997 998 999
        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
      })
  }
}

```

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
1017 1018 1019 1020
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1021 1022
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1023
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
1024 1025 1026 1027 1028 1029 1030
| 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 已提交
1031

1032 1033
**示例:**

1034
  ```ts
L
liuliu 已提交
1035 1036 1037 1038
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1039 1040 1041
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1042
  };
H
HuangXW 已提交
1043 1044

  try {
L
liuliu 已提交
1045
    this.context.stopServiceExtensionAbility(want, (error: BusinessError) => {
H
HuangXW 已提交
1046 1047
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1048
        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1049 1050 1051 1052 1053 1054 1055
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1056
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1057
  }
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
1078 1079 1080
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1081

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1086 1087
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1088
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
1089 1090 1091 1092 1093 1094 1095
| 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 已提交
1096

1097 1098
**示例:**

1099
  ```ts
L
liuliu 已提交
1100 1101 1102 1103
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1104 1105 1106
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1107
  };
H
HuangXW 已提交
1108 1109 1110

  try {
    this.context.stopServiceExtensionAbility(want)
L
liuliu 已提交
1111
      .then(() => {
H
HuangXW 已提交
1112 1113 1114
        // 执行正常业务
        console.log('stopServiceExtensionAbility succeed');
      })
L
liuliu 已提交
1115
      .catch((error: BusinessError) => {
H
HuangXW 已提交
1116
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1117
        console.error('stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1118 1119 1120
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1121
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1122
  }
1123 1124 1125 1126 1127 1128 1129 1130
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
1131 1132 1133 1134 1135
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1136 1137 1138 1139 1140 1141 1142 1143 1144

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

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

**参数:**

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

H
HuangXW 已提交
1149 1150 1151 1152
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1153 1154
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1155
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
1156 1157 1158 1159 1160 1161 1162
| 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 已提交
1163

1164 1165
**示例:**

1166
  ```ts
L
liuliu 已提交
1167 1168 1169 1170
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1171 1172 1173
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1174
  };
M
mingxihua 已提交
1175
  let accountId = 100;
H
HuangXW 已提交
1176 1177

  try {
L
liuliu 已提交
1178
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
H
HuangXW 已提交
1179 1180
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1181
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code, error.message: ${error.message}');
H
HuangXW 已提交
1182 1183 1184 1185 1186 1187 1188
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1189
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1190
  }
1191 1192 1193 1194 1195 1196 1197 1198
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

Y
yuyaozhi 已提交
1199 1200 1201 1202 1203
> **说明:**
> 
> 当accountId为当前用户时,不需要校验该权限。

**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1204 1205 1206 1207 1208 1209 1210 1211 1212

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
1218 1219 1220
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1221

H
HuangXW 已提交
1222 1223 1224 1225
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1226 1227
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1228
| 16000004 | Can not start invisible component. |
H
huangshiwei 已提交
1229 1230 1231 1232 1233 1234 1235
| 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 已提交
1236

1237 1238
**示例:**

1239
  ```ts
L
liuliu 已提交
1240 1241 1242 1243
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1244 1245 1246
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1247
  };
M
mingxihua 已提交
1248
  let accountId = 100;
H
HuangXW 已提交
1249 1250 1251

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
L
liuliu 已提交
1252
      .then(() => {
H
HuangXW 已提交
1253 1254 1255
        // 执行正常业务
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
L
liuliu 已提交
1256
      .catch((error: BusinessError) => {
H
HuangXW 已提交
1257
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1258
        console.error('stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1259 1260 1261
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1262
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1263
  }
1264
  ```
1265

Y
yuyaozhi 已提交
1266
## ServiceExtensionContext.terminateSelf
1267

1268
terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1269

1270
停止Ability自身。
1271

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

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

Y
yuyaozhi 已提交
1276
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1277

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

H
HuangXW 已提交
1282 1283 1284 1285
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1286 1287 1288 1289 1290 1291 1292 1293
| 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 已提交
1294

Y
yuyaozhi 已提交
1295 1296
**示例:**

1297
  ```ts
L
liuliu 已提交
1298 1299 1300
  import { BusinessError } from '@ohos.base';

  this.context.terminateSelf((error: BusinessError) => {
H
HuangXW 已提交
1301 1302
    if (error.code) {
      // 处理业务逻辑错误
Z
zhoujun62 已提交
1303
      console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1304 1305 1306 1307
      return;
    }
    // 执行正常业务
    console.log('terminateSelf succeed');
1308
  });
1309 1310
  ```

Y
yuyaozhi 已提交
1311
## ServiceExtensionContext.terminateSelf
1312

1313
terminateSelf(): Promise&lt;void&gt;;
1314

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

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

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

Y
yuyaozhi 已提交
1321
**返回值:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1322

Z
zhongjianfei 已提交
1323 1324 1325
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1326

H
HuangXW 已提交
1327 1328 1329 1330
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
H
huangshiwei 已提交
1331 1332 1333 1334 1335 1336 1337 1338
| 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 已提交
1339

Y
yuyaozhi 已提交
1340 1341
**示例:**

1342
  ```ts
L
liuliu 已提交
1343 1344 1345
  import { BusinessError } from '@ohos.base';

  this.context.terminateSelf().then(() => {
H
HuangXW 已提交
1346 1347
    // 执行正常业务
    console.log('terminateSelf succeed');
L
liuliu 已提交
1348
  }).catch((error: BusinessError) => {
H
HuangXW 已提交
1349
    // 处理业务逻辑错误
Z
zhoujun62 已提交
1350
    console.error('terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}');
1351
  });
1352 1353
  ```

H
HuangXW 已提交
1354
## ServiceExtensionContext.connectServiceExtensionAbility
1355

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

1358
将一个Ability与服务类型的Ability绑定。
1359

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

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

Y
yuyaozhi 已提交
1364
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1365

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

Y
yuyaozhi 已提交
1371 1372
**返回值:**

Z
zhongjianfei 已提交
1373 1374 1375
| 类型 | 说明 |
| -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 |
1376

H
HuangXW 已提交
1377 1378 1379 1380
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1381 1382 1383
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1384
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1385 1386 1387 1388
| 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 已提交
1389
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1390
| 16000050 | Internal error. |
H
HuangXW 已提交
1391

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

Y
yuyaozhi 已提交
1394 1395
**示例:**

1396
  ```ts
L
liuliu 已提交
1397 1398 1399 1400 1401 1402
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
  import common from '@ohos.app.ability.common';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1403 1404
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
1405
  };
L
liuliu 已提交
1406 1407
  let options: common.ConnectOptions = {
    onConnect(elementName, remote) {
M
mingxihua 已提交
1408
      commRemote = remote;
L
liuliu 已提交
1409
      console.log('----------- onConnect -----------');
M
mingxihua 已提交
1410
    },
1411
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
M
mingxihua 已提交
1412
    onFailed(code) { console.error('----------- onFailed -----------') }
M
mingxihua 已提交
1413
  };
L
liuliu 已提交
1414
  let connection: number;
H
HuangXW 已提交
1415 1416 1417 1418
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1419
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1420
  }
1421 1422
  ```

H
HuangXW 已提交
1423
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1424

H
HuangXW 已提交
1425
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1426 1427 1428

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

Y
yuyaozhi 已提交
1429 1430
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS

1431 1432 1433 1434 1435 1436 1437 1438
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1439
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1440
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
Y
yuyaozhi 已提交
1441
| options | ConnectOptions | 是 | 远端对象实例。 |
1442 1443 1444 1445 1446 1447 1448

**返回值:**

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

H
HuangXW 已提交
1449 1450 1451 1452
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1453 1454 1455
| 16000001 | The specified ability does not exist. |
| 16000002 | Incorrect ability type. |
| 16000004 | Can not start invisible component. |
Y
yuyaozhi 已提交
1456
| 16000005 | The specified process does not have the permission. |
Y
yuyaozhi 已提交
1457 1458 1459 1460
| 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 已提交
1461
| 16000011 | The context does not exist.        |
Y
yuyaozhi 已提交
1462
| 16000050 | Internal error. |
H
HuangXW 已提交
1463

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

1466 1467
**示例:**

1468
  ```ts
L
liuliu 已提交
1469 1470 1471 1472 1473 1474
  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
  import common from '@ohos.app.ability.common';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let want: Want = {
M
mingxihua 已提交
1475 1476 1477
    deviceId: '',
    bundleName: 'com.example.myapplication',
    abilityName: 'EntryAbility'
1478
  };
M
mingxihua 已提交
1479
  let accountId = 100;
L
liuliu 已提交
1480
  let options: common.ConnectOptions = {
M
mingxihua 已提交
1481
    onConnect(elementName, remote) { 
M
mingxihua 已提交
1482
      commRemote = remote;
M
mingxihua 已提交
1483 1484
      console.log('----------- onConnect -----------');
    },
M
mingxihua 已提交
1485 1486 1487
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------'); },
    onFailed(code) { console.log('----------- onFailed -----------'); }
  };
L
liuliu 已提交
1488
  let connection: number;
H
HuangXW 已提交
1489 1490 1491 1492
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1493
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1494
  }
1495
  ```
1496

H
HuangXW 已提交
1497
## ServiceExtensionContext.disconnectServiceExtensionAbility
1498

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

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

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

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

Y
yuyaozhi 已提交
1507
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1508

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

H
HuangXW 已提交
1514 1515 1516 1517
**错误码:**

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

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

Y
yuyaozhi 已提交
1523 1524
**示例:**

1525
  ```ts
L
liuliu 已提交
1526 1527
  import { BusinessError } from '@ohos.base';

H
HuangXW 已提交
1528
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1529
  let connection = 1;
H
HuangXW 已提交
1530 1531

  try {
L
liuliu 已提交
1532
    this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => {
M
mingxihua 已提交
1533
      commRemote = null;
H
HuangXW 已提交
1534 1535
      if (error.code) {
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1536
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1537 1538 1539 1540
        return;
      }
      // 执行正常业务
      console.log('disconnectServiceExtensionAbility succeed');
1541
    });
H
HuangXW 已提交
1542
  } catch (paramError) {
M
mingxihua 已提交
1543
    commRemote = null;
H
HuangXW 已提交
1544
    // 处理入参错误异常
Z
zhoujun62 已提交
1545
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1546
  }
1547 1548
  ```

H
HuangXW 已提交
1549
## ServiceExtensionContext.disconnectServiceExtensionAbility
1550

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

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

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

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

Y
yuyaozhi 已提交
1559
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1560

Z
zhongjianfei 已提交
1561 1562
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1563
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1564

Y
yuyaozhi 已提交
1565 1566
**返回值:**

Z
zhongjianfei 已提交
1567 1568 1569
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
H
HuangXW 已提交
1570 1571 1572 1573 1574

**错误码:**

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

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

Y
yuyaozhi 已提交
1580 1581
**示例:**

1582
  ```ts
L
liuliu 已提交
1583 1584
  import { BusinessError } from '@ohos.base';

M
mingxihua 已提交
1585
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1586
  let connection = 1;
H
HuangXW 已提交
1587 1588 1589

  try {
    this.context.disconnectServiceExtensionAbility(connection)
L
liuliu 已提交
1590
      .then(() => {
M
mingxihua 已提交
1591
        commRemote = null;
H
HuangXW 已提交
1592 1593 1594
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
L
liuliu 已提交
1595
      .catch((error: BusinessError) => {
M
mingxihua 已提交
1596
        commRemote = null;
H
HuangXW 已提交
1597
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1598
        console.error('disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1599 1600
      });
  } catch (paramError) {
M
mingxihua 已提交
1601
    commRemote = null;
H
HuangXW 已提交
1602
    // 处理入参错误异常
Z
zhoujun62 已提交
1603
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1604
  }
1605 1606 1607 1608 1609 1610
  ```

## ServiceExtensionContext.startAbilityByCall

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

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

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

Y
yuyaozhi 已提交
1618 1619
**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION

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

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

1624 1625 1626 1627
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
m00512953 已提交
1628
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
1629 1630 1631 1632 1633 1634 1635

**返回值:**

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

H
HuangXW 已提交
1636 1637 1638 1639
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1640
| 16000001 | The specified ability does not exist. |
H
huangshiwei 已提交
1641
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1642
| 16000004 | Can not start invisible component. |
M
m00512953 已提交
1643
| 16000005 | Static permission denied. The specified process does not have the permission. |
H
huangshiwei 已提交
1644
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
1645
| 16000008 | The crowdtesting application expires. |
H
huangshiwei 已提交
1646
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
1647
| 16000050 | Internal error. |
H
huangshiwei 已提交
1648 1649 1650
| 16200001 | The caller has been released.        |

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

1652 1653
**示例:**

1654
  后台启动:
H
HuangXW 已提交
1655

1656
  ```ts
L
liuliu 已提交
1657 1658 1659 1660 1661
  import { Caller } from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let caller: Caller;
1662 1663

  // 后台启动Ability,不配置parameters
L
liuliu 已提交
1664
  let wantBackground: Want = {
M
mingxihua 已提交
1665 1666 1667 1668
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: ''
1669
  };
H
HuangXW 已提交
1670 1671 1672

  try {
    this.context.startAbilityByCall(wantBackground)
L
liuliu 已提交
1673
      .then((obj: Caller) => {
H
HuangXW 已提交
1674
        // 执行正常业务
1675
        caller = obj;
H
HuangXW 已提交
1676
        console.log('startAbilityByCall succeed');
L
liuliu 已提交
1677
      }).catch((error: BusinessError) => {
H
HuangXW 已提交
1678
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1679
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1680 1681 1682
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1683
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1684 1685 1686 1687 1688
  }
  ```

  前台启动:

1689
  ```ts
L
liuliu 已提交
1690 1691 1692 1693 1694
  import { Caller } from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import { BusinessError } from '@ohos.base';

  let caller: Caller;
1695

M
mingxihua 已提交
1696
  // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
L
liuliu 已提交
1697
  let wantForeground: Want = {
M
mingxihua 已提交
1698 1699 1700 1701
      bundleName: 'com.example.myservice',
      moduleName: 'entry',
      abilityName: 'EntryAbility',
      deviceId: '',
1702
      parameters: {
M
mingxihua 已提交
1703
        'ohos.aafwk.param.callAbilityToForeground': true
1704 1705
      }
  };
H
HuangXW 已提交
1706 1707

  try {
1708
    this.context.startAbilityByCall(wantForeground)
L
liuliu 已提交
1709
      .then((obj: Caller) => {
H
HuangXW 已提交
1710
        // 执行正常业务
1711
        caller = obj;
H
HuangXW 已提交
1712
        console.log('startAbilityByCall succeed');
L
liuliu 已提交
1713
      }).catch((error: BusinessError) => {
H
HuangXW 已提交
1714
        // 处理业务逻辑错误
Z
zhoujun62 已提交
1715
        console.error('startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}');
H
HuangXW 已提交
1716 1717 1718
      });
  } catch (paramError) {
    // 处理入参错误异常
Z
zhoujun62 已提交
1719
    console.error('error.code: ${paramError.code}, error.message: ${paramError.message}');
H
HuangXW 已提交
1720
  }
X
add doc  
xinking129 已提交
1721
  ```
1722
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1723

Y
yuyaozhi 已提交
1724
startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
U
unknown 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745

启动一个指定的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 已提交
1746 1747
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
| 错误码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
L
liuliu 已提交
1767 1768 1769 1770
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
1771 1772 1773 1774 1775
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};

try {
L
liuliu 已提交
1776
  this.context.startRecentAbility(want, (err: BusinessError) => {
U
unknown 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1787 1788 1789
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
U
unknown 已提交
1790 1791
}
  ```
1792
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1793

Y
yuyaozhi 已提交
1794
startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
U
unknown 已提交
1795 1796

启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
U
unknown 已提交
1797
当开发者需要携带启动参数时可以选择此API。
U
unknown 已提交
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

使用规则:
 - 调用方应用位于后台时,使用该接口启动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 已提交
1818 1819
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
| 错误码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
L
liuliu 已提交
1839 1840 1841 1842 1843
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
1844 1845 1846 1847
  deviceId: '',
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
1848
let options: StartOptions = {
U
unknown 已提交
1849 1850 1851 1852
  windowMode: 0
};

try {
L
liuliu 已提交
1853
  this.context.startRecentAbility(want, options, (err: BusinessError) => {
U
unknown 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
    if (err.code) {
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
      return;
    }
    // 执行正常业务
    console.info('startRecentAbility succeed');
  });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1864 1865 1866
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
U
unknown 已提交
1867 1868
}
  ```
1869
## ServiceExtensionContext.startRecentAbility
U
unknown 已提交
1870

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

U
unknown 已提交
1873 1874
启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。
当开发者期望启动结果以Promise形式返回时可以选择此API。
U
unknown 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893

使用规则:
 - 调用方应用位于后台时,使用该接口启动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 已提交
1894 1895
以下错误码的详细介绍请参见[errcode-ability](../errorcodes/errorcode-ability.md)

U
unknown 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
| 错误码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
L
liuliu 已提交
1915 1916 1917 1918 1919
import Want from '@ohos.app.ability.Want';
import StartOptions from '@ohos.app.ability.StartOptions';
import { BusinessError } from '@ohos.base';

let want: Want = {
U
unknown 已提交
1920 1921 1922
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
L
liuliu 已提交
1923
let options: StartOptions = {
U
unknown 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932
  windowMode: 0,
};

try {
  this.context.startRecentAbility(want, options)
    .then(() => {
      // 执行正常业务
      console.info('startRecentAbility succeed');
    })
L
liuliu 已提交
1933
    .catch((err: BusinessError) => {
U
unknown 已提交
1934 1935 1936 1937 1938
      // 处理业务逻辑错误
      console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
    });
} catch (err) {
  // 处理入参错误异常
L
liuliu 已提交
1939 1940 1941
  let code = (err as BusinessError).code;
  let message = (err as BusinessError).message;
  console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
U
unknown 已提交
1942
}
X
xinking129 已提交
1943
  ```
X
add doc  
xinking129 已提交
1944

X
xinking129 已提交
1945
## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>
X
add doc  
xinking129 已提交
1946 1947 1948 1949 1950 1951 1952

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 已提交
1953 1954 1955
 - 调用方应用位于后台时,使用该接口启动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 已提交
1956

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

X
add doc  
xinking129 已提交
1959 1960 1961 1962 1963 1964 1965 1966
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
X
xinking129 已提交
1967
| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
X
xinking129 已提交
1968
| accountId | number | 是 | 系统帐号的帐号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
X
add doc  
xinking129 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977

**返回值:**

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

**错误码:**

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

X
add doc  
xinking129 已提交
1980 1981
| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
Y
yuyaozhi 已提交
1982
| 16000001 | The specified ability does not exist. |
X
add doc  
xinking129 已提交
1983
| 16000002 | Incorrect ability type. |
Y
yuyaozhi 已提交
1984
| 16000004 | Can not start invisible component. |
X
add doc  
xinking129 已提交
1985 1986
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000006 | Cross-user operations are not allowed. |
Y
yuyaozhi 已提交
1987
| 16000008 | The crowdtesting application expires. |
X
add doc  
xinking129 已提交
1988
| 16000011 | The context does not exist. |
Y
yuyaozhi 已提交
1989 1990 1991
| 16000012 | The application is controlled.        |
| 16000013 | The application is controlled by EDM.       |
| 16000050 | Internal error. |
X
add doc  
xinking129 已提交
1992 1993 1994 1995 1996
| 16200001 | The caller has been released.        |

**示例:**

  ```ts
L
liuliu 已提交
1997 1998 1999 2000
  import { Caller } from '@ohos.app.ability.UIAbility';
  import Want from '@ohos.app.ability.Want';
  import StartOptions from '@ohos.app.ability.StartOptions';
  import { BusinessError } from '@ohos.base';
X
add doc  
xinking129 已提交
2001

L
liuliu 已提交
2002
  let caller: Caller;
X
add doc  
xinking129 已提交
2003 2004 2005
  // 系统账号的账号ID, -1表示当前激活用户
  let accountId = -1;
  // 指定启动的Ability
L
liuliu 已提交
2006
  let want: Want = {
X
add doc  
xinking129 已提交
2007 2008
      bundleName: 'com.acts.actscalleeabilityrely',
      moduleName: 'entry',
Y
yuyaozhi 已提交
2009 2010
      abilityName: 'EntryAbility',
      deviceId: '',
X
xinking129 已提交
2011 2012 2013 2014
      parameters: {
        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
        'ohos.aafwk.param.callAbilityToForeground': true
      }
X
add doc  
xinking129 已提交
2015 2016 2017 2018
  };

  try {
    this.context.startAbilityByCallWithAccount(want, accountId)
L
liuliu 已提交
2019
      .then((obj: Caller) => {
X
add doc  
xinking129 已提交
2020 2021 2022
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCallWithAccount succeed');
L
liuliu 已提交
2023
      }).catch((error: BusinessError) => {
X
add doc  
xinking129 已提交
2024 2025 2026 2027 2028 2029 2030
        // 处理业务逻辑错误
        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 已提交
2031
  ```