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