• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UIAbilityContext
2
3UIAbilityContext是UIAbility的上下文环境,继承自Context。
4
5UIAbilityContext模块提供允许访问特定UIAbility的资源的能力,包括对UIAbility的启动、停止的设置、获取caller通信接口、拉起弹窗请求用户授权等。
6
7> **说明:**
8>
9>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>  - 本模块接口仅可在Stage模型下使用。
11
12## 导入模块
13
14```ts
15import common from '@ohos.app.ability.common';
16```
17
18## 属性
19
20**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
21
22| 名称 | 类型 | 可读 | 可写 | 说明 |
23| -------- | -------- | -------- | -------- | -------- |
24| abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | 是 | 否 | Abilityinfo相关信息 |
25| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | 是 | 否 | 当前hap包的信息 |
26| config | [Configuration](js-apis-app-ability-configuration.md) | 是 | 否 | 表示配置信息。 |
27
28## UIAbilityContext.startAbility
29
30startAbility(want: Want, callback: AsyncCallback<void>): void;
31
32启动Ability(callback形式)。
33
34使用规则:
35 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
36 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
37 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
38
39**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
40
41**参数:**
42
43| 参数名 | 类型 | 必填 | 说明 |
44| -------- | -------- | -------- | -------- |
45| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
46| callback | AsyncCallback<void> | 是 | callback形式返回启动结果 |
47
48**错误码:**
49
50| 错误码ID | 错误信息 |
51| ------- | -------------------------------- |
52| 16000001 | The specified ability does not exist. |
53| 16000002 | Incorrect ability type. |
54| 16000004 | Can not start invisible component. |
55| 16000005 | The specified process does not have the permission. |
56| 16000006 | Cross-user operations are not allowed. |
57| 16000008 | The crowdtesting application expires. |
58| 16000009 | An ability cannot be started or stopped in Wukong mode. |
59| 16000010 | The call with the continuation flag is forbidden. |
60| 16000011 | The context does not exist. |
61| 16000050 | Internal error. |
62| 16000053 | The ability is not on the top of the UI. |
63| 16000055 | Installation-free timed out. |
64| 16200001 | The caller has been released. |
65
66错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
67
68**示例:**
69
70  ```ts
71  let want = {
72    bundleName: 'com.example.myapp',
73    abilityName: 'MyAbility'
74  };
75
76  try {
77    this.context.startAbility(want, (error) => {
78      if (error.code) {
79        // 处理业务逻辑错误
80        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
81          ' error.message: ' + JSON.stringify(error.message));
82        return;
83      }
84      // 执行正常业务
85      console.log('startAbility succeed');
86    });
87  } catch (paramError) {
88    // 处理入参错误异常
89    console.log('error.code: ' + JSON.stringify(paramError.code) +
90      ' error.message: ' + JSON.stringify(paramError.message));
91  }
92  ```
93
94
95## UIAbilityContext.startAbility
96
97startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void;
98
99启动Ability(callback形式)。
100
101使用规则:
102 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
103 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
104 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
105
106**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
107
108**参数:**
109
110| 参数名 | 类型 | 必填 | 说明 |
111| -------- | -------- | -------- | -------- |
112| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
113| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
114| callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 |
115
116**错误码:**
117
118| 错误码ID | 错误信息 |
119| ------- | -------------------------------- |
120| 16000001 | The specified ability does not exist. |
121| 16000002 | Incorrect ability type. |
122| 16000004 | Can not start invisible component. |
123| 16000005 | The specified process does not have the permission. |
124| 16000006 | Cross-user operations are not allowed. |
125| 16000008 | The crowdtesting application expires. |
126| 16000009 | An ability cannot be started or stopped in Wukong mode. |
127| 16000010 | The call with the continuation flag is forbidden. |
128| 16000011 | The context does not exist. |
129| 16000050 | Internal error. |
130| 16000053 | The ability is not on the top of the UI. |
131| 16000055 | Installation-free timed out. |
132| 16200001 | The caller has been released. |
133
134错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
135
136**示例:**
137
138  ```ts
139  let want = {
140    deviceId: '',
141    bundleName: 'com.extreme.test',
142    abilityName: 'MainAbility'
143  };
144  let options = {
145    windowMode: 0
146  };
147
148  try {
149    this.context.startAbility(want, options, (error) => {
150      if (error.code) {
151        // 处理业务逻辑错误
152        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
153          ' error.message: ' + JSON.stringify(error.message));
154        return;
155      }
156      // 执行正常业务
157      console.log('startAbility succeed');
158    });
159  } catch (paramError) {
160    // 处理入参错误异常
161    console.log('error.code: ' + JSON.stringify(paramError.code) +
162      ' error.message: ' + JSON.stringify(paramError.message));
163  }
164  ```
165
166## UIAbilityContext.startAbility
167
168startAbility(want: Want, options?: StartOptions): Promise<void>;
169
170启动Ability(promise形式)。
171
172使用规则:
173 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
174 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
175 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
176
177**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
178
179**参数:**
180
181| 参数名 | 类型 | 必填 | 说明 |
182| -------- | -------- | -------- | -------- |
183| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
184| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
185
186**返回值:**
187
188| 类型 | 说明 |
189| -------- | -------- |
190| Promise<void> | Promise形式返回启动结果。 |
191
192**错误码:**
193
194| 错误码ID | 错误信息 |
195| ------- | -------------------------------- |
196| 16000001 | The specified ability does not exist. |
197| 16000002 | Incorrect ability type. |
198| 16000004 | Can not start invisible component. |
199| 16000005 | The specified process does not have the permission. |
200| 16000006 | Cross-user operations are not allowed. |
201| 16000008 | The crowdtesting application expires. |
202| 16000009 | An ability cannot be started or stopped in Wukong mode. |
203| 16000010 | The call with the continuation flag is forbidden. |
204| 16000011 | The context does not exist. |
205| 16000050 | Internal error. |
206| 16000053 | The ability is not on the top of the UI. |
207| 16000055 | Installation-free timed out. |
208| 16200001 | The caller has been released. |
209
210错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
211
212**示例:**
213
214  ```ts
215  let want = {
216    bundleName: 'com.example.myapp',
217    abilityName: 'MyAbility'
218  };
219  let options = {
220  	windowMode: 0,
221  };
222
223  try {
224    this.context.startAbility(want, options)
225      .then((data) => {
226        // 执行正常业务
227        console.log('startAbility succeed');
228      })
229      .catch((error) => {
230        // 处理业务逻辑错误
231        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
232          ' error.message: ' + JSON.stringify(error.message));
233      });
234  } catch (paramError) {
235    // 处理入参错误异常
236    console.log('error.code: ' + JSON.stringify(paramError.code) +
237      ' error.message: ' + JSON.stringify(paramError.message));
238  }
239  ```
240
241
242## UIAbilityContext.startAbilityForResult
243
244startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void;
245
246启动一个Ability。Ability被启动后,有如下情况(callback形式):
247 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
248 - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
249 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
250
251使用规则:
252 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
253 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
254 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
255
256**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
257
258**参数:**
259
260| 参数名 | 类型 | 必填 | 说明 |
261| -------- | -------- | -------- | -------- |
262| want |[Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
263| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 执行结果回调函数。 |
264
265**错误码:**
266
267| 错误码ID | 错误信息 |
268| ------- | -------------------------------- |
269| 16000001 | The specified ability does not exist. |
270| 16000002 | Incorrect ability type. |
271| 16000004 | Can not start invisible component. |
272| 16000005 | The specified process does not have the permission. |
273| 16000006 | Cross-user operations are not allowed. |
274| 16000008 | The crowdtesting application expires. |
275| 16000009 | An ability cannot be started or stopped in Wukong mode. |
276| 16000010 | The call with the continuation flag is forbidden. |
277| 16000011 | The context does not exist. |
278| 16000050 | Internal error. |
279| 16000053 | The ability is not on the top of the UI. |
280| 16000055 | Installation-free timed out. |
281| 16200001 | The caller has been released. |
282
283错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
284
285**示例:**
286
287  ```ts
288  let want = {
289    deviceId: '',
290    bundleName: 'com.extreme.test',
291    abilityName: 'MainAbility'
292  };
293
294  try {
295    this.context.startAbilityForResult(want, (error, result) => {
296      if (error.code) {
297        // 处理业务逻辑错误
298        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
299          ' error.message: ' + JSON.stringify(error.message));
300        return;
301      }
302      // 执行正常业务
303      console.log('startAbilityForResult succeed, result.resultCode = ' +
304        result.resultCode)
305    });
306  } catch (paramError) {
307    // 处理入参错误异常
308    console.log('error.code: ' + JSON.stringify(paramError.code) +
309      ' error.message: ' + JSON.stringify(paramError.message));
310  }
311  ```
312
313## UIAbilityContext.startAbilityForResult
314
315startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void;
316
317启动一个Ability。Ability被启动后,有如下情况(callback形式):
318 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
319 - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
320 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
321
322使用规则:
323 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
324 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
325 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
326
327**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
328
329**参数:**
330
331| 参数名 | 类型 | 必填 | 说明 |
332| -------- | -------- | -------- | -------- |
333| want |[Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
334| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
335| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | 是 | 执行结果回调函数。 |
336
337**错误码:**
338
339| 错误码ID | 错误信息 |
340| ------- | -------------------------------- |
341| 16000001 | The specified ability does not exist. |
342| 16000002 | Incorrect ability type. |
343| 16000004 | Can not start invisible component. |
344| 16000005 | The specified process does not have the permission. |
345| 16000006 | Cross-user operations are not allowed. |
346| 16000008 | The crowdtesting application expires. |
347| 16000009 | An ability cannot be started or stopped in Wukong mode. |
348| 16000010 | The call with the continuation flag is forbidden. |
349| 16000011 | The context does not exist. |
350| 16000050 | Internal error. |
351| 16000053 | The ability is not on the top of the UI. |
352| 16000055 | Installation-free timed out. |
353| 16200001 | The caller has been released. |
354
355错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
356
357**示例:**
358
359  ```ts
360  let want = {
361    deviceId: '',
362    bundleName: 'com.extreme.test',
363    abilityName: 'MainAbility'
364  };
365  let options = {
366    windowMode: 0,
367  };
368
369try {
370  this.context.startAbilityForResult(want, options, (err, result) => {
371    if (err.code) {
372      // 处理业务逻辑错误
373      console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
374      return;
375    }
376    // 执行正常业务
377    console.info('startAbilityForResult succeed');
378  });
379} catch (err) {
380  // 处理入参错误异常
381  console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`);
382}
383  ```
384
385
386## UIAbilityContext.startAbilityForResult
387
388startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>;
389
390启动一个Ability。Ability被启动后,有如下情况(promise形式):
391 - 正常情况下可通过调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止并且返回结果给调用方。
392 - 异常情况下比如杀死Ability会返回异常信息给调用方, 异常信息中resultCode为-1。
393 - 如果被启动的Ability模式是单实例模式, 不同应用多次调用该接口启动这个Ability,当这个Ability调用[terminateSelfWithResult](#uiabilitycontextterminateselfwithresult)接口使之终止时,只将正常结果返回给最后一个调用方, 其它调用方返回异常信息, 异常信息中resultCode为-1。
394
395使用规则:
396 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
397 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
398 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
399
400**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
401
402**参数:**
403
404| 参数名 | 类型 | 必填 | 说明 |
405| -------- | -------- | -------- | -------- |
406| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
407| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
408
409
410**返回值:**
411
412| 类型 | 说明 |
413| -------- | -------- |
414| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise形式返回执行结果。 |
415
416**错误码:**
417
418| 错误码ID | 错误信息 |
419| ------- | -------------------------------- |
420| 16000001 | The specified ability does not exist. |
421| 16000002 | Incorrect ability type. |
422| 16000004 | Can not start invisible component. |
423| 16000005 | The specified process does not have the permission. |
424| 16000006 | Cross-user operations are not allowed. |
425| 16000008 | The crowdtesting application expires. |
426| 16000009 | An ability cannot be started or stopped in Wukong mode. |
427| 16000010 | The call with the continuation flag is forbidden. |
428| 16000011 | The context does not exist. |
429| 16000050 | Internal error. |
430| 16000053 | The ability is not on the top of the UI. |
431| 16000055 | Installation-free timed out. |
432| 16200001 | The caller has been released. |
433
434错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
435
436**示例:**
437
438  ```ts
439  let want = {
440    bundleName: 'com.example.myapp',
441    abilityName: 'MyAbility'
442  };
443  let options = {
444  	windowMode: 0,
445  };
446
447  try {
448    this.context.startAbilityForResult(want, options)
449      .then((result) => {
450        // 执行正常业务
451        console.log('startAbilityForResult succeed, result.resultCode = ' + result.resultCode);
452      })
453      .catch((error) => {
454        // 处理业务逻辑错误
455        console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
456          ' error.message: ' + JSON.stringify(error.message));
457      });
458  } catch (paramError) {
459    // 处理入参错误异常
460    console.log('error.code: ' + JSON.stringify(paramError.code) +
461      ' error.message: ' + JSON.stringify(paramError.message));
462  }
463  ```
464
465## UIAbilityContext.startAbilityForResultWithAccount
466
467startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void;
468
469启动一个Ability并在该Ability帐号销毁时返回执行结果(callback形式)。
470
471**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
472
473**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
474
475**系统API**: 此接口为系统接口,三方应用不支持调用。
476
477**参数:**
478
479| 参数名 | 类型 | 必填 | 说明 |
480| -------- | -------- | -------- | -------- |
481| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
482| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
483| callback | AsyncCallback\<AbilityResult\> | 是 | 启动Ability的回调函数,返回Ability结果。 |
484
485**错误码:**
486
487| 错误码ID | 错误信息 |
488| ------- | -------------------------------- |
489| 16000001 | The specified ability does not exist. |
490| 16000002 | Incorrect ability type. |
491| 16000004 | Can not start invisible component. |
492| 16000005 | The specified process does not have the permission. |
493| 16000006 | Cross-user operations are not allowed. |
494| 16000008 | The crowdtesting application expires. |
495| 16000009 | An ability cannot be started or stopped in Wukong mode. |
496| 16000010 | The call with the continuation flag is forbidden. |
497| 16000011 | The context does not exist. |
498| 16000050 | Internal error. |
499| 16000053 | The ability is not on the top of the UI. |
500| 16000055 | Installation-free timed out. |
501| 16200001 | The caller has been released. |
502
503错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
504
505**示例:**
506
507  ```ts
508  let want = {
509    deviceId: '',
510    bundleName: 'com.extreme.test',
511    abilityName: 'MainAbility'
512  };
513  let accountId = 100;
514
515  try {
516    this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => {
517      if (error.code) {
518        // 处理业务逻辑错误
519        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
520          ' error.message: ' + JSON.stringify(error.message));
521        return;
522      }
523      // 执行正常业务
524      console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' +
525        result.resultCode)
526    });
527  } catch (paramError) {
528    // 处理入参错误异常
529    console.log('error.code: ' + JSON.stringify(paramError.code) +
530      ' error.message: ' + JSON.stringify(paramError.message));
531  }
532  ```
533
534
535## UIAbilityContext.startAbilityForResultWithAccount
536
537startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
538
539启动一个Ability并在该Ability帐号销毁时返回执行结果(callback形式)。
540
541**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
542
543**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
544
545**系统API**: 此接口为系统接口,三方应用不支持调用。
546
547**参数:**
548
549| 参数名 | 类型 | 必填 | 说明 |
550| -------- | -------- | -------- | -------- |
551| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
552| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
553| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
554| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
555
556**错误码:**
557
558| 错误码ID | 错误信息 |
559| ------- | -------------------------------- |
560| 16000001 | The specified ability does not exist. |
561| 16000002 | Incorrect ability type. |
562| 16000004 | Can not start invisible component. |
563| 16000005 | The specified process does not have the permission. |
564| 16000006 | Cross-user operations are not allowed. |
565| 16000008 | The crowdtesting application expires. |
566| 16000009 | An ability cannot be started or stopped in Wukong mode. |
567| 16000010 | The call with the continuation flag is forbidden. |
568| 16000011 | The context does not exist. |
569| 16000050 | Internal error. |
570| 16000053 | The ability is not on the top of the UI. |
571| 16000055 | Installation-free timed out. |
572| 16200001 | The caller has been released. |
573
574错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
575
576**示例:**
577
578  ```ts
579  let want = {
580    deviceId: '',
581    bundleName: 'com.extreme.test',
582    abilityName: 'MainAbility'
583  };
584  let accountId = 100;
585  let options = {
586    windowMode: 0
587  };
588
589  try {
590    this.context.startAbilityForResultWithAccount(want, accountId, options, (error, result) => {
591      if (error.code) {
592        // 处理业务逻辑错误
593        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
594          ' error.message: ' + JSON.stringify(error.message));
595        return;
596      }
597      // 执行正常业务
598      console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' +
599        result.resultCode)
600    });
601  } catch (paramError) {
602    // 处理入参错误异常
603    console.log('error.code: ' + JSON.stringify(paramError.code) +
604      ' error.message: ' + JSON.stringify(paramError.message));
605  }
606  ```
607
608
609## UIAbilityContext.startAbilityForResultWithAccount
610
611startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\>;
612
613启动一个Ability并在该Ability帐号销毁时返回执行结果(promise形式)。
614
615**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
616
617**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
618
619**系统API**: 此接口为系统接口,三方应用不支持调用。
620
621**参数:**
622
623| 参数名 | 类型 | 必填 | 说明 |
624| -------- | -------- | -------- | -------- |
625| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
626| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
627| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
628
629**返回值:**
630
631| 类型 | 说明 |
632| -------- | -------- |
633| Promise&lt;AbilityResult&gt; | 返回一个Promise,包含Ability结果。 |
634
635**错误码:**
636
637| 错误码ID | 错误信息 |
638| ------- | -------------------------------- |
639| 16000001 | The specified ability does not exist. |
640| 16000002 | Incorrect ability type. |
641| 16000004 | Can not start invisible component. |
642| 16000005 | The specified process does not have the permission. |
643| 16000006 | Cross-user operations are not allowed. |
644| 16000008 | The crowdtesting application expires. |
645| 16000009 | An ability cannot be started or stopped in Wukong mode. |
646| 16000010 | The call with the continuation flag is forbidden. |
647| 16000011 | The context does not exist. |
648| 16000050 | Internal error. |
649| 16000053 | The ability is not on the top of the UI. |
650| 16000055 | Installation-free timed out. |
651| 16200001 | The caller has been released. |
652
653错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
654
655**示例:**
656
657  ```ts
658  let want = {
659    deviceId: '',
660    bundleName: 'com.extreme.test',
661    abilityName: 'MainAbility'
662  };
663  let accountId = 100;
664  let options = {
665    windowMode: 0
666  };
667
668  try {
669    this.context.startAbilityForResultWithAccount(want, accountId, options)
670      .then((result) => {
671        // 执行正常业务
672        console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' +
673          result.resultCode)
674      })
675      .catch((error) => {
676        // 处理业务逻辑错误
677        console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
678          ' error.message: ' + JSON.stringify(error.message));
679      });
680  } catch (paramError) {
681    // 处理入参错误异常
682    console.log('error.code: ' + JSON.stringify(paramError.code) +
683      ' error.message: ' + JSON.stringify(paramError.message));
684  }
685  ```
686## UIAbilityContext.startServiceExtensionAbility
687
688startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
689
690启动一个新的ServiceExtensionAbility(callback形式)。
691
692**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
693
694**系统API**: 此接口为系统接口,三方应用不支持调用。
695
696**参数:**
697
698| 参数名 | 类型 | 必填 | 说明 |
699| -------- | -------- | -------- | -------- |
700| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
701| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
702
703**错误码:**
704
705| 错误码ID | 错误信息 |
706| ------- | -------------------------------- |
707| 16000001 | The specified ability does not exist. |
708| 16000002 | Incorrect ability type. |
709| 16000005 | The specified process does not have the permission. |
710| 16000006 | Cross-user operations are not allowed. |
711| 16000008 | The crowdtesting application expires. |
712| 16000011 | The context does not exist. |
713| 16000050 | Internal error. |
714| 16200001 | The caller has been released. |
715
716错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
717
718**示例:**
719
720  ```ts
721  let want = {
722    deviceId: '',
723    bundleName: 'com.extreme.test',
724    abilityName: 'MainAbility'
725  };
726
727  try {
728    this.context.startServiceExtensionAbility(want, (error) => {
729      if (error.code) {
730        // 处理业务逻辑错误
731        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
732          ' error.message: ' + JSON.stringify(error.message));
733        return;
734      }
735      // 执行正常业务
736      console.log('startServiceExtensionAbility succeed');
737    });
738  } catch (paramError) {
739    // 处理入参错误异常
740    console.log('error.code: ' + JSON.stringify(paramError.code) +
741      ' error.message: ' + JSON.stringify(paramError.message));
742  }
743  ```
744
745## UIAbilityContext.startServiceExtensionAbility
746
747startServiceExtensionAbility(want: Want): Promise\<void>;
748
749启动一个新的ServiceExtensionAbility(Promise形式)。
750
751**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
752
753**系统API**: 此接口为系统接口,三方应用不支持调用。
754
755**参数:**
756
757| 参数名 | 类型 | 必填 | 说明 |
758| -------- | -------- | -------- | -------- |
759| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
760
761**错误码:**
762
763| 错误码ID | 错误信息 |
764| ------- | -------------------------------- |
765| 16000001 | The specified ability does not exist. |
766| 16000002 | Incorrect ability type. |
767| 16000005 | The specified process does not have the permission. |
768| 16000006 | Cross-user operations are not allowed. |
769| 16000008 | The crowdtesting application expires. |
770| 16000011 | The context does not exist. |
771| 16000050 | Internal error. |
772| 16200001 | The caller has been released. |
773
774错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
775
776**示例:**
777
778  ```ts
779  let want = {
780    deviceId: '',
781    bundleName: 'com.extreme.test',
782    abilityName: 'MainAbility'
783  };
784
785try {
786  this.context.startServiceExtensionAbility(want)
787    .then(() => {
788      // 执行正常业务
789      console.info('startServiceExtensionAbility succeed');
790    })
791    .catch((err) => {
792      // 处理业务逻辑错误
793      console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
794    });
795} catch (err) {
796  // 处理入参错误异常
797  console.error(`startServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
798}
799  ```
800
801## UIAbilityContext.startServiceExtensionAbilityWithAccount
802
803startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
804
805启动一个新的ServiceExtensionAbility(callback形式)。
806
807**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
808
809**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
810
811**系统API**: 此接口为系统接口,三方应用不支持调用。
812
813**参数:**
814
815| 参数名 | 类型 | 必填 | 说明 |
816| -------- | -------- | -------- | -------- |
817| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
818| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
819| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
820
821**错误码:**
822
823| 错误码ID | 错误信息 |
824| ------- | -------------------------------- |
825| 16000001 | The specified ability does not exist. |
826| 16000002 | Incorrect ability type. |
827| 16000005 | The specified process does not have the permission. |
828| 16000006 | Cross-user operations are not allowed. |
829| 16000008 | The crowdtesting application expires. |
830| 16000011 | The context does not exist. |
831| 16000050 | Internal error. |
832| 16200001 | The caller has been released. |
833
834错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
835
836**示例:**
837
838  ```ts
839  let want = {
840    deviceId: '',
841    bundleName: 'com.extreme.test',
842    abilityName: 'MainAbility'
843  };
844  let accountId = 100;
845
846  try {
847    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
848      if (error.code) {
849        // 处理业务逻辑错误
850        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
851          ' error.message: ' + JSON.stringify(error.message));
852        return;
853      }
854      // 执行正常业务
855      console.log('startServiceExtensionAbilityWithAccount succeed');
856    });
857  } catch (paramError) {
858    // 处理入参错误异常
859    console.log('error.code: ' + JSON.stringify(paramError.code) +
860      ' error.message: ' + JSON.stringify(paramError.message));
861  }
862  ```
863
864## UIAbilityContext.startServiceExtensionAbilityWithAccount
865
866startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
867
868启动一个新的ServiceExtensionAbility(Promise形式)。
869
870**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
871
872**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
873
874**系统API**: 此接口为系统接口,三方应用不支持调用。
875
876**参数:**
877
878| 参数名 | 类型 | 必填 | 说明 |
879| -------- | -------- | -------- | -------- |
880| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
881| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
882
883**错误码:**
884
885| 错误码ID | 错误信息 |
886| ------- | -------------------------------- |
887| 16000001 | The specified ability does not exist. |
888| 16000002 | Incorrect ability type. |
889| 16000005 | The specified process does not have the permission. |
890| 16000006 | Cross-user operations are not allowed. |
891| 16000008 | The crowdtesting application expires. |
892| 16000011 | The context does not exist. |
893| 16000050 | Internal error. |
894| 16200001 | The caller has been released. |
895
896错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
897
898**示例:**
899
900  ```ts
901  let want = {
902    deviceId: '',
903    bundleName: 'com.extreme.test',
904    abilityName: 'MainAbility'
905  };
906  let accountId = 100;
907
908  try {
909    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
910      .then((data) => {
911        // 执行正常业务
912        console.log('startServiceExtensionAbilityWithAccount succeed');
913      })
914      .catch((error) => {
915        // 处理业务逻辑错误
916        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
917          ' error.message: ' + JSON.stringify(error.message));
918      });
919  } catch (paramError) {
920    // 处理入参错误异常
921    console.log('error.code: ' + JSON.stringify(paramError.code) +
922      ' error.message: ' + JSON.stringify(paramError.message));
923  }
924  ```
925## UIAbilityContext.stopServiceExtensionAbility
926
927stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
928
929停止同一应用程序内的服务(callback形式)。
930
931**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
932
933**系统API**: 此接口为系统接口,三方应用不支持调用。
934
935**参数:**
936
937| 参数名 | 类型 | 必填 | 说明 |
938| -------- | -------- | -------- | -------- |
939| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
940| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
941
942**错误码:**
943
944| 错误码ID | 错误信息 |
945| ------- | -------------------------------- |
946| 16000001 | The specified ability does not exist. |
947| 16000002 | Incorrect ability type. |
948| 16000005 | The specified process does not have the permission. |
949| 16000006 | Cross-user operations are not allowed. |
950| 16000011 | The context does not exist. |
951| 16000050 | Internal error. |
952| 16200001 | The caller has been released. |
953
954错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
955
956**示例:**
957
958  ```ts
959  let want = {
960    deviceId: '',
961    bundleName: 'com.extreme.test',
962    abilityName: 'MainAbility'
963  };
964
965  try {
966    this.context.stopServiceExtensionAbility(want, (error) => {
967      if (error.code) {
968        // 处理业务逻辑错误
969        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
970          ' error.message: ' + JSON.stringify(error.message));
971        return;
972      }
973      // 执行正常业务
974      console.log('stopServiceExtensionAbility succeed');
975    });
976  } catch (paramError) {
977    // 处理入参错误异常
978    console.log('error.code: ' + JSON.stringify(paramError.code) +
979      ' error.message: ' + JSON.stringify(paramError.message));
980  }
981  ```
982
983## UIAbilityContext.stopServiceExtensionAbility
984
985stopServiceExtensionAbility(want: Want): Promise\<void>;
986
987停止同一应用程序内的服务(Promise形式)。
988
989**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
990
991**系统API**: 此接口为系统接口,三方应用不支持调用。
992
993**参数:**
994
995| 参数名 | 类型 | 必填 | 说明 |
996| -------- | -------- | -------- | -------- |
997| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
998
999**错误码:**
1000
1001| 错误码ID | 错误信息 |
1002| ------- | -------------------------------- |
1003| 16000001 | The specified ability does not exist. |
1004| 16000002 | Incorrect ability type. |
1005| 16000005 | The specified process does not have the permission. |
1006| 16000006 | Cross-user operations are not allowed. |
1007| 16000011 | The context does not exist. |
1008| 16000050 | Internal error. |
1009| 16200001 | The caller has been released. |
1010
1011错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1012
1013**示例:**
1014
1015  ```ts
1016  let want = {
1017    deviceId: '',
1018    bundleName: 'com.extreme.test',
1019    abilityName: 'MainAbility'
1020  };
1021
1022  try {
1023    this.context.stopServiceExtensionAbility(want)
1024      .then((data) => {
1025        // 执行正常业务
1026        console.log('stopServiceExtensionAbility succeed');
1027      })
1028      .catch((error) => {
1029        // 处理业务逻辑错误
1030        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1031          ' error.message: ' + JSON.stringify(error.message));
1032      });
1033  } catch (paramError) {
1034    // 处理入参错误异常
1035    console.log('error.code: ' + JSON.stringify(paramError.code) +
1036      ' error.message: ' + JSON.stringify(paramError.message));
1037  }
1038  ```
1039
1040## UIAbilityContext.stopServiceExtensionAbilityWithAccount
1041
1042stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
1043
1044使用帐户停止同一应用程序内的服务(callback形式)。
1045
1046**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1047
1048**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1049
1050**系统API**: 此接口为系统接口,三方应用不支持调用。
1051
1052**参数:**
1053
1054| 参数名 | 类型 | 必填 | 说明 |
1055| -------- | -------- | -------- | -------- |
1056| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1057| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1058| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
1059
1060**错误码:**
1061
1062| 错误码ID | 错误信息 |
1063| ------- | -------------------------------- |
1064| 16000001 | The specified ability does not exist. |
1065| 16000002 | Incorrect ability type. |
1066| 16000005 | The specified process does not have the permission. |
1067| 16000006 | Cross-user operations are not allowed. |
1068| 16000011 | The context does not exist. |
1069| 16000050 | Internal error. |
1070| 16200001 | The caller has been released. |
1071
1072错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1073
1074**示例:**
1075
1076  ```ts
1077  let want = {
1078    deviceId: '',
1079    bundleName: 'com.extreme.test',
1080    abilityName: 'MainAbility'
1081  };
1082  let accountId = 100;
1083
1084  try {
1085    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
1086      if (error.code) {
1087        // 处理业务逻辑错误
1088        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1089          ' error.message: ' + JSON.stringify(error.message));
1090        return;
1091      }
1092      // 执行正常业务
1093      console.log('stopServiceExtensionAbilityWithAccount succeed');
1094    });
1095  } catch (paramError) {
1096    // 处理入参错误异常
1097    console.log('error.code: ' + JSON.stringify(paramError.code) +
1098      ' error.message: ' + JSON.stringify(paramError.message));
1099  }
1100  ```
1101
1102## UIAbilityContext.stopServiceExtensionAbilityWithAccount
1103
1104stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
1105
1106使用帐户停止同一应用程序内的服务(Promise形式)。
1107
1108**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1109
1110**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1111
1112**系统API**: 此接口为系统接口,三方应用不支持调用。
1113
1114**参数:**
1115
1116| 参数名 | 类型 | 必填 | 说明 |
1117| -------- | -------- | -------- | -------- |
1118| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1119| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1120
1121**错误码:**
1122
1123| 错误码ID | 错误信息 |
1124| ------- | -------------------------------- |
1125| 16000001 | The specified ability does not exist. |
1126| 16000002 | Incorrect ability type. |
1127| 16000005 | The specified process does not have the permission. |
1128| 16000006 | Cross-user operations are not allowed. |
1129| 16000011 | The context does not exist. |
1130| 16000050 | Internal error. |
1131| 16200001 | The caller has been released. |
1132
1133错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1134
1135**示例:**
1136
1137  ```ts
1138  let want = {
1139    deviceId: '',
1140    bundleName: 'com.extreme.test',
1141    abilityName: 'MainAbility'
1142  };
1143  let accountId = 100;
1144
1145  try {
1146    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
1147      .then((data) => {
1148        // 执行正常业务
1149        console.log('stopServiceExtensionAbilityWithAccount succeed');
1150      })
1151      .catch((error) => {
1152        // 处理业务逻辑错误
1153        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1154          ' error.message: ' + JSON.stringify(error.message));
1155      });
1156  } catch (paramError) {
1157    // 处理入参错误异常
1158    console.log('error.code: ' + JSON.stringify(paramError.code) +
1159      ' error.message: ' + JSON.stringify(paramError.message));
1160  }
1161  ```
1162
1163## UIAbilityContext.terminateSelf
1164
1165terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1166
1167停止Ability自身(callback形式)。
1168
1169**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1170
1171**参数:**
1172
1173| 参数名 | 类型 | 必填 | 说明 |
1174| -------- | -------- | -------- | -------- |
1175| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1176
1177**错误码:**
1178
1179| 错误码ID | 错误信息 |
1180| ------- | -------------------------------- |
1181| 16000001 | The specified ability does not exist. |
1182| 16000004 | Can not start invisible component. |
1183| 16000005 | The specified process does not have the permission. |
1184| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1185| 16000011 | The context does not exist. |
1186| 16000050 | Internal error. |
1187
1188错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1189
1190**示例:**
1191
1192  ```ts
1193  this.context.terminateSelf((error) => {
1194    if (error.code) {
1195      // 处理业务逻辑错误
1196      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1197        ' error.message: ' + JSON.stringify(error.message));
1198      return;
1199    }
1200    // 执行正常业务
1201    console.log('terminateSelf succeed');
1202  });
1203  ```
1204
1205
1206## UIAbilityContext.terminateSelf
1207
1208terminateSelf(): Promise&lt;void&gt;;
1209
1210停止Ability自身(promise形式)。
1211
1212**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1213
1214**返回值:**
1215
1216| 类型 | 说明 |
1217| -------- | -------- |
1218| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1219
1220**错误码:**
1221
1222| 错误码ID | 错误信息 |
1223| ------- | -------------------------------- |
1224| 16000001 | The specified ability does not exist. |
1225| 16000004 | Can not start invisible component. |
1226| 16000005 | The specified process does not have the permission. |
1227| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1228| 16000011 | The context does not exist. |
1229| 16000050 | Internal error. |
1230
1231错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1232
1233**示例:**
1234
1235  ```ts
1236  this.context.terminateSelf().then((data) => {
1237    // 执行正常业务
1238    console.log('terminateSelf succeed');
1239  }).catch((error) => {
1240    // 处理业务逻辑错误
1241    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1242      ' error.message: ' + JSON.stringify(error.message));
1243  });
1244  ```
1245
1246
1247## UIAbilityContext.terminateSelfWithResult
1248
1249terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void;
1250
1251停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(callback形式)。
1252
1253**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1254
1255**参数:**
1256
1257| 参数名 | 类型 | 必填 | 说明 |
1258| -------- | -------- | -------- | -------- |
1259| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给调用startAbilityForResult&nbsp;接口调用方的相关信息。 |
1260| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回停止结果。 |
1261
1262**错误码:**
1263
1264| 错误码ID | 错误信息 |
1265| ------- | -------------------------------- |
1266| 16000001 | The specified ability does not exist. |
1267| 16000004 | Can not start invisible component. |
1268| 16000005 | The specified process does not have the permission. |
1269| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1270| 16000011 | The context does not exist. |
1271| 16000050 | Internal error. |
1272
1273错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1274
1275**示例:**
1276
1277  ```ts
1278  let want = {
1279    bundleName: 'com.extreme.myapplication',
1280    abilityName: 'SecondAbility'
1281  }
1282  let resultCode = 100;
1283  // 返回给接口调用方AbilityResult信息
1284  let abilityResult = {
1285    want,
1286    resultCode
1287  }
1288
1289  try {
1290    this.context.terminateSelfWithResult(abilityResult, (error) => {
1291      if (error.code) {
1292        // 处理业务逻辑错误
1293        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
1294          ' error.message: ' + JSON.stringify(error.message));
1295        return;
1296      }
1297      // 执行正常业务
1298      console.log('terminateSelfWithResult succeed');
1299    });
1300  } catch (paramError) {
1301      // 处理入参错误异常
1302      console.log('error.code: ' + JSON.stringify(paramError.code) +
1303        ' error.message: ' + JSON.stringify(paramError.message));
1304  }
1305  ```
1306
1307
1308## UIAbilityContext.terminateSelfWithResult
1309
1310terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;;
1311
1312停止当前的Ability。如果该Ability是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时会将结果返回给调用者,如果该Ability不是通过调用[startAbilityForResult](#uiabilitycontextstartabilityforresult)接口被拉起的,调用terminateSelfWithResult接口时不会有结果返回给调用者(promise形式)。
1313
1314**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1315
1316**参数:**
1317
1318| 参数名 | 类型 | 必填 | 说明 |
1319| -------- | -------- | -------- | -------- |
1320| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | 是 | 返回给startAbilityForResult&nbsp;调用方的信息。 |
1321
1322**返回值:**
1323
1324| 类型 | 说明 |
1325| -------- | -------- |
1326| Promise&lt;void&gt; | promise形式返回停止结果。 |
1327
1328**错误码:**
1329
1330| 错误码ID | 错误信息 |
1331| ------- | -------------------------------- |
1332| 16000001 | The specified ability does not exist. |
1333| 16000004 | Can not start invisible component. |
1334| 16000005 | The specified process does not have the permission. |
1335| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1336| 16000011 | The context does not exist. |
1337| 16000050 | Internal error. |
1338
1339错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1340
1341**示例:**
1342
1343  ```ts
1344  let want = {
1345    bundleName: 'com.extreme.myapplication',
1346    abilityName: 'SecondAbility'
1347  }
1348  let resultCode = 100;
1349  // 返回给接口调用方AbilityResult信息
1350  let abilityResult = {
1351    want,
1352    resultCode
1353  }
1354
1355  try {
1356    this.context.terminateSelfWithResult(abilityResult)
1357      .then((data) => {
1358        // 执行正常业务
1359        console.log('terminateSelfWithResult succeed');
1360      })
1361      .catch((error) => {
1362        // 处理业务逻辑错误
1363        console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
1364          ' error.message: ' + JSON.stringify(error.message));
1365      });
1366  } catch (paramError) {
1367    // 处理入参错误异常
1368    console.log('error.code: ' + JSON.stringify(paramError.code) +
1369      ' error.message: ' + JSON.stringify(paramError.message));
1370  }
1371  ```
1372
1373## UIAbilityContext.connectServiceExtensionAbility
1374
1375connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1376
1377使用AbilityInfo.AbilityType.SERVICE模板将当前Ability连接到一个Ability。
1378
1379**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1380
1381**参数:**
1382
1383| 参数名 | 类型 | 必填 | 说明 |
1384| -------- | -------- | -------- | -------- |
1385| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1386| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 否 | 远端对象实例。 |
1387
1388**返回值:**
1389
1390| 类型 | 说明 |
1391| -------- | -------- |
1392| number | 返回Ability连接的结果code。 |
1393
1394**错误码:**
1395
1396| 错误码ID | 错误信息 |
1397| ------- | -------------------------------- |
1398| 16000001 | The specified ability does not exist. |
1399| 16000005 | The specified process does not have the permission. |
1400| 16000011 | The context does not exist. |
1401| 16000050 | Internal error. |
1402
1403错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1404
1405**示例:**
1406
1407  ```ts
1408let want = {
1409  deviceId: '',
1410  bundleName: 'com.example.myapplication',
1411  abilityName: 'ServiceExtensionAbility'
1412};
1413let commRemote;
1414let options = {
1415  onConnect(elementName, remote) {
1416    commRemote = remote;
1417    console.info('onConnect...')
1418  },
1419  onDisconnect(elementName) {
1420    console.info('onDisconnect...')
1421  },
1422  onFailed(code) {
1423    console.info('onFailed...')
1424  }
1425
1426  let connection = null;
1427  try {
1428    connection = this.context.connectServiceExtensionAbility(want, options);
1429  } catch (paramError) {
1430    // 处理入参错误异常
1431    console.log('error.code: ' + JSON.stringify(paramError.code) +
1432      ' error.message: ' + JSON.stringify(paramError.message));
1433  }
1434  ```
1435
1436
1437## UIAbilityContext.connectServiceExtensionAbilityWithAccount
1438
1439connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1440
1441使用AbilityInfo.AbilityType.SERVICE模板和account将当前Ability连接到一个Ability。
1442
1443**需要权限:** ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1444
1445**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1446
1447**系统API**: 此接口为系统接口,三方应用不支持调用。
1448
1449**参数:**
1450
1451| 参数名 | 类型 | 必填 | 说明 |
1452| -------- | -------- | -------- | -------- |
1453| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1454| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1455| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 否 | 远端对象实例。 |
1456
1457**返回值:**
1458
1459| 类型 | 说明 |
1460| -------- | -------- |
1461| number | 返回Ability连接的结果code。 |
1462
1463**错误码:**
1464
1465| 错误码ID | 错误信息 |
1466| ------- | -------------------------------- |
1467| 16000001 | The specified ability does not exist. |
1468| 16000005 | The specified process does not have the permission. |
1469| 16000011 | The context does not exist. |
1470| 16000050 | Internal error. |
1471
1472错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1473
1474**示例:**
1475
1476  ```ts
1477let want = {
1478  deviceId: '',
1479  bundleName: 'com.example.myapplication',
1480  abilityName: 'ServiceExtensionAbility'
1481};
1482let accountId = 100;
1483let commRemote;
1484let options = {
1485  onConnect(elementName, remote) {
1486    commRemote = remote;
1487    console.info('onConnect...')
1488  },
1489  onDisconnect(elementName) {
1490    console.info('onDisconnect...')
1491  },
1492  onFailed(code) {
1493    console.info('onFailed...')
1494  }
1495
1496  let connection = null;
1497  try {
1498    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1499  } catch (paramError) {
1500    // 处理入参错误异常
1501    console.log('error.code: ' + JSON.stringify(paramError.code) +
1502      ' error.message: ' + JSON.stringify(paramError.message));
1503  }
1504  ```
1505
1506## UIAbilityContext.disconnectServiceExtensionAbility
1507
1508disconnectServiceExtensionAbility(connection: number): Promise\<void>;
1509
1510断开连接(promise形式)。
1511
1512**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1513
1514**参数:**
1515
1516| 参数名 | 类型 | 必填 | 说明 |
1517| -------- | -------- | -------- | -------- |
1518| connection | number | 是 | 连接的Ability的数字代码。 |
1519
1520**返回值:**
1521
1522| 类型 | 说明 |
1523| -------- | -------- |
1524| Promise\<void> | 返回执行结果。 |
1525
1526**错误码:**
1527
1528| 错误码ID | 错误信息 |
1529| ------- | -------------------------------- |
1530| 16000011 | The context does not exist. |
1531| 16000050 | Internal error. |
1532
1533错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1534
1535**示例:**
1536
1537  ```ts
1538// connection为connectServiceExtensionAbility中的返回值
1539let connection = 1;
1540
1541  try {
1542    this.context.disconnectServiceExtensionAbility(connection)
1543      .then((data) => {
1544        // 执行正常业务
1545        console.log('disconnectServiceExtensionAbility succeed');
1546      })
1547      .catch((error) => {
1548        // 处理业务逻辑错误
1549        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1550          ' error.message: ' + JSON.stringify(error.message));
1551      });
1552  } catch (paramError) {
1553    // 处理入参错误异常
1554    console.log('error.code: ' + JSON.stringify(paramError.code) +
1555      ' error.message: ' + JSON.stringify(paramError.message));
1556  }
1557  ```
1558
1559## UIAbilityContext.disconnectServiceExtensionAbility
1560
1561disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void;
1562
1563断开连接(callback形式)。
1564
1565**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1566
1567**参数:**
1568
1569| 参数名 | 类型 | 必填 | 说明 |
1570| -------- | -------- | -------- | -------- |
1571| connection | number | 是 | 连接的Ability的数字代码。 |
1572| callback | AsyncCallback\<void> | 是 | 表示指定的回调方法。 |
1573
1574**错误码:**
1575
1576| 错误码ID | 错误信息 |
1577| ------- | -------------------------------- |
1578| 16000011 | The context does not exist. |
1579| 16000050 | Internal error. |
1580
1581错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1582
1583**示例:**
1584
1585  ```ts
1586// connection为connectServiceExtensionAbility中的返回值
1587let connection = 1;
1588let commRemote;
1589
1590try {
1591  this.context.disconnectServiceExtensionAbility(connection, (err) => {
1592    commRemote = null;
1593    if (err.code) {
1594      // 处理业务逻辑错误
1595      console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
1596      return;
1597    }
1598    // 执行正常业务
1599    console.info('disconnectServiceExtensionAbility succeed');
1600  });
1601} catch (err) {
1602  commRemote = null;
1603  // 处理入参错误异常
1604  console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`);
1605}
1606
1607  try {
1608    this.context.disconnectServiceExtensionAbility(connection, (error) => {
1609      if (error.code) {
1610        // 处理业务逻辑错误
1611        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1612          ' error.message: ' + JSON.stringify(error.message));
1613        return;
1614      }
1615      // 执行正常业务
1616      console.log('disconnectServiceExtensionAbility succeed');
1617    });
1618  } catch (paramError) {
1619    // 处理入参错误异常
1620    console.log('error.code: ' + JSON.stringify(paramError.code) +
1621      ' error.message: ' + JSON.stringify(paramError.message));
1622  }
1623  ```
1624
1625## UIAbilityContext.startAbilityByCall
1626
1627startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1628
1629启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。
1630
1631使用规则:
1632 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1633 - 目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
1634 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
1635
1636**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1637
1638**系统API**: 此接口为系统接口,三方应用不支持调用。
1639
1640**参数:**
1641
1642| 参数名 | 类型 | 必填 | 说明 |
1643| -------- | -------- | -------- | -------- |
1644| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
1645
1646**返回值:**
1647
1648| 类型 | 说明 |
1649| -------- | -------- |
1650| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |
1651
1652**错误码:**
1653
1654| 错误码ID | 错误信息 |
1655| ------- | -------------------------------- |
1656| 16000001 | The specified ability does not exist. |
1657| 16000002 | Incorrect ability type. |
1658| 16000004 | Can not start invisible component. |
1659| 16000005 | The specified process does not have the permission. |
1660| 16000006 | Cross-user operations are not allowed. |
1661| 16000008 | The crowdtesting application expires. |
1662| 16000011 | The context does not exist. |
1663| 16000050 | Internal error. |
1664| 16200001 | The caller has been released. |
1665
1666错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1667
1668**示例:**
1669
1670  后台启动:
1671
1672  ```ts
1673  let caller = undefined;
1674
1675  // 后台启动Ability,不配置parameters
1676  let wantBackground = {
1677      bundleName: 'com.example.myservice',
1678      moduleName: 'entry',
1679      abilityName: 'MainAbility',
1680      deviceId: ''
1681  };
1682
1683  try {
1684    this.context.startAbilityByCall(wantBackground)
1685      .then((obj) => {
1686        // 执行正常业务
1687        caller = obj;
1688        console.log('startAbilityByCall succeed');
1689      }).catch((error) => {
1690        // 处理业务逻辑错误
1691        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1692          ' error.message: ' + JSON.stringify(error.message));
1693      });
1694  } catch (paramError) {
1695    // 处理入参错误异常
1696    console.log('error.code: ' + JSON.stringify(paramError.code) +
1697      ' error.message: ' + JSON.stringify(paramError.message));
1698  }
1699  ```
1700
1701  前台启动:
1702
1703  ```ts
1704  let caller = undefined;
1705
1706  // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
1707  let wantForeground = {
1708      bundleName: 'com.example.myservice',
1709      moduleName: 'entry',
1710      abilityName: 'MainAbility',
1711      deviceId: '',
1712      parameters: {
1713        'ohos.aafwk.param.callAbilityToForeground': true
1714      }
1715  };
1716
1717  try {
1718    this.context.startAbilityByCall(wantForeground)
1719      .then((obj) => {
1720        // 执行正常业务
1721        caller = obj;
1722        console.log('startAbilityByCall succeed');
1723      }).catch((error) => {
1724        // 处理业务逻辑错误
1725        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1726          ' error.message: ' + JSON.stringify(error.message));
1727      });
1728  } catch (paramError) {
1729    // 处理入参错误异常
1730    console.log('error.code: ' + JSON.stringify(paramError.code) +
1731      ' error.message: ' + JSON.stringify(paramError.message));
1732  }
1733  ```
1734
1735## UIAbilityContext.startAbilityWithAccount
1736
1737startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void;
1738
1739根据account启动Ability(callback形式)。
1740
1741**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1742
1743**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1744
1745**系统API**: 此接口为系统接口,三方应用不支持调用。
1746
1747**参数:**
1748
1749| 参数名 | 类型 | 必填 | 说明 |
1750| -------- | -------- | -------- | -------- |
1751| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1752| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1753| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
1754
1755**错误码:**
1756
1757| 错误码ID | 错误信息 |
1758| ------- | -------------------------------- |
1759| 16000001 | The specified ability does not exist. |
1760| 16000002 | Incorrect ability type. |
1761| 16000004 | Can not start invisible component. |
1762| 16000005 | The specified process does not have the permission. |
1763| 16000006 | Cross-user operations are not allowed. |
1764| 16000008 | The crowdtesting application expires. |
1765| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1766| 16000010 | The call with the continuation flag is forbidden. |
1767| 16000011 | The context does not exist. |
1768| 16000050 | Internal error. |
1769| 16000053 | The ability is not on the top of the UI. |
1770| 16000055 | Installation-free timed out. |
1771| 16200001 | The caller has been released. |
1772
1773错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1774
1775**示例:**
1776
1777  ```ts
1778  let want = {
1779    deviceId: '',
1780    bundleName: 'com.extreme.test',
1781    abilityName: 'MainAbility'
1782  };
1783  let accountId = 100;
1784
1785  try {
1786    this.context.startAbilityWithAccount(want, accountId, (error) => {
1787      if (error.code) {
1788        // 处理业务逻辑错误
1789        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1790          ' error.message: ' + JSON.stringify(error.message));
1791        return;
1792      }
1793      // 执行正常业务
1794      console.log('startAbilityWithAccount succeed');
1795    });
1796  } catch (paramError) {
1797    // 处理入参错误异常
1798    console.log('error.code: ' + JSON.stringify(paramError.code) +
1799      ' error.message: ' + JSON.stringify(paramError.message));
1800  }
1801  ```
1802
1803
1804## UIAbilityContext.startAbilityWithAccount
1805
1806startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
1807
1808根据account启动Ability(callback形式)。
1809
1810**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1811
1812**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1813
1814**系统API**: 此接口为系统接口,三方应用不支持调用。
1815
1816**参数:**
1817
1818| 参数名 | 类型 | 必填 | 说明 |
1819| -------- | -------- | -------- | -------- |
1820| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1821| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。|
1822| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
1823| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
1824
1825**错误码:**
1826
1827| 错误码ID | 错误信息 |
1828| ------- | -------------------------------- |
1829| 16000001 | The specified ability does not exist. |
1830| 16000002 | Incorrect ability type. |
1831| 16000004 | Can not start invisible component. |
1832| 16000005 | The specified process does not have the permission. |
1833| 16000006 | Cross-user operations are not allowed. |
1834| 16000008 | The crowdtesting application expires. |
1835| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1836| 16000010 | The call with the continuation flag is forbidden. |
1837| 16000011 | The context does not exist. |
1838| 16000050 | Internal error. |
1839| 16000053 | The ability is not on the top of the UI. |
1840| 16000055 | Installation-free timed out. |
1841| 16200001 | The caller has been released. |
1842
1843错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1844
1845**示例:**
1846
1847  ```ts
1848  let want = {
1849    deviceId: '',
1850    bundleName: 'com.extreme.test',
1851    abilityName: 'MainAbility'
1852  };
1853  let accountId = 100;
1854  let options = {
1855    windowMode: 0
1856  };
1857
1858  try {
1859    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
1860      if (error.code) {
1861        // 处理业务逻辑错误
1862        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1863          ' error.message: ' + JSON.stringify(error.message));
1864        return;
1865      }
1866      // 执行正常业务
1867      console.log('startAbilityWithAccount succeed');
1868    });
1869  } catch (paramError) {
1870    // 处理入参错误异常
1871    console.log('error.code: ' + JSON.stringify(paramError.code) +
1872      ' error.message: ' + JSON.stringify(paramError.message));
1873  }
1874  ```
1875
1876
1877## UIAbilityContext.startAbilityWithAccount
1878
1879startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\>;
1880
1881根据account启动Ability(Promise形式)。
1882
1883**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1884
1885**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1886
1887**系统API**: 此接口为系统接口,三方应用不支持调用。
1888
1889**参数:**
1890
1891| 参数名 | 类型 | 必填 | 说明 |
1892| -------- | -------- | -------- | -------- |
1893| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1894| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1895| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
1896
1897**错误码:**
1898
1899| 错误码ID | 错误信息 |
1900| ------- | -------------------------------- |
1901| 16000001 | The specified ability does not exist. |
1902| 16000002 | Incorrect ability type. |
1903| 16000004 | Can not start invisible component. |
1904| 16000005 | The specified process does not have the permission. |
1905| 16000006 | Cross-user operations are not allowed. |
1906| 16000008 | The crowdtesting application expires. |
1907| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1908| 16000010 | The call with the continuation flag is forbidden. |
1909| 16000011 | The context does not exist. |
1910| 16000050 | Internal error. |
1911| 16000053 | The ability is not on the top of the UI. |
1912| 16000055 | Installation-free timed out. |
1913| 16200001 | The caller has been released. |
1914
1915错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
1916
1917**示例:**
1918
1919  ```ts
1920  let want = {
1921    deviceId: '',
1922    bundleName: 'com.extreme.test',
1923    abilityName: 'MainAbility'
1924  };
1925  let accountId = 100;
1926  let options = {
1927    windowMode: 0
1928  };
1929
1930  try {
1931    this.context.startAbilityWithAccount(want, accountId, options)
1932      .then((data) => {
1933        // 执行正常业务
1934        console.log('startAbilityWithAccount succeed');
1935      })
1936      .catch((error) => {
1937        // 处理业务逻辑错误
1938        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
1939          ' error.message: ' + JSON.stringify(error.message));
1940      });
1941  } catch (paramError) {
1942    // 处理入参错误异常
1943    console.log('error.code: ' + JSON.stringify(paramError.code) +
1944      ' error.message: ' + JSON.stringify(paramError.message));
1945  }
1946  ```
1947
1948## UIAbilityContext.requestPermissionsFromUser
1949
1950requestPermissionsFromUser(permissions: Array&lt;string&gt;, requestCallback: AsyncCallback&lt;PermissionRequestResult&gt;) : void;
1951
1952拉起弹窗请求用户授权(callback形式)。
1953
1954**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1955
1956**参数:**
1957
1958| 参数名 | 类型 | 必填 | 说明 |
1959| -------- | -------- | -------- | -------- |
1960| permissions | Array&lt;string&gt; | 是 | 权限列表。 |
1961| callback | AsyncCallback&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1962
1963**示例:**
1964
1965  ```ts
1966       let permissions=['com.example.permission']
1967       this.context.requestPermissionsFromUser(permissions,(result) => {
1968       console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
1969  });
1970
1971  ```
1972
1973
1974## UIAbilityContext.requestPermissionsFromUser
1975
1976requestPermissionsFromUser(permissions: Array&lt;string&gt;) : Promise&lt;PermissionRequestResult&gt;;
1977
1978拉起弹窗请求用户授权(promise形式)。
1979
1980**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1981
1982**参数:**
1983
1984| 参数名 | 类型 | 必填 | 说明 |
1985| -------- | -------- | -------- | -------- |
1986| permissions | Array&lt;string&gt; | 是 | 权限列表。 |
1987
1988**返回值:**
1989
1990| 类型 | 说明 |
1991| -------- | -------- |
1992| Promise&lt;[PermissionRequestResult](js-apis-permissionrequestresult.md)&gt; | 返回一个Promise,包含接口的结果。 |
1993
1994**示例:**
1995
1996  ```ts
1997   let permissions=['com.example.permission']
1998       this.context.requestPermissionsFromUser(permissions).then((data) => {
1999      console.log('success:' + JSON.stringify(data));
2000  }).catch((error) => {
2001      console.log('failed:' + JSON.stringify(error));
2002  });
2003
2004  ```
2005
2006
2007## UIAbilityContext.setMissionLabel
2008
2009setMissionLabel(label: string, callback:AsyncCallback&lt;void&gt;): void;
2010
2011设置ability在任务中显示的名称(callback形式)。
2012
2013**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2014
2015**参数:**
2016
2017| 参数名 | 类型 | 必填 | 说明 |
2018| -------- | -------- | -------- | -------- |
2019| label | string | 是 | 显示名称。 |
2020| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
2021
2022**错误码:**
2023
2024| 错误码ID | 错误信息 |
2025| ------- | -------------------------------- |
2026| 16000011 | The context does not exist. |
2027| 16000050 | Internal error. |
2028
2029错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2030
2031**示例:**
2032
2033  ```ts
2034  this.context.setMissionLabel('test',(result) => {
2035      console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
2036  });
2037  ```
2038
2039
2040## UIAbilityContext.setMissionLabel
2041
2042setMissionLabel(label: string): Promise&lt;void&gt;;
2043
2044设置ability在任务中显示的名称(promise形式)。
2045
2046**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2047
2048**参数:**
2049
2050| 参数名 | 类型 | 必填 | 说明 |
2051| -------- | -------- | -------- | -------- |
2052| label | string | 是 | 显示名称。 |
2053
2054**返回值:**
2055
2056| 类型 | 说明 |
2057| -------- | -------- |
2058| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
2059
2060**错误码:**
2061
2062| 错误码ID | 错误信息 |
2063| ------- | -------------------------------- |
2064| 16000011 | The context does not exist. |
2065| 16000050 | Internal error. |
2066
2067错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2068
2069**示例:**
2070
2071  ```ts
2072  this.context.setMissionLabel('test').then(() => {
2073      console.log('success');
2074  }).catch((error) => {
2075      console.log('failed:' + JSON.stringify(error));
2076  });
2077  ```
2078## UIAbilityContext.setMissionIcon
2079
2080setMissionIcon(icon: image.PixelMap, callback:AsyncCallback\<void>): void;
2081
2082设置当前ability在任务中显示的图标(callback形式)。
2083
2084**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2085
2086**系统API**: 此接口为系统接口,三方应用不支持调用。
2087
2088**参数:**
2089
2090| 参数名 | 类型 | 必填 | 说明 |
2091| -------- | -------- | -------- | -------- |
2092| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |
2093| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |
2094
2095**错误码:**
2096
2097| 错误码ID | 错误信息 |
2098| ------- | -------------------------------- |
2099| 16000011 | The context does not exist. |
2100| 16000050 | Internal error. |
2101
2102错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2103
2104**示例:**
2105
2106  ```ts
2107    import image from '@ohos.multimedia.image';
2108    let imagePixelMap;
2109    let color = new ArrayBuffer(0);
2110    let initializationOptions = {
2111       size: {
2112           height: 100,
2113           width: 100
2114       }
2115    };
2116    image.createPixelMap(color, initializationOptions)
2117       .then((data) => {
2118           imagePixelMap = data;
2119       })
2120       .catch((err) => {
2121           console.log('--------- createPixelMap fail, err: ---------', err)
2122       });
2123    this.context.setMissionIcon(imagePixelMap, (err) => {
2124       console.log('---------- setMissionIcon fail, err: -----------', err);
2125    })
2126  ```
2127
2128
2129## UIAbilityContext.setMissionIcon
2130
2131setMissionIcon(icon: image.PixelMap): Promise\<void>;
2132
2133设置当前ability在任务中显示的图标(promise形式)。
2134
2135**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2136
2137**系统API**: 此接口为系统接口,三方应用不支持调用。
2138
2139**参数:**
2140
2141| 参数名 | 类型 | 必填 | 说明 |
2142| -------- | -------- | -------- | -------- |
2143| icon | image.PixelMap | 是 | 在最近的任务中显示的ability图标。 |
2144
2145**返回值:**
2146
2147| 类型 | 说明 |
2148| -------- | -------- |
2149| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
2150
2151**错误码:**
2152
2153| 错误码ID | 错误信息 |
2154| ------- | -------------------------------- |
2155| 16000011 | The context does not exist. |
2156| 16000050 | Internal error. |
2157
2158错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2159
2160**示例:**
2161
2162  ```ts
2163    import image from '@ohos.multimedia.image';
2164    let imagePixelMap;
2165    let color = new ArrayBuffer(0);
2166    let initializationOptions = {
2167      size: {
2168          height: 100,
2169          width: 100
2170      }
2171    };
2172    image.createPixelMap(color, initializationOptions)
2173      .then((data) => {
2174          imagePixelMap = data;
2175      })
2176      .catch((err) => {
2177          console.log('--------- createPixelMap fail, err: ---------', err)
2178      });
2179    this.context.setMissionIcon(imagePixelMap)
2180      .then(() => {
2181          console.log('-------------- setMissionIcon success -------------');
2182      })
2183      .catch((err) => {
2184          console.log('-------------- setMissionIcon fail, err: -------------', err);
2185      });
2186  ```
2187## UIAbilityContext.restoreWindowStage
2188
2189restoreWindowStage(localStorage: LocalStorage) : void;
2190
2191恢复ability中的window stage数据。
2192
2193**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2194
2195**参数:**
2196
2197| 参数名 | 类型 | 必填 | 说明 |
2198| -------- | -------- | -------- | -------- |
2199| localStorage | image.LocalStorage | 是 | 用于恢复window stage的存储数据。 |
2200
2201**错误码:**
2202
2203| 错误码ID | 错误信息 |
2204| ------- | -------------------------------- |
2205| 16000011 | The context does not exist. |
2206| 16000050 | Internal error. |
2207
2208错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2209
2210**示例:**
2211
2212  ```ts
2213    let storage = new LocalStorage();
2214    this.context.restoreWindowStage(storage);
2215  ```
2216
2217## UIAbilityContext.isTerminating
2218
2219isTerminating(): boolean;
2220
2221查询ability是否在terminating状态。
2222
2223**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2224
2225**返回值:**
2226
2227| 类型 | 说明 |
2228| -------- | -------- |
2229| bool | true:ability当前处于terminating状态;false:不处于terminating状态。 |
2230
2231**错误码:**
2232
2233| 错误码ID | 错误信息 |
2234| ------- | -------------------------------- |
2235| 16000011 | The context does not exist. |
2236| 16000050 | Internal error. |
2237
2238错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)
2239
2240**示例:**
2241
2242  ```ts
2243  let isTerminating = this.context.isTerminating();
2244  console.log('ability state :' + isTerminating);
2245  ```