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