js-apis-inner-application-uiAbilityContext.md 81.6 KB
Newer Older
M
m00512953 已提交
1 2
# UIAbilityContext

3
UIAbilityContext是[UIAbility](js-apis-app-ability-uiAbility.md)的上下文环境,继承自[Context](js-apis-inner-application-context.md),提供UIAbility的相关配置信息以及操作UIAbility和ServiceExtensionAbility的方法,如启动UIAbility,停止当前UIAbilityContext所属的UIAbility,启动、停止、连接、断开连接ServiceExtensionAbility等。
M
m00512953 已提交
4 5 6 7 8 9 10 11 12 13

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

## 属性

**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core

D
merge  
donglin 已提交
14
| 名称 | 类型 | 可读 | 可写 | 说明 |
M
m00512953 已提交
15
| -------- | -------- | -------- | -------- | -------- |
16
| abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | 是 | 否 | UIAbility的相关信息。 |
zyjhandsome's avatar
zyjhandsome 已提交
17
| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | 是 | 否 | 当前HAP的信息。 |
18 19 20 21
| config | [Configuration](js-apis-app-ability-configuration.md) | 是 | 否 | 与UIAbility相关的配置信息,如语言、颜色模式等。 |

> **关于示例代码的说明:**
> - 本文档中的示例,通过this.context获取UIAbilityContext,this代表的是继承自UIAbility的UIAbility实例,若需要在页面中使用UIAbilityContext的能力,请参照[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)
M
m00512953 已提交
22

Z
zhongjianfei 已提交
23
## UIAbilityContext.startAbility
M
m00512953 已提交
24 25 26 27 28

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

启动Ability(callback形式)。

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

M
m00512953 已提交
34 35 36 37 38 39 40
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
41
| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |
M
m00512953 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
  };

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

Z
zhongjianfei 已提交
93
## UIAbilityContext.startAbility
M
m00512953 已提交
94 95 96 97 98

startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void;

启动Ability(callback形式)。

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

M
m00512953 已提交
104 105 106 107 108 109 110 111 112 113 114 115
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

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

**错误码:**

Z
zhongjianfei 已提交
116
| 错误码ID | 错误信息 |
M
m00512953 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
143
    bundleName: "com.example.myapplication",
M
m00512953 已提交
144 145 146 147 148 149 150 151 152 153 154
    abilityName: "MainAbility"
  };
  var options = {
    windowMode: 0
  };

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

Z
zhongjianfei 已提交
168
## UIAbilityContext.startAbility
M
m00512953 已提交
169 170 171 172 173

startAbility(want: Want, options?: StartOptions): Promise<void>;

启动Ability(promise形式)。

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

M
m00512953 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise<void> | Promise形式返回启动结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    bundleName: "com.example.myapp",
    abilityName: "MyAbility"
  };
  var options = {
226
    windowMode: 0,
M
m00512953 已提交
227 228 229 230
  };

  try {
    this.context.startAbility(want, options)
231
      .then(() => {
M
m00512953 已提交
232 233 234 235 236 237
        // 执行正常业务
        console.log('startAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
238
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
239 240 241
      });
  } catch (paramError) {
    // 处理入参错误异常
242
    console.log('startAbility failed, error.code: ' + JSON.stringify(paramError.code) +
243
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
244 245 246
  }
  ```

Z
zhongjianfei 已提交
247
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
248 249 250

startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void;

M
m00512953 已提交
251
启动一个Ability。Ability被启动后,正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用者。异常情况下比如杀死Ability会返回异常信息给调用者(callback形式)。
M
m00512953 已提交
252

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

M
m00512953 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 执行结果回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
296
    bundleName: "com.example.myapplication",
M
m00512953 已提交
297 298 299 300 301 302 303 304
    abilityName: "MainAbility"
  };

  try {
    this.context.startAbilityForResult(want, (error, result) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
305
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
306 307 308
        return;
      }
      // 执行正常业务
309
      console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode)
M
m00512953 已提交
310 311 312
    });
  } catch (paramError) {
    // 处理入参错误异常
313
    console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(paramError.code) +
314
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
315 316 317
  }
  ```

Z
zhongjianfei 已提交
318
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
319 320 321

startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void;

M
m00512953 已提交
322
启动一个Ability。Ability被启动后,正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用者。异常情况下比如杀死Ability会返回异常信息给调用者(callback形式)。
M
m00512953 已提交
323

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
 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
M
m00512953 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

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

**参数:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
368
    bundleName: "com.example.myapplication",
M
m00512953 已提交
369 370 371 372 373 374 375 376 377 378 379
    abilityName: "MainAbility"
  };
  var options = {
    windowMode: 0,
  };

  try {
    this.context.startAbilityForResult(want, options, (error, result) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
380
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
381 382 383
        return;
      }
      // 执行正常业务
384
      console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode)
M
m00512953 已提交
385 386 387
    });
  } catch (paramError) {
    // 处理入参错误异常
388 389
    console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(paramError.code) +
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
390 391 392 393
  }
  ```


Z
zhongjianfei 已提交
394
## UIAbilityContext.startAbilityForResult
M
m00512953 已提交
395 396 397

startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>;

M
m00512953 已提交
398
启动一个Ability。Ability被启动后,正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用者。异常情况下比如杀死Ability会返回异常信息给调用者(promise形式)。
M
m00512953 已提交
399

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

M
m00512953 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

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


**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise形式返回执行结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
449 450
    bundleName: "com.example.myapplication",
    abilityName: "MainAbility"
M
m00512953 已提交
451 452
  };
  var options = {
453
    windowMode: 0,
M
m00512953 已提交
454 455 456 457 458 459 460 461 462 463 464
  };

  try {
    this.context.startAbilityForResult(want, options)
      .then((result) => {
        // 执行正常业务
        console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode);
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
465
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
466 467 468
      });
  } catch (paramError) {
    // 处理入参错误异常
469 470
    console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(paramError.code) +
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
471 472 473
  }
  ```

Z
zhongjianfei 已提交
474
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
475 476 477

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

478
启动一个Ability并在该Ability销毁时返回执行结果(callback形式)。
M
m00512953 已提交
479

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

D
donglin 已提交
485
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
486 487 488 489 490 491 492 493 494 495

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
496 497
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | 是 | 启动Ability的回调函数,返回Ability结果。 |
M
m00512953 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
529
    bundleName: "com.example.myapplication",
M
m00512953 已提交
530 531 532 533 534 535 536 537 538
    abilityName: "MainAbility"
  };
  var accountId = 100;

  try {
    this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
539
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
540 541 542 543
        return;
      }
      // 执行正常业务
      console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
544
      result.resultCode + ' result.want = ' + JSON.stringify(result.want))
M
m00512953 已提交
545 546 547
    });
  } catch (paramError) {
    // 处理入参错误异常
548
    console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
549
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
550 551 552 553
  }
  ```


Z
zhongjianfei 已提交
554
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
555 556 557

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

558
启动一个Ability并在该Ability销毁时返回执行结果(callback形式)。
M
m00512953 已提交
559

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

D
donglin 已提交
565
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
566 567 568 569 570 571 572 573 574 575

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
576
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
577
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
578
| callback | AsyncCallback\<void\> | 是 | 启动Ability后,Ability被销毁时的回调函数。 |
M
m00512953 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
610
    bundleName: "com.example.myapplication",
M
m00512953 已提交
611 612 613 614 615 616 617 618
    abilityName: "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0
  };

  try {
619
    this.context.startAbilityForResultWithAccount(want, accountId, options, (error) => {
M
m00512953 已提交
620 621 622
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
623
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
624 625 626
        return;
      }
      // 执行正常业务
627
      console.log("startAbilityForResultWithAccount succeed")
M
m00512953 已提交
628 629 630
    });
  } catch (paramError) {
    // 处理入参错误异常
631
    console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
632
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
633 634 635 636
  }
  ```


Z
zhongjianfei 已提交
637
## UIAbilityContext.startAbilityForResultWithAccount
M
m00512953 已提交
638 639 640

startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\>;

641
启动一个Ability并在该Ability销毁时返回执行结果(promise形式)。
M
m00512953 已提交
642

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

D
donglin 已提交
648
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
649 650 651 652 653 654 655 656 657 658

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

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

**参数:**

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
666
| Promise&lt;[AbilityResult](js-apis-inner-ability-abilityResult.md)&gt; | Ability被销毁时的回调函数,包含AbilityResult参数。 |
M
m00512953 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
698
    bundleName: "com.example.myapplication",
M
m00512953 已提交
699 700 701 702 703 704 705 706 707 708 709 710
    abilityName: "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0
  };

  try {
    this.context.startAbilityForResultWithAccount(want, accountId, options)
      .then((result) => {
        // 执行正常业务
        console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
711
        result.resultCode)
M
m00512953 已提交
712 713 714 715
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
716
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
717 718 719
      });
  } catch (paramError) {
    // 处理入参错误异常
720 721
    console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
722 723
  }
  ```
Z
zhongjianfei 已提交
724
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
738 739
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
763 764
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
765 766 767 768 769 770 771
  };

  try {
    this.context.startServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
772
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
773 774 775 776 777 778 779
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
780
    console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(paramError.code) +
781
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
782 783 784
  }
  ```

Z
zhongjianfei 已提交
785
## UIAbilityContext.startServiceExtensionAbility
M
m00512953 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
799
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
823 824
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
825 826 827 828
  };

  try {
    this.context.startServiceExtensionAbility(want)
829
      .then(() => {
M
m00512953 已提交
830 831 832 833 834 835
        // 执行正常业务
        console.log('startServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
836
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
837 838 839
      });
  } catch (paramError) {
    // 处理入参错误异常
840
    console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(paramError.code) +
841
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
842 843 844
  }
  ```

Z
zhongjianfei 已提交
845
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
846 847 848 849 850

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

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

D
donglin 已提交
851
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
852 853 854 855 856 857 858 859 860

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
861
| want | [Want](js-apis-application-want.md) | 是 | 启动ServiceExtensionAbility的want信息。 |
862
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
863
| callback | AsyncCallback\<void\> | 是 | 启动ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
884 885
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
886 887 888 889 890 891 892 893
  };
  var accountId = 100;

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
894
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
895 896 897 898 899 900 901
        return;
      }
      // 执行正常业务
      console.log('startServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
902
    console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
903
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
904 905 906
  }
  ```

Z
zhongjianfei 已提交
907
## UIAbilityContext.startServiceExtensionAbilityWithAccount
M
m00512953 已提交
908 909 910 911 912

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

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

D
donglin 已提交
913
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
914 915 916 917 918 919 920 921 922 923

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
924
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
949 950
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
951 952 953 954 955 956 957 958 959 960 961 962
  };
  var accountId = 100;

  try {
    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('startServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
963
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
964 965 966
      });
  } catch (paramError) {
    // 处理入参错误异常
967
    console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
968
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
969 970
  }
  ```
Z
zhongjianfei 已提交
971
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
985 986
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1007 1008
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1009 1010 1011 1012 1013 1014 1015
  };

  try {
    this.context.stopServiceExtensionAbility(want, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1016
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1017 1018 1019 1020 1021 1022 1023
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
1024
    console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(paramError.code) +
1025
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1026 1027 1028
  }
  ```

Z
zhongjianfei 已提交
1029
## UIAbilityContext.stopServiceExtensionAbility
M
m00512953 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

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

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

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1043
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
M
m00512953 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1064 1065
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
  };

  try {
    this.context.stopServiceExtensionAbility(want)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1077
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1078 1079 1080
      });
  } catch (paramError) {
    // 处理入参错误异常
1081
    console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(paramError.code) +
1082
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1083 1084 1085
  }
  ```

Z
zhongjianfei 已提交
1086
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1087 1088 1089

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

1090
停止同一应用程序内指定账户的服务(callback形式)。
M
m00512953 已提交
1091

D
donglin 已提交
1092
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1102 1103 1104
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
| callback | AsyncCallback\<void\> | 是 | 停止ServiceExtensionAbility的回调函数。 |
M
m00512953 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1126 1127
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1128 1129 1130 1131 1132 1133 1134 1135
  };
  var accountId = 100;

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1136
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1137 1138 1139 1140 1141 1142 1143
        return;
      }
      // 执行正常业务
      console.log('stopServiceExtensionAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
1144
    console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
1145
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1146 1147 1148
  }
  ```

Z
zhongjianfei 已提交
1149
## UIAbilityContext.stopServiceExtensionAbilityWithAccount
M
m00512953 已提交
1150 1151 1152

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

1153
停止同一应用程序内指定账户的服务(Promise形式)。
M
m00512953 已提交
1154

D
donglin 已提交
1155
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1165 1166
| want | [Want](js-apis-application-want.md) | 是 | 停止ServiceExtensionAbility的want信息。 |
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1188 1189
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
  };
  var accountId = 100;

  try {
    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
      .then((data) => {
        // 执行正常业务
        console.log('stopServiceExtensionAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1202
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1203 1204 1205
      });
  } catch (paramError) {
    // 处理入参错误异常
1206
    console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
1207
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1208 1209 1210
  }
  ```

Z
zhongjianfei 已提交
1211
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

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

停止Ability自身(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1223
| callback | AsyncCallback&lt;void&gt; | 是 | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  try {
    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');
    });
  } catch (error) {
    // 捕获同步的参数错误
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
  }
M
m00512953 已提交
1255 1256 1257
  ```


Z
zhongjianfei 已提交
1258
## UIAbilityContext.terminateSelf
M
m00512953 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

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

停止Ability自身(promise形式)。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
1270
| Promise&lt;void&gt; | 停止Ability自身的回调函数。 |
M
m00512953 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
1286 1287 1288 1289 1290 1291 1292 1293 1294
  try {
    this.context.terminateSelf()
      .then(() => {
        // 执行正常业务
        console.log('terminateSelf succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1295
        ' error.message: ' + JSON.stringify(error.message));
1296 1297 1298
      });
  } catch (error) {
    // 捕获同步的参数错误
M
m00512953 已提交
1299
    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1300
    ' error.message: ' + JSON.stringify(error.message));
1301
  }
M
m00512953 已提交
1302 1303 1304
  ```


Z
zhongjianfei 已提交
1305
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1306 1307 1308

terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void;

M
m00512953 已提交
1309
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(callback形式)。
M
m00512953 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给调用startAbilityForResult&nbsp;接口调用方的相关信息。 |
| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回停止结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
1335 1336
    bundleName: "com.example.myapplication",
    abilityName: "MainAbility"
M
m00512953 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  }
  var resultCode = 100;
  // 返回给接口调用方AbilityResult信息
  var abilityResult = {
    want,
    resultCode
  }

  try {
    this.context.terminateSelfWithResult(abilityResult, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
1350
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1351 1352 1353 1354 1355 1356
        return;
      }
      // 执行正常业务
      console.log('terminateSelfWithResult succeed');
    });
  } catch (paramError) {
1357 1358 1359
    // 处理入参错误异常
    console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(paramError.code) +
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1360 1361 1362 1363
  }
  ```


Z
zhongjianfei 已提交
1364
## UIAbilityContext.terminateSelfWithResult
M
m00512953 已提交
1365 1366 1367

terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;;

M
m00512953 已提交
1368
停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
M
m00512953 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给startAbilityForResult&nbsp;调用方的信息。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | promise形式返回停止结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |


**示例:**

  ```ts
  var want = {
1400 1401
    bundleName: "com.example.myapplication",
    abilityName: "MainAbility"
M
m00512953 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
  }
  var resultCode = 100;
  // 返回给接口调用方AbilityResult信息
  var abilityResult = {
    want,
    resultCode
  }

  try {
    this.context.terminateSelfWithResult(abilityResult)
      .then((data) => {
        // 执行正常业务
        console.log('terminateSelfWithResult succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
1419
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1420 1421 1422
      });
  } catch (paramError) {
    // 处理入参错误异常
1423
    console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(paramError.code) +
1424
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1425 1426 1427
  }
  ```

Z
zhongjianfei 已提交
1428
## UIAbilityContext.connectServiceExtensionAbility
M
m00512953 已提交
1429 1430 1431

connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;

1432
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的Ability。
M
m00512953 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1442 1443
| want | [Want](js-apis-application-want.md) | 是 | 连接ServiceExtensionAbility的want信息。 |
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。 |
M
m00512953 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1468 1469
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1470 1471
  };
  var options = {
1472 1473 1474 1475 1476 1477 1478 1479 1480
    onConnect(elementName, remote) {
      console.log('----------- onConnect -----------')
    },
    onDisconnect(elementName) {
      console.log('----------- onDisconnect -----------')
    },
    onFailed(code) {
      console.log('----------- onFailed -----------')
    }
M
m00512953 已提交
1481 1482 1483 1484 1485 1486 1487 1488
  }

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbility(want, options);
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1489
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1490 1491 1492 1493
  }
  ```


Z
zhongjianfei 已提交
1494
## UIAbilityContext.connectServiceExtensionAbilityWithAccount
M
m00512953 已提交
1495 1496 1497

connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;

1498
将当前Ability连接到一个使用AbilityInfo.AbilityType.SERVICE模板的指定account的Ability。
M
m00512953 已提交
1499

D
donglin 已提交
1500
**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1511
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
1512
| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | 与ServiceExtensionAbility建立连接后回调函数的实例。。 |
M
m00512953 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537

**返回值:**

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

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1538 1539
    bundleName: "com.example.myapplication",
    abilityName: "ServiceExtensionAbility"
M
m00512953 已提交
1540 1541 1542
  };
  var accountId = 100;
  var options = {
1543 1544 1545 1546 1547 1548 1549 1550 1551
    onConnect(elementName, remote) {
      console.log('----------- onConnect -----------')
    },
    onDisconnect(elementName) {
      console.log('----------- onDisconnect -----------')
    },
    onFailed(code) {
      console.log('----------- onFailed -----------')
    }
M
m00512953 已提交
1552 1553 1554 1555 1556 1557 1558 1559
  }

  var connection = null;
  try {
    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1560
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1561 1562 1563
  }
  ```

Z
zhongjianfei 已提交
1564
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1565 1566 1567

disconnectServiceExtensionAbility(connection: number): Promise\<void>;

1568
断开与ServiceExtensionAbility的连接(promise形式)。
M
m00512953 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1578
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
M
m00512953 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise\<void> | 返回执行结果。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  // connection为connectServiceExtensionAbility中的返回值
  var connection = 1;

  try {
    this.context.disconnectServiceExtensionAbility(connection)
      .then((data) => {
        // 执行正常业务
        console.log('disconnectServiceExtensionAbility succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
          ' error.message: ' + JSON.stringify(error.message));
      });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
      ' error.message: ' + JSON.stringify(paramError.message));
  }
  ```

Z
zhongjianfei 已提交
1621
## UIAbilityContext.disconnectServiceExtensionAbility
M
m00512953 已提交
1622 1623 1624

disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void;

1625
断开与ServiceExtensionAbility的连接(callback形式)。
M
m00512953 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
1635
| connection | number | 是 | 连接的ServiceExtensionAbility的数字代码,即connectServiceExtensionAbility返回的connectionId。 |
1636
| callback | AsyncCallback\<void> | 是 | callback形式返回断开连接的结果。 |
M
m00512953 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  // connection为connectServiceExtensionAbility中的返回值
  var connection = 1;

  try {
    this.context.disconnectServiceExtensionAbility(connection, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1660
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1661 1662 1663 1664 1665 1666 1667 1668
        return;
      }
      // 执行正常业务
      console.log('disconnectServiceExtensionAbility succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1669
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1670 1671 1672
  }
  ```

Z
zhongjianfei 已提交
1673
## UIAbilityContext.startAbilityByCall
M
m00512953 已提交
1674 1675 1676 1677 1678

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

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

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

M
m00512953 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

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

**返回值:**

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

**示例:**

  后台启动:

  ```ts
  var caller = undefined;

  // 后台启动Ability,不配置parameters
  var wantBackground = {
1707 1708 1709 1710
    bundleName: "com.example.myservice",
    moduleName: "entry",
    abilityName: "MainAbility",
    deviceId: ""
M
m00512953 已提交
1711 1712 1713 1714 1715 1716 1717 1718 1719
  };

  try {
    this.context.startAbilityByCall(wantBackground)
      .then((obj) => {
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
1720 1721 1722 1723
      // 处理业务逻辑错误
      console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
    });
M
m00512953 已提交
1724 1725 1726
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1727
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
  }
  ```

  前台启动:

  ```ts
  var caller = undefined;

  // 前台启动Ability,将parameters中的"ohos.aafwk.param.callAbilityToForeground"配置为true
  var wantForeground = {
1738 1739 1740 1741 1742 1743 1744
    bundleName: "com.example.myservice",
    moduleName: "entry",
    abilityName: "MainAbility",
    deviceId: "",
    parameters: {
      "ohos.aafwk.param.callAbilityToForeground": true
    }
M
m00512953 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753
  };

  try {
    this.context.startAbilityByCall(wantForeground)
      .then((obj) => {
        // 执行正常业务
        caller = obj;
        console.log('startAbilityByCall succeed');
      }).catch((error) => {
1754 1755 1756 1757
      // 处理业务逻辑错误
      console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
      ' error.message: ' + JSON.stringify(error.message));
    });
M
m00512953 已提交
1758 1759 1760
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1761
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1762 1763 1764
  }
  ```

Z
zhongjianfei 已提交
1765
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1766 1767 1768

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

1769
根据want和accountId启动Ability(callback形式)。
M
m00512953 已提交
1770

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

D
donglin 已提交
1776
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1787
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1820
    bundleName: "com.example.myapplication",
M
m00512953 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829
    abilityName: "MainAbility"
  };
  var accountId = 100;

  try {
    this.context.startAbilityWithAccount(want, accountId, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1830
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1831 1832 1833 1834 1835 1836 1837 1838
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
    console.log('error.code: ' + JSON.stringify(paramError.code) +
1839
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1840 1841 1842 1843
  }
  ```


Z
zhongjianfei 已提交
1844
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1845 1846 1847

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

1848
根据want、accountId及startOptions启动Ability(callback形式)。
M
m00512953 已提交
1849

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

D
donglin 已提交
1855
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1866
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。|
1867
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
M
m00512953 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1900
    bundleName: "com.example.myapplication",
M
m00512953 已提交
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
    abilityName: "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0
  };

  try {
    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
      if (error.code) {
        // 处理业务逻辑错误
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1913
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
1914 1915 1916 1917 1918 1919 1920
        return;
      }
      // 执行正常业务
      console.log('startAbilityWithAccount succeed');
    });
  } catch (paramError) {
    // 处理入参错误异常
1921
    console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
1922
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
1923 1924 1925 1926
  }
  ```


Z
zhongjianfei 已提交
1927
## UIAbilityContext.startAbilityWithAccount
M
m00512953 已提交
1928 1929 1930

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

1931
根据want、accountId和startOptions启动Ability(Promise形式)。
M
m00512953 已提交
1932

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

D
donglin 已提交
1938
**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS,当accountId为当前用户时,不需要校验该权限。
M
m00512953 已提交
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1949
| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getCreatedOsAccountsCount)。 |
M
m00512953 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |

**错误码:**

| 错误码ID | 错误信息 |
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 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. |

**示例:**

  ```ts
  var want = {
    deviceId: "",
1982
    bundleName: "com.example.myapplication",
M
m00512953 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    abilityName: "MainAbility"
  };
  var accountId = 100;
  var options = {
    windowMode: 0
  };

  try {
    this.context.startAbilityWithAccount(want, accountId, options)
      .then((data) => {
        // 执行正常业务
        console.log('startAbilityWithAccount succeed');
      })
      .catch((error) => {
        // 处理业务逻辑错误
        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1999
        ' error.message: ' + JSON.stringify(error.message));
M
m00512953 已提交
2000 2001 2002
      });
  } catch (paramError) {
    // 处理入参错误异常
2003
    console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(paramError.code) +
2004
    ' error.message: ' + JSON.stringify(paramError.message));
M
m00512953 已提交
2005 2006 2007
  }
  ```

Z
zhongjianfei 已提交
2008
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025

setMissionLabel(label: string, callback:AsyncCallback&lt;void&gt;): void;

设置ability在任务中显示的名称(callback形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| label | string | 是 | 显示名称。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |

**示例:**

  ```ts
2026
  this.context.setMissionLabel("test", (result) => {
2027
    console.log('setMissionLabel:' + JSON.stringify(result));
M
m00512953 已提交
2028 2029 2030
  });
  ```

Z
zhongjianfei 已提交
2031
## UIAbilityContext.setMissionLabel
M
m00512953 已提交
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054

setMissionLabel(label: string): Promise&lt;void&gt;;

设置ability在任务中显示的名称(promise形式)。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| label | string | 是 | 显示名称。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |

**示例:**

  ```ts
  this.context.setMissionLabel("test").then(() => {
2055
    console.log('success');
M
m00512953 已提交
2056
  }).catch((error) => {
2057
    console.log('failed:' + JSON.stringify(error));
M
m00512953 已提交
2058 2059
  });
  ```
Z
zhongjianfei 已提交
2060
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079

setMissionIcon(icon: image.PixelMap, callback:AsyncCallback\<void>): void;

设置当前ability在任务中显示的图标(callback形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |
| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |

**示例:**

  ```ts
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
  import image from '@ohos.multimedia.image';
  var imagePixelMap;
  var color = new ArrayBuffer(0);
  var initializationOptions = {
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
M
m00512953 已提交
2092
    })
2093 2094 2095 2096 2097 2098
    .catch((err) => {
      console.log('--------- createPixelMap fail, err: ---------', err)
    });
  this.context.setMissionIcon(imagePixelMap, (err) => {
    console.log('---------- setMissionIcon fail, err: -----------', err);
  })
M
m00512953 已提交
2099 2100 2101
  ```


Z
zhongjianfei 已提交
2102
## UIAbilityContext.setMissionIcon
M
m00512953 已提交
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126

setMissionIcon(icon: image.PixelMap): Promise\<void>;

设置当前ability在任务中显示的图标(promise形式)。

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

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |

**示例:**

  ```ts
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
  var imagePixelMap;
  var color = new ArrayBuffer(0);
  var initializationOptions = {
    size: {
      height: 100,
      width: 100
    }
  };
  image.createPixelMap(color, initializationOptions)
    .then((data) => {
      imagePixelMap = data;
    })
    .catch((err) => {
      console.log('--------- createPixelMap fail, err: ---------', err)
    });
  this.context.setMissionIcon(imagePixelMap)
    .then(() => {
      console.log('-------------- setMissionIcon success -------------');
    })
    .catch((err) => {
      console.log('-------------- setMissionIcon fail, err: -------------', err);
    });
M
m00512953 已提交
2149
  ```
Z
zhongjianfei 已提交
2150
## UIAbilityContext.restoreWindowStage
M
m00512953 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166

restoreWindowStage(localStorage: LocalStorage) : void;

恢复ability中的window stage数据。

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

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| localStorage | image.LocalStorage | 是 | 用于恢复window stage的存储数据。 |

**示例:**

  ```ts
2167 2168
  var storage = new LocalStorage();
  this.context.restoreWindowStage(storage);
M
m00512953 已提交
2169 2170
  ```

Z
zhongjianfei 已提交
2171
## UIAbilityContext.isTerminating
M
m00512953 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189

isTerminating(): boolean;

查询ability是否在terminating状态。

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

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| bool | true:ability当前处于terminating状态;false:不处于terminating状态。 |

**示例:**

  ```ts
  var isTerminating = this.context.isTerminating();
  console.log('ability state :' + isTerminating);
2190
  ```