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

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

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 MainAbility 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
H
HuangXW 已提交
71
  var 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

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 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
H
HuangXW 已提交
144
  var want = {
145 146
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
H
HuangXW 已提交
147 148 149 150
  };
  var options = {
  	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 174 175 176 177 178 179 180 181 182 183

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

启动Ability。

**系统能力**: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
215
  var want = {
216 217 218
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
219 220
  };
  var 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 248 249 250 251 252 253 254 255

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

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

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

**参数:**

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

H
HuangXW 已提交
260 261 262 263
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
264
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
265
| 401 | Invalid input parameter. |
M
m00512953 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| 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 已提交
284

285 286
**示例:**

287
  ```ts
288
  var want = {
289 290 291
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
292 293 294
  };
  var accountId = 100;

H
HuangXW 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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));
  }
  ```
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

## ServiceExtensionContext.startAbilityWithAccount

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

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

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

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

**参数:**

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

H
HuangXW 已提交
332 333 334 335
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
336
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
337
| 401 | Invalid input parameter. |
M
m00512953 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
| 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 已提交
356

357 358
**示例:**

359
  ```ts
360
  var want = {
361 362 363
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
364 365 366
  };
  var accountId = 100;
  var options = {
H
HuangXW 已提交
367
    windowMode: 0
368
  };
H
HuangXW 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

  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));
  }
386 387 388 389 390
  ```


## ServiceExtensionContext.startAbilityWithAccount

391
startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
392 393 394 395 396 397 398 399 400 401 402

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

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

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

**参数:**

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

407 408
**返回值:**

Z
zhongjianfei 已提交
409 410 411
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
412

H
HuangXW 已提交
413 414 415 416
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
417
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
418
| 401 | Invalid input parameter. |
M
m00512953 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
| 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 已提交
437

438 439
**示例:**

440
  ```ts
441
  var want = {
442 443 444
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
445 446 447
  };
  var accountId = 100;
  var options = {
H
HuangXW 已提交
448
    windowMode: 0
449
  };
H
HuangXW 已提交
450 451 452 453 454 455 456 457 458

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

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

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

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

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

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

**参数:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
490
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
491
| 401 | Invalid input parameter. |
M
m00512953 已提交
492 493 494 495 496 497 498 499 500 501
| 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 已提交
502

503 504
**示例:**

505
  ```ts
506
  var want = {
507 508 509
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
510
  };
H
HuangXW 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

  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));
  }
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  ```

## ServiceExtensionContext.startServiceExtensionAbility

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

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

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

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

**参数:**

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

546 547
**返回值:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
556
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
557
| 401 | Invalid input parameter. |
M
m00512953 已提交
558 559 560 561 562 563 564 565 566 567
| 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 已提交
568

569 570
**示例:**

571
  ```ts
572
  var want = {
573 574 575
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
576
  };
H
HuangXW 已提交
577 578 579 580 581 582 583 584 585

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

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

D
donglin 已提交
602
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
603 604 605 606 607 608 609 610 611

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

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

**参数:**

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

H
HuangXW 已提交
616 617 618 619
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
620
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
621
| 401 | Invalid input parameter. |
M
m00512953 已提交
622 623 624 625 626 627 628 629 630 631 632
| 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 已提交
633 634


635 636
**示例:**

637
  ```ts
638
  var want = {
639 640 641
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
642 643
  };
  var accountId = 100;
H
HuangXW 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

  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));
  }
661 662 663 664 665 666 667 668
  ```

## ServiceExtensionContext.startServiceExtensionAbilityWithAccount

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

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

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

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

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

**参数:**

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

682 683
**返回值:**

Z
zhongjianfei 已提交
684 685 686
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
687

H
HuangXW 已提交
688 689 690 691
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
692
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
693
| 401 | Invalid input parameter. |
M
m00512953 已提交
694 695 696 697 698 699 700 701 702 703 704
| 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 已提交
705

706 707
**示例:**

708
  ```ts
709
  var want = {
710 711 712
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
713 714
  };
  var accountId = 100;
H
HuangXW 已提交
715 716 717 718 719 720 721 722 723

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

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

H
HuangXW 已提交
751 752 753 754
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
755
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
756
| 401 | Invalid input parameter. |
M
m00512953 已提交
757 758 759 760 761 762 763
| 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 已提交
764

765 766
**示例:**

767
  ```ts
768
  var want = {
769 770 771
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
772
  };
H
HuangXW 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

  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));
  }
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  ```

## ServiceExtensionContext.stopServiceExtensionAbility

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

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

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

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

**参数:**

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

**返回值:**

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

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

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

828 829
**示例:**

830
  ```ts
831
  var want = {
832 833 834
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
835
  };
H
HuangXW 已提交
836 837 838 839 840 841 842 843 844

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

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

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

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

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

**参数:**

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

H
HuangXW 已提交
875 876 877 878
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
879
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
880
| 401 | Invalid input parameter. |
M
m00512953 已提交
881 882 883 884 885 886 887 888
| 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 已提交
889

890 891
**示例:**

892
  ```ts
893
  var want = {
894 895 896
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
897 898
  };
  var accountId = 100;
H
HuangXW 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

  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));
  }
916 917 918 919 920 921 922 923
  ```

## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount

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

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

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

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

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

**参数:**

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

**返回值:**

Z
zhongjianfei 已提交
939 940 941
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
942

H
HuangXW 已提交
943 944 945 946
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
947
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
948
| 401 | Invalid input parameter. |
M
m00512953 已提交
949 950 951 952 953 954 955 956
| 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 已提交
957

958 959
**示例:**

960
  ```ts
961
  var want = {
962 963 964
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
965 966
  };
  var accountId = 100;
H
HuangXW 已提交
967 968 969 970 971 972 973 974 975

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

Y
yuyaozhi 已提交
986
## ServiceExtensionContext.terminateSelf
987

988
terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
989

990
停止Ability自身。
991

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

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

Y
yuyaozhi 已提交
996
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
997

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

H
HuangXW 已提交
1002 1003 1004 1005
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1006
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1007
| 401 | Invalid input parameter. |
M
m00512953 已提交
1008 1009 1010 1011
| 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 已提交
1012

Y
yuyaozhi 已提交
1013 1014
**示例:**

1015
  ```ts
H
HuangXW 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024
  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');
1025
  });
1026 1027
  ```

Y
yuyaozhi 已提交
1028
## ServiceExtensionContext.terminateSelf
1029

1030
terminateSelf(): Promise&lt;void&gt;;
1031

1032
停止自身。通过Promise返回结果。
1033

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

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

Y
yuyaozhi 已提交
1038
**返回值:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1039

Z
zhongjianfei 已提交
1040 1041 1042
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1043

H
HuangXW 已提交
1044 1045 1046 1047
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1048
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1049
| 401 | Invalid input parameter. |
M
m00512953 已提交
1050 1051 1052 1053
| 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 已提交
1054

Y
yuyaozhi 已提交
1055 1056
**示例:**

1057
  ```ts
1058
  this.context.terminateSelf().then((data) => {
H
HuangXW 已提交
1059 1060
    // 执行正常业务
    console.log('terminateSelf succeed');
1061
  }).catch((error) => {
H
HuangXW 已提交
1062 1063 1064
    // 处理业务逻辑错误
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
1065
  });
1066 1067
  ```

H
HuangXW 已提交
1068
## ServiceExtensionContext.connectServiceExtensionAbility
1069

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

1072
将一个Ability与服务类型的Ability绑定。
1073

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

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

Y
yuyaozhi 已提交
1078
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1079

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

Y
yuyaozhi 已提交
1085 1086
**返回值:**

Z
zhongjianfei 已提交
1087 1088 1089
| 类型 | 说明 |
| -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 |
1090

H
HuangXW 已提交
1091 1092 1093 1094
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1095
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1096
| 401 | Invalid input parameter. |
M
m00512953 已提交
1097 1098 1099 1100 1101
| 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 已提交
1102

Y
yuyaozhi 已提交
1103 1104
**示例:**

1105
  ```ts
H
HuangXW 已提交
1106
  var want = {
1107 1108
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
1109
  };
H
HuangXW 已提交
1110
  var options = {
1111 1112 1113
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
1114
  }
H
HuangXW 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1124 1125
  ```

H
HuangXW 已提交
1126
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1127

H
HuangXW 已提交
1128
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

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

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

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

**参数:**

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

**返回值:**

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

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

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1154
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1155
| 401 | Invalid input parameter. |
M
m00512953 已提交
1156 1157 1158 1159 1160 1161
| 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 已提交
1162

1163 1164
**示例:**

1165
  ```ts
1166
  var want = {
1167 1168 1169
    deviceId: "",
    bundleName: "com.extreme.test",
    abilityName: "MainAbility"
1170 1171 1172 1173 1174 1175 1176
  };
  var accountId = 100;
  var options = {
    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
    onFailed(code) { console.log('----------- onFailed -----------') }
  }
H
HuangXW 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1186
  ```
1187

H
HuangXW 已提交
1188
## ServiceExtensionContext.disconnectServiceExtensionAbility
1189

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

1192
将一个Ability与绑定的服务类型的Ability解绑。
1193

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

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

Y
yuyaozhi 已提交
1198
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1199

Z
zhongjianfei 已提交
1200 1201 1202 1203
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| connection | number | 是 | 在connectAbility中返回的number。 |
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 |
1204

H
HuangXW 已提交
1205 1206 1207 1208
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1209
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1210
| 401 | Invalid input parameter. |
M
m00512953 已提交
1211 1212 1213 1214
| 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 已提交
1215

Y
yuyaozhi 已提交
1216 1217
**示例:**

1218
  ```ts
H
HuangXW 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  // connection为connectServiceExtensionAbility中的返回值
  var connection = 1;

  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');
1232
    });
H
HuangXW 已提交
1233 1234 1235 1236 1237
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1238 1239
  ```

H
HuangXW 已提交
1240
## ServiceExtensionContext.disconnectServiceExtensionAbility
1241

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

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

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

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

Y
yuyaozhi 已提交
1250
**参数:**
ahjxliubao2's avatar
ahjxliubao2 已提交
1251

Z
zhongjianfei 已提交
1252 1253 1254
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| connection | number | 是 | 在connectAbility中返回的number。 |
1255

Y
yuyaozhi 已提交
1256 1257
**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1266
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1267
| 401 | Invalid input parameter. |
M
m00512953 已提交
1268 1269 1270 1271
| 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 已提交
1272

Y
yuyaozhi 已提交
1273 1274
**示例:**

1275
  ```ts
H
HuangXW 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
  // connection为connectAbility中的返回值
  var connection = 1;

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
1287 1288
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
H
HuangXW 已提交
1289 1290 1291 1292 1293 1294
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
1295 1296 1297 1298 1299 1300
  ```

## ServiceExtensionContext.startAbilityByCall

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

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

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

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

1307 1308 1309 1310
**参数:**

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

**返回值:**

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

H
HuangXW 已提交
1319 1320 1321 1322
**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
M
m00512953 已提交
1323
| 201 | The application does not have permission to call the interface. |
H
HuangXW 已提交
1324
| 401 | Invalid input parameter. |
M
m00512953 已提交
1325 1326 1327 1328 1329 1330 1331
| 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 已提交
1332

1333 1334
**示例:**

1335
  后台启动:
H
HuangXW 已提交
1336

1337
  ```ts
H
HuangXW 已提交
1338
  var caller = undefined;
1339 1340 1341

  // 后台启动Ability,不配置parameters
  var wantBackground = {
1342 1343 1344 1345
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: ""
1346
  };
H
HuangXW 已提交
1347 1348 1349 1350 1351

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

  前台启动:

1368
  ```ts
H
HuangXW 已提交
1369
  var caller = undefined;
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

  // 前台启动Ability,将parameters中的"ohos.aafwk.param.callAbilityToForeground"配置为true
  var wantForeground = {
      bundleName: "com.example.myservice",
      moduleName: "entry",
      abilityName: "MainAbility",
      deviceId: "",
      parameters: {
        "ohos.aafwk.param.callAbilityToForeground": true
      }
  };
H
HuangXW 已提交
1381 1382

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