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

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

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

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

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

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

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

  let context = undefined;
20
  class EntryAbility extends ServiceExtensionAbility {
H
HuangXW 已提交
21
    onCreate() {
Z
zhongjianfei 已提交
22
      context = this.context; // 获取ServiceExtensionContext
H
HuangXW 已提交
23
    }
24 25
  }
```
Y
yuyaozhi 已提交
26

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

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

启动Ability。

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

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

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

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

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

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

Y
yuyaozhi 已提交
68 69
**示例:**

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

**参数:**

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

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

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

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

214
  ```ts
M
mingxihua 已提交
215
  let want = {
216
    deviceId: "",
217 218
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
219
  };
M
mingxihua 已提交
220
  let options = {
H
HuangXW 已提交
221
    windowMode: 0
222
  };
H
HuangXW 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

  try {
    this.context.startAbility(want, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('startAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
240 241 242 243
  ```

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

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

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

**参数:**

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

H
HuangXW 已提交
265 266 267 268
**错误码:**

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

290 291
**示例:**

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

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

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

329 330 331 332 333 334 335 336
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

H
HuangXW 已提交
342 343 344 345
**错误码:**

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

367 368
**示例:**

369
  ```ts
M
mingxihua 已提交
370
  let want = {
371
    deviceId: "",
372 373
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
374
  };
M
mingxihua 已提交
375 376
  let accountId = 100;
  let options = {
H
HuangXW 已提交
377
    windowMode: 0
378
  };
H
HuangXW 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
396 397 398 399 400
  ```


## ServiceExtensionContext.startAbilityWithAccount

401
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
402 403 404

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

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

410 411 412 413 414 415 416 417
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

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

**参数:**

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

422 423
**返回值:**

Z
zhongjianfei 已提交
424 425 426
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
427

H
HuangXW 已提交
428 429 430 431
**错误码:**

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

453 454
**示例:**

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

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

484 485 486 487 488 489 490 491 492 493 494 495 496 497
## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
501 502 503 504
**错误码:**

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

518 519
**示例:**

520
  ```ts
M
mingxihua 已提交
521
  let want = {
522
    deviceId: "",
523 524
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
525
  };
H
HuangXW 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

561 562
**返回值:**

Z
zhongjianfei 已提交
563 564 565
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
566

H
HuangXW 已提交
567 568 569 570
**错误码:**

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

584 585
**示例:**

586
  ```ts
M
mingxihua 已提交
587
  let want = {
588
    deviceId: "",
589 590
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
591
  };
H
HuangXW 已提交
592 593 594 595 596 597 598 599 600

  try {
    this.context.startServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
601 602
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
603 604 605 606 607 608
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
609 610 611 612 613 614 615 616
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

D
donglin 已提交
617
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
618 619 620 621 622 623 624 625 626

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

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

**参数:**

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

H
HuangXW 已提交
631 632 633 634
**错误码:**

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


650 651
**示例:**

652
  ```ts
M
mingxihua 已提交
653
  let want = {
654
    deviceId: "",
655 656
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
657
  };
M
mingxihua 已提交
658
  let accountId = 100;
H
HuangXW 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
676 677 678 679 680 681 682 683
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

D
donglin 已提交
684
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
685 686 687 688 689 690 691 692 693

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

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

**参数:**

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

697 698
**返回值:**

Z
zhongjianfei 已提交
699 700 701
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
702

H
HuangXW 已提交
703 704 705 706
**错误码:**

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

721 722
**示例:**

723
  ```ts
M
mingxihua 已提交
724
  let want = {
725
    deviceId: "",
726 727
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
728
  };
M
mingxihua 已提交
729
  let accountId = 100;
H
HuangXW 已提交
730 731 732 733 734 735 736 737 738

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
739 740
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
741 742 743 744 745 746
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
766 767 768 769
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
770
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
771
| 401 | Invalid input parameter. |
M
m00512953 已提交
772 773 774 775 776 777 778
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
779

780 781
**示例:**

782
  ```ts
M
mingxihua 已提交
783
  let want = {
784
    deviceId: "",
785 786
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
787
  };
H
HuangXW 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
825 826 827
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
828

H
HuangXW 已提交
829 830 831 832
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
833
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
834
| 401 | Invalid input parameter. |
M
m00512953 已提交
835 836 837 838 839 840 841
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
842

843 844
**示例:**

845
  ```ts
M
mingxihua 已提交
846
  let want = {
847
    deviceId: "",
848 849
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
850
  };
H
HuangXW 已提交
851 852 853 854 855 856 857 858 859

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
860 861
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
862 863 864 865 866 867
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
868 869 870 871 872 873 874 875
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

D
donglin 已提交
876
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
877 878 879 880 881 882 883 884 885

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

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

**参数:**

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

H
HuangXW 已提交
890 891 892 893
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
894
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
895
| 401 | Invalid input parameter. |
M
m00512953 已提交
896 897 898 899 900 901 902 903
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
904

905 906
**示例:**

907
  ```ts
M
mingxihua 已提交
908
  let want = {
909
    deviceId: "",
910 911
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
912
  };
M
mingxihua 已提交
913
  let accountId = 100;
H
HuangXW 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
931 932 933 934 935 936 937 938
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

D
donglin 已提交
939
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
940 941 942 943 944 945 946 947 948

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
954 955 956
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
957

H
HuangXW 已提交
958 959 960 961
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
962
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
963
| 401 | Invalid input parameter. |
M
m00512953 已提交
964 965 966 967 968 969 970 971
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
972

973 974
**示例:**

975
  ```ts
M
mingxihua 已提交
976
  let want = {
977
    deviceId: "",
978 979
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
980
  };
M
mingxihua 已提交
981
  let accountId = 100;
H
HuangXW 已提交
982 983 984 985 986 987 988 989 990

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
991 992
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
993 994 995 996 997 998
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
999
  ```
1000

Y
yuyaozhi 已提交
1001
## ServiceExtensionContext.terminateSelf
1002

1003
terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1004

1005
停止Ability自身。
1006

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

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

Y
yuyaozhi 已提交
1011
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1012

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1021
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1022
| 401 | Invalid input parameter. |
M
m00512953 已提交
1023 1024 1025 1026
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1027

Y
yuyaozhi 已提交
1028 1029
**示例:**

1030
  ```ts
H
HuangXW 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039
  this.context.terminateSelf((error) => {
    if (error.code) {
      // 处理业务逻辑错误
      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
        ' error.message: ' + JSON.stringify(error.message));
      return;
    }
    // 执行正常业务
    console.log('terminateSelf succeed');
1040
  });
1041 1042
  ```

Y
yuyaozhi 已提交
1043
## ServiceExtensionContext.terminateSelf
1044

1045
terminateSelf(): Promise&lt;void&gt;;
1046

1047
停止自身。通过Promise返回结果。
1048

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

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

Y
yuyaozhi 已提交
1053
**返回值:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1054

Z
zhongjianfei 已提交
1055 1056 1057
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1058

H
HuangXW 已提交
1059 1060 1061 1062
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1063
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1064
| 401 | Invalid input parameter. |
M
m00512953 已提交
1065 1066 1067 1068
| 16000001 | Input error. The specified ability name does not exist. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1069

Y
yuyaozhi 已提交
1070 1071
**示例:**

1072
  ```ts
1073
  this.context.terminateSelf().then((data) => {
H
HuangXW 已提交
1074 1075
    // 执行正常业务
    console.log('terminateSelf succeed');
1076
  }).catch((error) => {
H
HuangXW 已提交
1077 1078 1079
    // 处理业务逻辑错误
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
1080
  });
1081 1082
  ```

H
HuangXW 已提交
1083
## ServiceExtensionContext.connectServiceExtensionAbility
1084

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

1087
将一个Ability与服务类型的Ability绑定。
1088

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

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

Y
yuyaozhi 已提交
1093
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1094

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

Y
yuyaozhi 已提交
1100 1101
**返回值:**

Z
zhongjianfei 已提交
1102 1103 1104
| 类型 | 说明 |
| -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 |
1105

H
HuangXW 已提交
1106 1107 1108 1109
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1110
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1111
| 401 | Invalid input parameter. |
M
m00512953 已提交
1112 1113 1114 1115 1116
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1117

Y
yuyaozhi 已提交
1118 1119
**示例:**

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

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

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

H
HuangXW 已提交
1143
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

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

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

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

**参数:**

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

**返回值:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1169
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1170
| 401 | Invalid input parameter. |
M
m00512953 已提交
1171 1172 1173 1174 1175 1176
| 16000001 | Input error. The specified ability name does not exist. |
| 16000002 | Ability type error. The specified ability type is wrong. |
| 16000004 | Visibility verification failed. |
| 16000006 | Can not cross user operations. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1177

1178 1179
**示例:**

1180
  ```ts
M
mingxihua 已提交
1181
  let want = {
1182
    deviceId: "",
1183 1184
    bundleName: "com.example.myapplication",
    abilityName: "EntryAbility"
1185
  };
M
mingxihua 已提交
1186 1187
  let accountId = 100;
  let options = {
1188 1189 1190 1191
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
H
HuangXW 已提交
1192

M
mingxihua 已提交
1193
  let connection = null;
H
HuangXW 已提交
1194 1195 1196 1197 1198 1199 1200
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1201
  ```
1202

H
HuangXW 已提交
1203
## ServiceExtensionContext.disconnectServiceExtensionAbility
1204

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

1207
将一个Ability与绑定的服务类型的Ability解绑。
1208

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

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

Y
yuyaozhi 已提交
1213
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1214

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

H
HuangXW 已提交
1220 1221 1222 1223
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1224
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1225
| 401 | Invalid input parameter. |
M
m00512953 已提交
1226 1227 1228 1229
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1230

Y
yuyaozhi 已提交
1231 1232
**示例:**

1233
  ```ts
H
HuangXW 已提交
1234
  // connection为connectServiceExtensionAbility中的返回值
M
mingxihua 已提交
1235
  let connection = 1;
H
HuangXW 已提交
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
        return;
      }
      // 执行正常业务
      console.log('disconnectServiceExtensionAbility succeed');
1247
    });
H
HuangXW 已提交
1248 1249 1250 1251 1252
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1253 1254
  ```

H
HuangXW 已提交
1255
## ServiceExtensionContext.disconnectServiceExtensionAbility
1256

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

1259
将一个Ability与绑定的服务类型的Ability解绑。通过Promise返回结果。
1260

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

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

Y
yuyaozhi 已提交
1265
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1266

Z
zhongjianfei 已提交
1267 1268
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
M
mingxihua 已提交
1269
| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1270

Y
yuyaozhi 已提交
1271 1272
**返回值:**

Z
zhongjianfei 已提交
1273 1274 1275
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
H
HuangXW 已提交
1276 1277 1278 1279 1280

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1281
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1282
| 401 | Invalid input parameter. |
M
m00512953 已提交
1283 1284 1285 1286
| 16000001 | Input error. The specified ability name does not exist. |
| 16000003 | Input error. The specified id does not exist. |
| 16000011 | Context does not exist.        |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1287

Y
yuyaozhi 已提交
1288 1289
**示例:**

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

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
1302 1303
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
1304 1305 1306 1307 1308 1309
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1310 1311 1312 1313 1314 1315
  ```

## ServiceExtensionContext.startAbilityByCall

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

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

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

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

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

1327 1328 1329 1330
**参数:**

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

**返回值:**

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

H
HuangXW 已提交
1339 1340 1341 1342
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1343
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1344
| 401 | Invalid input parameter. |
M
m00512953 已提交
1345 1346 1347 1348 1349 1350 1351
| 16000001 | Input error. The specified ability name does not exist. |
| 16000004 | Visibility verification failed. |
| 16000005 | Static permission denied. The specified process does not have the permission. |
| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
| 16000008 | Crowdtest App Expiration. |
| 16000009 | Can not start ability in wukong mode. |
| 16000050 | Internal Error. |
H
HuangXW 已提交
1352

1353 1354
**示例:**

1355
  后台启动:
H
HuangXW 已提交
1356

1357
  ```ts
M
mingxihua 已提交
1358
  let caller = undefined;
1359 1360

  // 后台启动Ability,不配置parameters
M
mingxihua 已提交
1361
  let wantBackground = {
1362 1363
      bundleName: "com.example.myservice",
      moduleName: "entry",
1364
      abilityName: "EntryAbility",
1365
      deviceId: ""
1366
  };
H
HuangXW 已提交
1367 1368 1369 1370 1371

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // 执行正常业务
1372
        caller = obj;
H
HuangXW 已提交
1373 1374 1375
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
1376 1377
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
  ```

  前台启动:

1388
  ```ts
M
mingxihua 已提交
1389
  let caller = undefined;
1390 1391

  // 前台启动Ability,将parameters中的"ohos.aafwk.param.callAbilityToForeground"配置为true
M
mingxihua 已提交
1392
  let wantForeground = {
1393 1394
      bundleName: "com.example.myservice",
      moduleName: "entry",
1395
      abilityName: "EntryAbility",
1396 1397 1398 1399 1400
      deviceId: "",
      parameters: {
        "ohos.aafwk.param.callAbilityToForeground": true
      }
  };
H
HuangXW 已提交
1401 1402

  try {
1403
    this.context.startAbilityByCall(wantForeground)
H
HuangXW 已提交
1404 1405
      .then((obj) => {
        // 执行正常业务
1406
        caller = obj;
H
HuangXW 已提交
1407 1408 1409
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
        // 处理业务逻辑错误
1410 1411
        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
1412 1413 1414 1415 1416 1417
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
M
m00512953 已提交
1418
  ```