1# ServiceExtensionContext (系统接口)
2
3ServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。
4
5ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力,包括启动、停止、绑定、解绑Ability。
6
7> **说明:**
8> 
9>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>  - 本模块接口仅可在Stage模型下使用。
11>  - 本模块接口需要在主线程中使用,不要在Worker、TaskPool等子线程中使用。
12>  - 本模块接口为系统接口。
13
14## 导入模块
15
16```ts
17import { common } from '@kit.AbilityKit';
18```
19
20## 使用说明
21
22在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。
23
24**示例:**
25
26```ts
27import { ServiceExtensionAbility } from '@kit.AbilityKit';
28import { rpc } from '@kit.IPCKit';
29
30let commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放
31
32class EntryAbility extends ServiceExtensionAbility {
33  onCreate() {
34    let context = this.context; // 获取ServiceExtensionContext
35  }
36}
37```
38
39## ServiceExtensionContext.startAbility
40
41startAbility(want: Want, callback: AsyncCallback<void>): void;
42
43启动Ability。
44
45**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
46
47**系统API**: 此接口为系统接口,三方应用不支持调用。
48
49**参数:**
50
51| 参数名 | 类型 | 必填 | 说明 |
52| -------- | -------- | -------- | -------- |
53| want | [Want](js-apis-app-ability-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
54| callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 |
55
56**错误码:**
57
58以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
59
60| 错误码ID | 错误信息 |
61| ------- | -------- |
62| 201 | The application does not have permission to call the interface. |
63| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
64| 16000001 | The specified ability does not exist. |
65| 16000002 | Incorrect ability type. |
66| 16000004 | Failed to start the invisible ability. |
67| 16000005 | The specified process does not have the permission. |
68| 16000006 | Cross-user operations are not allowed. |
69| 16000008 | The crowdtesting application expires. |
70| 16000009 | An ability cannot be started or stopped in Wukong mode. |
71| 16000010 | The call with the continuation flag is forbidden.        |
72| 16000011 | The context does not exist.        |
73| 16000012 | The application is controlled.        |
74| 16000013 | The application is controlled by EDM.       |
75| 16000019 | No matching ability is found. |
76| 16000050 | Internal error. |
77| 16000053 | The ability is not on the top of the UI. |
78| 16000055 | Installation-free timed out. |
79| 16000071 | App clone is not supported. |
80| 16000072 | App clone or multi-instance is not supported. |
81| 16000073 | The app clone index is invalid. |
82| 16000076 | The app instance key is invalid. |
83| 16000077 | The number of app instances reaches the limit. |
84| 16000078 | The multi-instance is not supported. |
85| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
86| 16000080 | Creating an instance is not supported. |
87| 16200001 | The caller has been released. |
88
89**示例:**
90
91```ts
92import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
93import { BusinessError } from '@kit.BasicServicesKit';
94
95class EntryAbility extends ServiceExtensionAbility {
96  onCreate() {
97    let want: Want = {
98      bundleName: 'com.example.myapp',
99      abilityName: 'MyAbility'
100    };
101
102    try {
103      this.context.startAbility(want, (error: BusinessError) => {
104        if (error.code) {
105          // 处理业务逻辑错误
106          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
107          return;
108        }
109        // 执行正常业务
110        console.log('startAbility succeed');
111      });
112    } catch (paramError) {
113      // 处理入参错误异常
114      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
115    }
116  }
117}
118```
119
120## ServiceExtensionContext.startAbility
121
122startAbility(want: Want, options?: StartOptions): Promise\<void>;
123
124启动Ability,结果以Promise的形式返回。
125
126**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
127
128**系统API**: 此接口为系统接口,三方应用不支持调用。
129
130**参数:**
131
132| 参数名 | 类型 | 必填 | 说明 |
133| -------- | -------- | -------- | -------- |
134| want | [Want](js-apis-app-ability-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
135| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
136
137**返回值:**
138
139| 类型 | 说明 |
140| -------- | -------- |
141| Promise&lt;void&gt; | 返回一个Promise,包含启动的结果。 |
142
143**错误码:**
144
145以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
146
147| 错误码ID | 错误信息 |
148| ------- | -------- |
149| 201 | The application does not have permission to call the interface. |
150| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
151| 16000001 | The specified ability does not exist. |
152| 16000002 | Incorrect ability type. |
153| 16000004 | Failed to start the invisible ability. |
154| 16000005 | The specified process does not have the permission. |
155| 16000006 | Cross-user operations are not allowed. |
156| 16000008 | The crowdtesting application expires. |
157| 16000009 | An ability cannot be started or stopped in Wukong mode. |
158| 16000010 | The call with the continuation flag is forbidden.        |
159| 16000011 | The context does not exist.        |
160| 16000012 | The application is controlled.        |
161| 16000013 | The application is controlled by EDM.       |
162| 16000019 | No matching ability is found. |
163| 16000050 | Internal error. |
164| 16000053 | The ability is not on the top of the UI. |
165| 16000055 | Installation-free timed out. |
166| 16000071 | App clone is not supported. |
167| 16000072 | App clone or multi-instance is not supported. |
168| 16000073 | The app clone index is invalid. |
169| 16000076 | The app instance key is invalid. |
170| 16000077 | The number of app instances reaches the limit. |
171| 16000078 | The multi-instance is not supported. |
172| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
173| 16000080 | Creating an instance is not supported. |
174| 16200001 | The caller has been released. |
175
176**示例:**
177
178```ts
179import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
180import { BusinessError } from '@kit.BasicServicesKit';
181
182class EntryAbility extends ServiceExtensionAbility {
183  onCreate() {
184    let want: Want = {
185      bundleName: 'com.example.myapp',
186      abilityName: 'MyAbility'
187    };
188    let options: StartOptions = {
189      windowMode: 0,
190    };
191
192    try {
193      this.context.startAbility(want, options)
194        .then((data: void) => {
195          // 执行正常业务
196          console.log('startAbility succeed');
197        })
198        .catch((error: BusinessError) => {
199          // 处理业务逻辑错误
200          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
201        });
202    } catch (paramError) {
203      // 处理入参错误异常
204      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
205    }
206  }
207}
208```
209
210## ServiceExtensionContext.startAbility
211
212startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
213
214启动Ability,结果以Callback的形式返回。
215
216**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
217
218**系统API**: 此接口为系统接口,三方应用不支持调用。
219
220**参数:**
221
222| 参数名 | 类型 | 必填 | 说明 |
223| -------- | -------- | -------- | -------- |
224| want | [Want](js-apis-app-ability-want.md)  | 是 | 启动Ability的want信息。 |
225| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
226| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回启动结果。 |
227
228**错误码:**
229
230以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
231
232| 错误码ID | 错误信息 |
233| ------- | -------- |
234| 201 | The application does not have permission to call the interface. |
235| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
236| 16000001 | The specified ability does not exist. |
237| 16000002 | Incorrect ability type. |
238| 16000004 | Failed to start the invisible ability. |
239| 16000005 | The specified process does not have the permission. |
240| 16000006 | Cross-user operations are not allowed. |
241| 16000008 | The crowdtesting application expires. |
242| 16000009 | An ability cannot be started or stopped in Wukong mode. |
243| 16000010 | The call with the continuation flag is forbidden.        |
244| 16000011 | The context does not exist.        |
245| 16000012 | The application is controlled.        |
246| 16000013 | The application is controlled by EDM.       |
247| 16000019 | No matching ability is found. |
248| 16000050 | Internal error. |
249| 16000053 | The ability is not on the top of the UI. |
250| 16000055 | Installation-free timed out. |
251| 16000071 | App clone is not supported. |
252| 16000072 | App clone or multi-instance is not supported. |
253| 16000073 | The app clone index is invalid. |
254| 16000076 | The app instance key is invalid. |
255| 16000077 | The number of app instances reaches the limit. |
256| 16000078 | The multi-instance is not supported. |
257| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
258| 16000080 | Creating an instance is not supported. |
259| 16200001 | The caller has been released. |
260
261**示例:**
262
263```ts
264import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
265import { BusinessError } from '@kit.BasicServicesKit';
266
267class EntryAbility extends ServiceExtensionAbility {
268  onCreate() {
269    let want: Want = {
270      deviceId: '',
271      bundleName: 'com.example.myapplication',
272      abilityName: 'EntryAbility'
273    };
274    let options: StartOptions = {
275      windowMode: 0
276    };
277
278    try {
279      this.context.startAbility(want, options, (error: BusinessError) => {
280        if (error.code) {
281          // 处理业务逻辑错误
282          console.error(`startAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
283          return;
284        }
285        // 执行正常业务
286        console.log('startAbility succeed');
287      });
288    } catch (paramError) {
289      // 处理入参错误异常
290      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
291    }
292  }
293}
294```
295
296## ServiceExtensionContext.startAbilityWithAccount
297
298startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
299
300根据account启动Ability(callback形式)。
301
302> **说明:**
303>
304> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
305
306**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
307
308**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
309
310**系统API**: 此接口为系统接口,三方应用不支持调用。
311
312**参数:**
313
314| 参数名 | 类型 | 必填 | 说明 |
315| -------- | -------- | -------- | -------- |
316| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
317| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 |
318| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
319
320**错误码:**
321
322以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
323
324| 错误码ID | 错误信息 |
325| ------- | -------- |
326| 201 | The application does not have permission to call the interface. |
327| 202 | The application is not system-app, can not use system-api. |
328| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
329| 16000001 | The specified ability does not exist. |
330| 16000002 | Incorrect ability type. |
331| 16000004 | Failed to start the invisible ability. |
332| 16000005 | The specified process does not have the permission. |
333| 16000006 | Cross-user operations are not allowed. |
334| 16000008 | The crowdtesting application expires. |
335| 16000009 | An ability cannot be started or stopped in Wukong mode. |
336| 16000010 | The call with the continuation flag is forbidden.        |
337| 16000011 | The context does not exist.        |
338| 16000012 | The application is controlled.        |
339| 16000013 | The application is controlled by EDM.       |
340| 16000019 | No matching ability is found. |
341| 16000050 | Internal error. |
342| 16000053 | The ability is not on the top of the UI. |
343| 16000055 | Installation-free timed out. |
344| 16000071 | App clone is not supported. |
345| 16000072 | App clone or multi-instance is not supported. |
346| 16000073 | The app clone index is invalid. |
347| 16000076 | The app instance key is invalid. |
348| 16000077 | The number of app instances reaches the limit. |
349| 16000078 | The multi-instance is not supported. |
350| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
351| 16000080 | Creating an instance is not supported. |
352| 16200001 | The caller has been released. |
353
354**示例:**
355
356```ts
357import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
358import { BusinessError } from '@kit.BasicServicesKit';
359
360class EntryAbility extends ServiceExtensionAbility {
361  onCreate() {
362    let want: Want = {
363      deviceId: '',
364      bundleName: 'com.example.myapplication',
365      abilityName: 'EntryAbility'
366    };
367    let accountId = 100;
368
369    try {
370      this.context.startAbilityWithAccount(want, accountId, (error: BusinessError) => {
371        if (error.code) {
372          // 处理业务逻辑错误
373          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
374          return;
375        }
376        // 执行正常业务
377        console.log('startAbilityWithAccount succeed');
378      });
379    } catch (paramError) {
380      // 处理入参错误异常
381      console.error(`error.code: ${paramError.code}, error.message: ${paramError.message}`);
382    }
383  }
384}
385```
386
387## ServiceExtensionContext.startAbilityWithAccount
388
389startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
390
391根据account启动Ability(callback形式)。
392
393> **说明:**
394>
395> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
396
397**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
398
399**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
400
401**系统API**: 此接口为系统接口,三方应用不支持调用。
402
403**参数:**
404
405| 参数名 | 类型 | 必填 | 说明 |
406| -------- | -------- | -------- | -------- |
407| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
408| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 |
409| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
410| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
411
412**错误码:**
413
414以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
415
416| 错误码ID | 错误信息 |
417| ------- | -------- |
418| 201 | The application does not have permission to call the interface. |
419| 202 | The application is not system-app, can not use system-api. |
420| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
421| 16000001 | The specified ability does not exist. |
422| 16000002 | Incorrect ability type. |
423| 16000004 | Failed to start the invisible ability. |
424| 16000005 | The specified process does not have the permission. |
425| 16000006 | Cross-user operations are not allowed. |
426| 16000008 | The crowdtesting application expires. |
427| 16000009 | An ability cannot be started or stopped in Wukong mode. |
428| 16000010 | The call with the continuation flag is forbidden.        |
429| 16000011 | The context does not exist.        |
430| 16000012 | The application is controlled.        |
431| 16000013 | The application is controlled by EDM.       |
432| 16000019 | No matching ability is found. |
433| 16000050 | Internal error. |
434| 16000053 | The ability is not on the top of the UI. |
435| 16000055 | Installation-free timed out. |
436| 16000071 | App clone is not supported. |
437| 16000072 | App clone or multi-instance is not supported. |
438| 16000073 | The app clone index is invalid. |
439| 16000076 | The app instance key is invalid. |
440| 16000077 | The number of app instances reaches the limit. |
441| 16000078 | The multi-instance is not supported. |
442| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
443| 16000080 | Creating an instance is not supported. |
444| 16200001 | The caller has been released. |
445
446**示例:**
447
448```ts
449import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
450import { BusinessError } from '@kit.BasicServicesKit';
451
452class EntryAbility extends ServiceExtensionAbility {
453  onCreate() {
454    let want: Want = {
455      deviceId: '',
456      bundleName: 'com.example.myapplication',
457      abilityName: 'EntryAbility'
458    };
459    let accountId = 100;
460    let options: StartOptions = {
461      windowMode: 0
462    };
463
464    try {
465      this.context.startAbilityWithAccount(want, accountId, options, (error: BusinessError) => {
466        if (error.code) {
467          // 处理业务逻辑错误
468          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
469          return;
470        }
471        // 执行正常业务
472        console.log('startAbilityWithAccount succeed');
473      });
474    } catch (paramError) {
475      // 处理入参错误异常
476      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
477    }
478  }
479}
480```
481
482
483## ServiceExtensionContext.startAbilityWithAccount
484
485startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
486
487根据account启动Ability(Promise形式)。
488
489> **说明:**
490>
491> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
492
493**需要权限**:ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
494
495**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
496
497**系统API**: 此接口为系统接口,三方应用不支持调用。
498
499**参数:**
500
501| 参数名 | 类型 | 必填 | 说明 |
502| -------- | -------- | -------- | -------- |
503| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
504| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 |
505| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
506
507**返回值:**
508
509| 类型 | 说明 |
510| -------- | -------- |
511| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
512
513**错误码:**
514
515以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
516
517| 错误码ID | 错误信息 |
518| ------- | -------- |
519| 201 | The application does not have permission to call the interface. |
520| 202 | The application is not system-app, can not use system-api. |
521| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
522| 16000001 | The specified ability does not exist. |
523| 16000002 | Incorrect ability type. |
524| 16000004 | Failed to start the invisible ability. |
525| 16000005 | The specified process does not have the permission. |
526| 16000006 | Cross-user operations are not allowed. |
527| 16000008 | The crowdtesting application expires. |
528| 16000009 | An ability cannot be started or stopped in Wukong mode. |
529| 16000010 | The call with the continuation flag is forbidden.        |
530| 16000011 | The context does not exist.        |
531| 16000012 | The application is controlled.        |
532| 16000013 | The application is controlled by EDM.       |
533| 16000019 | No matching ability is found. |
534| 16000050 | Internal error. |
535| 16000053 | The ability is not on the top of the UI. |
536| 16000055 | Installation-free timed out. |
537| 16000071 | App clone is not supported. |
538| 16000072 | App clone or multi-instance is not supported. |
539| 16000073 | The app clone index is invalid. |
540| 16000076 | The app instance key is invalid. |
541| 16000077 | The number of app instances reaches the limit. |
542| 16000078 | The multi-instance is not supported. |
543| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
544| 16000080 | Creating an instance is not supported. |
545| 16200001 | The caller has been released. |
546
547**示例:**
548
549```ts
550import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
551import { BusinessError } from '@kit.BasicServicesKit';
552
553class EntryAbility extends ServiceExtensionAbility {
554  onCreate() {
555    let want: Want = {
556      deviceId: '',
557      bundleName: 'com.example.myapplication',
558      abilityName: 'EntryAbility'
559    };
560    let accountId = 100;
561    let options: StartOptions = {
562      windowMode: 0
563    };
564
565    try {
566      this.context.startAbilityWithAccount(want, accountId, options)
567        .then((data: void) => {
568          // 执行正常业务
569          console.log('startAbilityWithAccount succeed');
570        })
571        .catch((error: BusinessError) => {
572          // 处理业务逻辑错误
573          console.error(`startAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
574        });
575    } catch (paramError) {
576      // 处理入参错误异常
577      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
578    }
579  }
580}
581```
582
583## ServiceExtensionContext.startServiceExtensionAbility
584
585startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
586
587启动一个新的ServiceExtensionAbility(callback形式)。
588
589**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
590
591**系统API**: 此接口为系统接口,三方应用不支持调用。
592
593**参数:**
594
595| 参数名 | 类型 | 必填 | 说明 |
596| -------- | -------- | -------- | -------- |
597| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
598| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
599
600**错误码:**
601
602以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
603
604| 错误码ID | 错误信息 |
605| ------- | -------- |
606| 201 | The application does not have permission to call the interface. |
607| 202 | The application is not system-app, can not use system-api. |
608| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
609| 16000001 | The specified ability does not exist. |
610| 16000002 | Incorrect ability type. |
611| 16000004 | Failed to start the invisible ability. |
612| 16000005 | The specified process does not have the permission. |
613| 16000006 | Cross-user operations are not allowed. |
614| 16000008 | The crowdtesting application expires. |
615| 16000011 | The context does not exist.        |
616| 16000012 | The application is controlled.        |
617| 16000013 | The application is controlled by EDM.       |
618| 16000019 | No matching ability is found. |
619| 16000050 | Internal error. |
620| 16200001 | The caller has been released. |
621
622**示例:**
623
624```ts
625import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
626import { BusinessError } from '@kit.BasicServicesKit';
627
628class EntryAbility extends ServiceExtensionAbility {
629  onCreate() {
630    let want: Want = {
631      deviceId: '',
632      bundleName: 'com.example.myapplication',
633      abilityName: 'EntryAbility'
634    };
635
636    try {
637      this.context.startServiceExtensionAbility(want, (error: BusinessError) => {
638        if (error.code) {
639          // 处理业务逻辑错误
640          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
641          return;
642        }
643        // 执行正常业务
644        console.log('startServiceExtensionAbility succeed');
645      });
646    } catch (paramError) {
647      // 处理入参错误异常
648      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
649    }
650  }
651}
652```
653
654## ServiceExtensionContext.startServiceExtensionAbility
655
656startServiceExtensionAbility(want: Want): Promise\<void>;
657
658启动一个新的ServiceExtensionAbility(Promise形式)。
659
660**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
661
662**系统API**: 此接口为系统接口,三方应用不支持调用。
663
664**参数:**
665
666| 参数名 | 类型 | 必填 | 说明 |
667| -------- | -------- | -------- | -------- |
668| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
669
670**返回值:**
671
672| 类型 | 说明 |
673| -------- | -------- |
674| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
675
676**错误码:**
677
678以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
679
680| 错误码ID | 错误信息 |
681| ------- | -------- |
682| 201 | The application does not have permission to call the interface. |
683| 202 | The application is not system-app, can not use system-api. |
684| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
685| 16000001 | The specified ability does not exist. |
686| 16000002 | Incorrect ability type. |
687| 16000004 | Failed to start the invisible ability. |
688| 16000005 | The specified process does not have the permission. |
689| 16000006 | Cross-user operations are not allowed. |
690| 16000008 | The crowdtesting application expires. |
691| 16000011 | The context does not exist.        |
692| 16000012 | The application is controlled.        |
693| 16000013 | The application is controlled by EDM.       |
694| 16000019 | No matching ability is found. |
695| 16000050 | Internal error. |
696| 16200001 | The caller has been released. |
697
698**示例:**
699
700```ts
701import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
702import { BusinessError } from '@kit.BasicServicesKit';
703
704class EntryAbility extends ServiceExtensionAbility {
705  onCreate() {
706    let want: Want = {
707      deviceId: '',
708      bundleName: 'com.example.myapplication',
709      abilityName: 'EntryAbility'
710    };
711
712    try {
713      this.context.startServiceExtensionAbility(want)
714        .then((data) => {
715          // 执行正常业务
716          console.log('startServiceExtensionAbility succeed');
717        })
718        .catch((error: BusinessError) => {
719          // 处理业务逻辑错误
720          console.error(`startServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
721        });
722    } catch (paramError) {
723      // 处理入参错误异常
724      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
725    }
726  }
727}
728```
729
730## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
731
732startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
733
734启动一个新的ServiceExtensionAbility(callback形式)。
735
736> **说明:**
737> 
738> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。  
739> 当accountId为当前用户时,无需进行权限校验。
740
741**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
742
743**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
744
745**系统API**: 此接口为系统接口,三方应用不支持调用。
746
747**参数:**
748
749| 参数名 | 类型 | 必填 | 说明 |
750| -------- | -------- | -------- | -------- |
751| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
752| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 |
753| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
754
755**错误码:**
756
757以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
758
759| 错误码ID | 错误信息 |
760| ------- | -------- |
761| 201 | The application does not have permission to call the interface. |
762| 202 | The application is not system-app, can not use system-api. |
763| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
764| 16000001 | The specified ability does not exist. |
765| 16000002 | Incorrect ability type. |
766| 16000004 | Failed to start the invisible ability. |
767| 16000005 | The specified process does not have the permission. |
768| 16000006 | Cross-user operations are not allowed. |
769| 16000008 | The crowdtesting application expires. |
770| 16000011 | The context does not exist.        |
771| 16000012 | The application is controlled.        |
772| 16000013 | The application is controlled by EDM.       |
773| 16000019 | No matching ability is found. |
774| 16000050 | Internal error. |
775| 16200001 | The caller has been released. |
776
777**示例:**
778
779```ts
780import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
781import { BusinessError } from '@kit.BasicServicesKit';
782
783class EntryAbility extends ServiceExtensionAbility {
784  onCreate() {
785    let want: Want = {
786      deviceId: '',
787      bundleName: 'com.example.myapplication',
788      abilityName: 'EntryAbility'
789    };
790    let accountId = 100;
791
792    try {
793      this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
794        if (error.code) {
795          // 处理业务逻辑错误
796          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
797          return;
798        }
799        // 执行正常业务
800        console.log('startServiceExtensionAbilityWithAccount succeed');
801      });
802    } catch (paramError) {
803      // 处理入参错误异常
804      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
805    }
806  }
807}
808```
809
810## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
811
812startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
813
814启动一个新的ServiceExtensionAbility(Promise形式)。
815
816> **说明:**
817> 
818> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。  
819> 当accountId为当前用户时,无需进行权限校验。
820
821**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
822
823**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
824
825**系统API**: 此接口为系统接口,三方应用不支持调用。
826
827**参数:**
828
829| 参数名 | 类型 | 必填 | 说明 |
830| -------- | -------- | -------- | -------- |
831| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
832| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 |
833
834**返回值:**
835
836| 类型 | 说明 |
837| -------- | -------- |
838| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
839
840**错误码:**
841
842以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
843
844| 错误码ID | 错误信息 |
845| ------- | -------- |
846| 201 | The application does not have permission to call the interface. |
847| 202 | The application is not system-app, can not use system-api. |
848| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
849| 16000001 | The specified ability does not exist. |
850| 16000002 | Incorrect ability type. |
851| 16000004 | Failed to start the invisible ability. |
852| 16000005 | The specified process does not have the permission. |
853| 16000006 | Cross-user operations are not allowed. |
854| 16000008 | The crowdtesting application expires. |
855| 16000011 | The context does not exist.        |
856| 16000012 | The application is controlled.        |
857| 16000013 | The application is controlled by EDM.       |
858| 16000019 | No matching ability is found. |
859| 16000050 | Internal error. |
860| 16200001 | The caller has been released. |
861
862**示例:**
863
864```ts
865import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
866import { BusinessError } from '@kit.BasicServicesKit';
867
868class EntryAbility extends ServiceExtensionAbility {
869  onCreate() {
870    let want: Want = {
871      deviceId: '',
872      bundleName: 'com.example.myapplication',
873      abilityName: 'EntryAbility'
874    };
875    let accountId = 100;
876
877    try {
878      this.context.startServiceExtensionAbilityWithAccount(want, accountId)
879        .then((data: void) => {
880          // 执行正常业务
881          console.log('startServiceExtensionAbilityWithAccount succeed');
882        })
883        .catch((error: BusinessError) => {
884          // 处理业务逻辑错误
885          console.error(`startServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
886        });
887    } catch (paramError) {
888      // 处理入参错误异常
889      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
890    }
891  }
892}
893```
894
895## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
896
897startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void;
898
899使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。
900
901> **说明:**
902>
903> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
904
905**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
906
907**系统API**: 此接口为系统接口,三方应用不支持调用。
908
909**参数:**
910
911| 参数名 | 类型 | 必填 | 说明 |
912| -------- | -------- | -------- | -------- |
913| want | [Want](js-apis-app-ability-want.md)  | 是 | 启动Ability的want信息。 |
914| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |
915
916**错误码:**
917
918以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
919
920| 错误码ID | 错误信息 |
921| ------- | -------- |
922| 201 | The application does not have permission to call the interface. |
923| 202 | The application is not system-app, can not use system-api. |
924| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
925| 16000001 | The specified ability does not exist. |
926| 16000002 | Incorrect ability type. |
927| 16000004 | Failed to start the invisible ability. |
928| 16000005 | The specified process does not have the permission. |
929| 16000006 | Cross-user operations are not allowed. |
930| 16000008 | The crowdtesting application expires. |
931| 16000009 | An ability cannot be started or stopped in Wukong mode. |
932| 16000010 | The call with the continuation flag is forbidden.        |
933| 16000011 | The context does not exist.        |
934| 16000012 | The application is controlled.        |
935| 16000013 | The application is controlled by EDM.       |
936| 16000050 | Internal error. |
937| 16000053 | The ability is not on the top of the UI. |
938| 16000055 | Installation-free timed out. |
939| 16000071 | App clone is not supported. |
940| 16000072 | App clone or multi-instance is not supported. |
941| 16000073 | The app clone index is invalid. |
942| 16000076 | The app instance key is invalid. |
943| 16000077 | The number of app instances reaches the limit. |
944| 16000078 | The multi-instance is not supported. |
945| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
946| 16000080 | Creating an instance is not supported. |
947| 16200001 | The caller has been released. |
948
949**示例:**
950
951```ts
952import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
953
954class EntryAbility extends ServiceExtensionAbility {
955  onCreate(want: Want) {
956    // want包含启动该应用的Caller信息
957    let localWant: Want = want;
958    localWant.bundleName = 'com.example.demo';
959    localWant.moduleName = 'entry';
960    localWant.abilityName = 'TestAbility';
961
962    // 使用启动方的Caller身份信息启动新Ability
963    this.context.startAbilityAsCaller(localWant, (err) => {
964      if (err && err.code != 0) {
965        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
966      } else {
967        console.log('startAbilityAsCaller success.');
968      }
969    })
970  }
971}
972```
973
974## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
975
976startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
977
978使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用callback异步回调。
979
980> **说明:**
981>
982> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
983
984**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
985
986**系统API**: 此接口为系统接口,三方应用不支持调用。
987
988**参数:**
989
990| 参数名 | 类型 | 必填 | 说明 |
991| -------- | -------- | -------- | -------- |
992| want | [Want](js-apis-app-ability-want.md)  | 是 | 启动Ability的want信息。 |
993| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
994| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当启动Ability成功,err为undefined,否则为错误对象。 |
995
996**错误码:**
997
998以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
999
1000| 错误码ID | 错误信息 |
1001| ------- | -------- |
1002| 201 | The application does not have permission to call the interface. |
1003| 202 | The application is not system-app, can not use system-api. |
1004| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1005| 16000001 | The specified ability does not exist. |
1006| 16000004 | Failed to start the invisible ability. |
1007| 16000005 | The specified process does not have the permission. |
1008| 16000006 | Cross-user operations are not allowed. |
1009| 16000008 | The crowdtesting application expires. |
1010| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1011| 16000011 | The context does not exist.        |
1012| 16000012 | The application is controlled.        |
1013| 16000013 | The application is controlled by EDM.       |
1014| 16000050 | Internal error. |
1015| 16000053 | The ability is not on the top of the UI. |
1016| 16000055 | Installation-free timed out. |
1017| 16000071 | App clone is not supported. |
1018| 16000072 | App clone or multi-instance is not supported. |
1019| 16000073 | The app clone index is invalid. |
1020| 16000076 | The app instance key is invalid. |
1021| 16000077 | The number of app instances reaches the limit. |
1022| 16000078 | The multi-instance is not supported. |
1023| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
1024| 16000080 | Creating an instance is not supported. |
1025| 16200001 | The caller has been released. |
1026
1027**示例:**
1028
1029```ts
1030import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
1031
1032class EntryAbility extends ServiceExtensionAbility {
1033  onCreate(want: Want) {
1034    // want包含启动该应用的Caller信息
1035    let localWant: Want = want;
1036    localWant.bundleName = 'com.example.demo';
1037    localWant.moduleName = 'entry';
1038    localWant.abilityName = 'TestAbility';
1039
1040    let option: StartOptions = {
1041      displayId: 0
1042    }
1043
1044    // 使用启动方的Caller身份信息启动新Ability
1045    this.context.startAbilityAsCaller(localWant, option, (err) => {
1046      if (err && err.code != 0) {
1047        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
1048      } else {
1049        console.log('startAbilityAsCaller success.');
1050      }
1051    })
1052  }
1053}
1054```
1055
1056## ServiceExtensionContext.startAbilityAsCaller<sup>10+<sup>
1057
1058startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void>;
1059
1060使用设置的caller信息启动一个Ability,caller信息由want携带,在系统服务层识别,Ability可以在onCreate生命周期的want参数中获取到caller信息。使用该接口启动一个Ability时,want的caller信息不会被当前自身的应用信息覆盖,系统服务层可获取到初始caller的信息。使用Promise异步回调。
1061
1062> **说明:**
1063>
1064> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
1065
1066**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1067
1068**系统API**: 此接口为系统接口,三方应用不支持调用。
1069
1070**参数:**
1071
1072| 参数名 | 类型 | 必填 | 说明 |
1073| -------- | -------- | -------- | -------- |
1074| want | [Want](js-apis-app-ability-want.md)  | 是 | 启动Ability的want信息。 |
1075| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
1076
1077**返回值:**
1078
1079| 类型 | 说明 |
1080| -------- | -------- |
1081| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
1082
1083**错误码:**
1084
1085以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1086
1087| 错误码ID | 错误信息 |
1088| ------- | -------- |
1089| 201 | The application does not have permission to call the interface. |
1090| 202 | The application is not system-app, can not use system-api. |
1091| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1092| 16000001 | The specified ability does not exist. |
1093| 16000002 | Incorrect ability type. |
1094| 16000004 | Failed to start the invisible ability. |
1095| 16000005 | The specified process does not have the permission. |
1096| 16000006 | Cross-user operations are not allowed. |
1097| 16000008 | The crowdtesting application expires. |
1098| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1099| 16000010 | The call with the continuation flag is forbidden.        |
1100| 16000011 | The context does not exist.        |
1101| 16000012 | The application is controlled.        |
1102| 16000013 | The application is controlled by EDM.       |
1103| 16000050 | Internal error. |
1104| 16000053 | The ability is not on the top of the UI. |
1105| 16000055 | Installation-free timed out. |
1106| 16000071 | App clone is not supported. |
1107| 16000072 | App clone or multi-instance is not supported. |
1108| 16000073 | The app clone index is invalid. |
1109| 16000076 | The app instance key is invalid. |
1110| 16000077 | The number of app instances reaches the limit. |
1111| 16000078 | The multi-instance is not supported. |
1112| 16000079 | The APP_INSTANCE_KEY cannot be specified. |
1113| 16000080 | Creating an instance is not supported. |
1114| 16200001 | The caller has been released. |
1115
1116**示例:**
1117
1118```ts
1119import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
1120import { BusinessError } from '@kit.BasicServicesKit';
1121
1122class EntryAbility extends ServiceExtensionAbility {
1123  onCreate(want: Want) {
1124    // want包含启动该应用的Caller信息
1125    let localWant: Want = want;
1126    localWant.bundleName = 'com.example.demo';
1127    localWant.moduleName = 'entry';
1128    localWant.abilityName = 'TestAbility';
1129
1130    let option: StartOptions = {
1131      displayId: 0
1132    };
1133
1134    // 使用启动方的Caller身份信息启动新Ability
1135    this.context.startAbilityAsCaller(localWant, option)
1136      .then(() => {
1137        console.log('startAbilityAsCaller success.');
1138      })
1139      .catch((err: BusinessError) => {
1140        console.error('startAbilityAsCaller failed, err:' + JSON.stringify(err));
1141      })
1142  }
1143}
1144```
1145
1146## ServiceExtensionContext.stopServiceExtensionAbility
1147
1148stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
1149
1150停止同一应用程序内的服务(callback形式)。
1151
1152**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1153
1154**系统API**: 此接口为系统接口,三方应用不支持调用。
1155
1156**参数:**
1157
1158| 参数名 | 类型 | 必填 | 说明 |
1159| -------- | -------- | -------- | -------- |
1160| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 |
1161| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
1162
1163**错误码:**
1164
1165以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1166
1167| 错误码ID | 错误信息 |
1168| ------- | -------- |
1169| 201 | The application does not have permission to call the interface. |
1170| 202 | The application is not system-app, can not use system-api. |
1171| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1172| 16000001 | The specified ability does not exist. |
1173| 16000002 | Incorrect ability type. |
1174| 16000004 | Failed to start the invisible ability. |
1175| 16000005 | The specified process does not have the permission. |
1176| 16000006 | Cross-user operations are not allowed. |
1177| 16000011 | The context does not exist.        |
1178| 16000050 | Internal error. |
1179| 16200001 | The caller has been released. |
1180
1181**示例:**
1182
1183```ts
1184import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1185import { BusinessError } from '@kit.BasicServicesKit';
1186
1187class EntryAbility extends ServiceExtensionAbility {
1188  onCreate() {
1189    let want: Want = {
1190      deviceId: '',
1191      bundleName: 'com.example.myapplication',
1192      abilityName: 'EntryAbility'
1193    };
1194
1195    try {
1196      this.context.stopServiceExtensionAbility(want, (error: BusinessError) => {
1197        if (error.code) {
1198          // 处理业务逻辑错误
1199          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1200          return;
1201        }
1202        // 执行正常业务
1203        console.log('stopServiceExtensionAbility succeed');
1204      });
1205    } catch (paramError) {
1206      // 处理入参错误异常
1207      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1208    }
1209  }
1210}
1211```
1212
1213## ServiceExtensionContext.stopServiceExtensionAbility
1214
1215stopServiceExtensionAbility(want: Want): Promise\<void>;
1216
1217停止同一应用程序内的服务(Promise形式)。
1218
1219**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1220
1221**系统API**: 此接口为系统接口,三方应用不支持调用。
1222
1223**参数:**
1224
1225| 参数名 | 类型 | 必填 | 说明 |
1226| -------- | -------- | -------- | -------- |
1227| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 |
1228
1229**返回值:**
1230
1231| 类型 | 说明 |
1232| -------- | -------- |
1233| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1234
1235**错误码:**
1236
1237以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1238
1239| 错误码ID | 错误信息 |
1240| ------- | -------- |
1241| 201 | The application does not have permission to call the interface. |
1242| 202 | The application is not system-app, can not use system-api. |
1243| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1244| 16000001 | The specified ability does not exist. |
1245| 16000002 | Incorrect ability type. |
1246| 16000004 | Failed to start the invisible ability. |
1247| 16000005 | The specified process does not have the permission. |
1248| 16000006 | Cross-user operations are not allowed. |
1249| 16000011 | The context does not exist.        |
1250| 16000050 | Internal error. |
1251| 16200001 | The caller has been released. |
1252
1253**示例:**
1254
1255```ts
1256import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1257import { BusinessError } from '@kit.BasicServicesKit';
1258
1259class EntryAbility extends ServiceExtensionAbility {
1260  onCreate() {
1261    let want: Want = {
1262      deviceId: '',
1263      bundleName: 'com.example.myapplication',
1264      abilityName: 'EntryAbility'
1265    };
1266
1267    try {
1268      this.context.stopServiceExtensionAbility(want)
1269        .then(() => {
1270          // 执行正常业务
1271          console.log('stopServiceExtensionAbility succeed');
1272        })
1273        .catch((error: BusinessError) => {
1274          // 处理业务逻辑错误
1275          console.error(`stopServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1276        });
1277    } catch (paramError) {
1278      // 处理入参错误异常
1279      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1280    }
1281  }
1282}
1283```
1284
1285## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1286
1287stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
1288
1289使用帐户停止同一应用程序内的服务(callback形式)。
1290
1291> **说明:**
1292> 
1293> 当accountId为当前用户时,无需进行权限校验。
1294
1295**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1296
1297**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1298
1299**系统API**: 此接口为系统接口,三方应用不支持调用。
1300
1301**参数:**
1302
1303| 参数名 | 类型 | 必填 | 说明 |
1304| -------- | -------- | -------- | -------- |
1305| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 |
1306| accountId | number | 是 | 需要停止的系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 |
1307| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
1308
1309**错误码:**
1310
1311以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1312
1313| 错误码ID | 错误信息 |
1314| ------- | -------- |
1315| 201 | The application does not have permission to call the interface. |
1316| 202 | The application is not system-app, can not use system-api. |
1317| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1318| 16000001 | The specified ability does not exist. |
1319| 16000002 | Incorrect ability type. |
1320| 16000004 | Failed to start the invisible ability. |
1321| 16000005 | The specified process does not have the permission. |
1322| 16000006 | Cross-user operations are not allowed. |
1323| 16000011 | The context does not exist.        |
1324| 16000050 | Internal error. |
1325| 16200001 | The caller has been released. |
1326
1327**示例:**
1328
1329```ts
1330import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1331import { BusinessError } from '@kit.BasicServicesKit';
1332
1333class EntryAbility extends ServiceExtensionAbility {
1334  onCreate() {
1335    let want: Want = {
1336      deviceId: '',
1337      bundleName: 'com.example.myapplication',
1338      abilityName: 'EntryAbility'
1339    };
1340    let accountId = 100;
1341
1342    try {
1343      this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error: BusinessError) => {
1344        if (error.code) {
1345          // 处理业务逻辑错误
1346          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
1347          return;
1348        }
1349        // 执行正常业务
1350        console.log('stopServiceExtensionAbilityWithAccount succeed');
1351      });
1352    } catch (paramError) {
1353      // 处理入参错误异常
1354      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1355    }
1356  }
1357}
1358```
1359
1360## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
1361
1362stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
1363
1364使用帐户停止同一应用程序内的服务(Promise形式)。
1365
1366> **说明:**
1367> 
1368> 当accountId为当前用户时,无需进行权限校验。
1369
1370**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1371
1372**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1373
1374**系统API**: 此接口为系统接口,三方应用不支持调用。
1375
1376**参数:**
1377
1378| 参数名 | 类型 | 必填 | 说明 |
1379| -------- | -------- | -------- | -------- |
1380| want | [Want](js-apis-app-ability-want.md) | 是 | 停止Ability的want信息。 |
1381| accountId | number | 是 | 需要停止的系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 |
1382
1383**返回值:**
1384
1385| 类型 | 说明 |
1386| -------- | -------- |
1387| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1388
1389**错误码:**
1390
1391以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1392
1393| 错误码ID | 错误信息 |
1394| ------- | -------- |
1395| 201 | The application does not have permission to call the interface. |
1396| 202 | The application is not system-app, can not use system-api. |
1397| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1398| 16000001 | The specified ability does not exist. |
1399| 16000002 | Incorrect ability type. |
1400| 16000004 | Failed to start the invisible ability. |
1401| 16000005 | The specified process does not have the permission. |
1402| 16000006 | Cross-user operations are not allowed. |
1403| 16000011 | The context does not exist.        |
1404| 16000050 | Internal error. |
1405| 16200001 | The caller has been released. |
1406
1407**示例:**
1408
1409```ts
1410import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
1411import { BusinessError } from '@kit.BasicServicesKit';
1412
1413class EntryAbility extends ServiceExtensionAbility {
1414  onCreate() {
1415    let want: Want = {
1416      deviceId: '',
1417      bundleName: 'com.example.myapplication',
1418      abilityName: 'EntryAbility'
1419    };
1420    let accountId = 100;
1421
1422    try {
1423      this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
1424        .then(() => {
1425          // 执行正常业务
1426          console.log('stopServiceExtensionAbilityWithAccount succeed');
1427        })
1428        .catch((error: BusinessError) => {
1429          // 处理业务逻辑错误
1430          console.error(`stopServiceExtensionAbilityWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
1431        });
1432    } catch (paramError) {
1433      // 处理入参错误异常
1434      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1435    }
1436  }
1437}
1438```
1439
1440## ServiceExtensionContext.terminateSelf
1441
1442terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1443
1444停止Ability自身。
1445
1446**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1447
1448**系统API**: 此接口为系统接口,三方应用不支持调用。
1449
1450**参数:**
1451
1452| 参数名 | 类型 | 必填 | 说明 |
1453| -------- | -------- | -------- | -------- |
1454| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1455
1456**错误码:**
1457
1458以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1459
1460| 错误码ID | 错误信息 |
1461| ------- | -------- |
1462| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1463| 16000001 | The specified ability does not exist. |
1464| 16000004 | Failed to start the invisible ability. |
1465| 16000005 | The specified process does not have the permission. |
1466| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1467| 16000011 | The context does not exist.        |
1468| 16000050 | Internal error. |
1469
1470**示例:**
1471
1472```ts
1473import { ServiceExtensionAbility } from '@kit.AbilityKit';
1474import { BusinessError } from '@kit.BasicServicesKit';
1475
1476class EntryAbility extends ServiceExtensionAbility {
1477  onCreate() {
1478    this.context.terminateSelf((error: BusinessError) => {
1479      if (error.code) {
1480        // 处理业务逻辑错误
1481        console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
1482        return;
1483      }
1484      // 执行正常业务
1485      console.log('terminateSelf succeed');
1486    });
1487  }
1488}
1489```
1490
1491## ServiceExtensionContext.terminateSelf
1492
1493terminateSelf(): Promise&lt;void&gt;;
1494
1495停止Ability自身。通过Promise返回结果。
1496
1497**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1498
1499**系统API**: 此接口为系统接口,三方应用不支持调用。
1500
1501**返回值:**
1502
1503| 类型 | 说明 |
1504| -------- | -------- |
1505| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1506
1507**错误码:**
1508
1509以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。
1510
1511| 错误码ID | 错误信息 |
1512| ------- | -------------------------------- |
1513| 16000001 | The specified ability does not exist. |
1514| 16000004 | Failed to start the invisible ability. |
1515| 16000005 | The specified process does not have the permission. |
1516| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1517| 16000011 | The context does not exist.        |
1518| 16000050 | Internal error. |
1519
1520**示例:**
1521
1522```ts
1523import { ServiceExtensionAbility } from '@kit.AbilityKit';
1524import { BusinessError } from '@kit.BasicServicesKit';
1525
1526class EntryAbility extends ServiceExtensionAbility {
1527  onCreate() {
1528    this.context.terminateSelf().then(() => {
1529      // 执行正常业务
1530      console.log('terminateSelf succeed');
1531    }).catch((error: BusinessError) => {
1532      // 处理业务逻辑错误
1533      console.error(`terminateSelf failed, error.code: ${error.code}, error.message: ${error.message}`);
1534    });
1535  }
1536}
1537```
1538
1539## ServiceExtensionContext.connectServiceExtensionAbility
1540
1541connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1542
1543将当前Ability连接到一个ServiceExtensionAbility。
1544
1545> **说明:**
1546>
1547> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
1548
1549**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1550
1551**系统API**: 此接口为系统接口,三方应用不支持调用。
1552
1553**参数:**
1554
1555| 参数名 | 类型 | 必填 | 说明 |
1556| -------- | -------- | -------- | -------- |
1557| want | [Want](js-apis-app-ability-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如Ability名称,Bundle名称等。 |
1558| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 |
1559
1560**返回值:**
1561
1562| 类型 | 说明 |
1563| -------- | -------- |
1564| number | 返回一个number,后续根据这个number去断开连接。 |
1565
1566**错误码:**
1567
1568以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1569
1570| 错误码ID | 错误信息 |
1571| ------- | -------- |
1572| 201 | The application does not have permission to call the interface. |
1573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1574| 16000001 | The specified ability does not exist. |
1575| 16000002 | Incorrect ability type. |
1576| 16000004 | Failed to start the invisible ability. |
1577| 16000005 | The specified process does not have the permission. |
1578| 16000006 | Cross-user operations are not allowed. |
1579| 16000008 | The crowdtesting application expires. |
1580| 16000053 | The ability is not on the top of the UI. |
1581| 16000055 | Installation-free timed out. |
1582| 16000011 | The context does not exist.        |
1583| 16000050 | Internal error. |
1584
1585**示例:**
1586
1587```ts
1588import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
1589import { rpc } from '@kit.IPCKit';
1590import { BusinessError } from '@kit.BasicServicesKit';
1591
1592let commRemote: rpc.IRemoteObject; // 断开连接时需要释放
1593
1594class EntryAbility extends ServiceExtensionAbility {
1595  onCreate() {
1596    let want: Want = {
1597      bundleName: 'com.example.myapp',
1598      abilityName: 'MyAbility'
1599    };
1600    let options: common.ConnectOptions = {
1601      onConnect(elementName, remote) {
1602        commRemote = remote;
1603        console.log('----------- onConnect -----------');
1604      },
1605      onDisconnect(elementName) {
1606        console.log('----------- onDisconnect -----------');
1607      },
1608      onFailed(code) {
1609        console.error('----------- onFailed -----------');
1610      }
1611    };
1612    let connection: number;
1613
1614    try {
1615      connection = this.context.connectServiceExtensionAbility(want, options);
1616    } catch (paramError) {
1617      // 处理入参错误异常
1618      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1619    }
1620  }
1621}
1622```
1623
1624## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1625
1626connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1627
1628将当前Ability连接到一个指定account的ServiceExtensionAbility。
1629
1630> **说明:**
1631>
1632> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。  
1633> 当accountId为当前用户时,无需进行权限校验。
1634
1635**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
1636
1637**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1638
1639**系统API**: 此接口为系统接口,三方应用不支持调用。
1640
1641**参数:**
1642
1643| 参数名 | 类型 | 必填 | 说明 |
1644| -------- | -------- | -------- | -------- |
1645| want | [Want](js-apis-app-ability-want.md) | 是 | 启动Ability的want信息。 |
1646| accountId | number | 是 | 系统账号的账号ID,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated)。 |
1647| options | ConnectOptions | 是 | 远端对象实例。 |
1648
1649**返回值:**
1650
1651| 类型 | 说明 |
1652| -------- | -------- |
1653| number | 返回Ability连接的结果code。 |
1654
1655**错误码:**
1656
1657以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1658
1659| 错误码ID | 错误信息 |
1660| ------- | -------- |
1661| 201 | The application does not have permission to call the interface. |
1662| 202 | The application is not system-app, can not use system-api. |
1663| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1664| 16000001 | The specified ability does not exist. |
1665| 16000002 | Incorrect ability type. |
1666| 16000004 | Failed to start the invisible ability. |
1667| 16000005 | The specified process does not have the permission. |
1668| 16000006 | Cross-user operations are not allowed. |
1669| 16000008 | The crowdtesting application expires. |
1670| 16000053 | The ability is not on the top of the UI. |
1671| 16000055 | Installation-free timed out. |
1672| 16000011 | The context does not exist.        |
1673| 16000050 | Internal error. |
1674
1675**示例:**
1676
1677```ts
1678import { ServiceExtensionAbility, Want, common } from '@kit.AbilityKit';
1679import { rpc } from '@kit.IPCKit';
1680import { BusinessError } from '@kit.BasicServicesKit';
1681
1682let commRemote: rpc.IRemoteObject; // 断开连接时需要释放
1683
1684class EntryAbility extends ServiceExtensionAbility {
1685  onCreate() {
1686    let want: Want = {
1687      deviceId: '',
1688      bundleName: 'com.example.myapplication',
1689      abilityName: 'EntryAbility'
1690    };
1691    let accountId = 100;
1692    let options: common.ConnectOptions = {
1693      onConnect(elementName, remote) {
1694        commRemote = remote;
1695        console.log('----------- onConnect -----------');
1696      },
1697      onDisconnect(elementName) {
1698        console.log('----------- onDisconnect -----------');
1699      },
1700      onFailed(code) {
1701        console.log('----------- onFailed -----------');
1702      }
1703    };
1704    let connection: number;
1705
1706    try {
1707      connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1708    } catch (paramError) {
1709      // 处理入参错误异常
1710      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1711    }
1712  }
1713}
1714```
1715
1716## ServiceExtensionContext.disconnectServiceExtensionAbility
1717
1718disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1719
1720将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空。
1721
1722**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1723
1724**系统API**: 此接口为系统接口,三方应用不支持调用。
1725
1726**参数:**
1727
1728| 参数名 | 类型 | 必填 | 说明 |
1729| -------- | -------- | -------- | -------- |
1730| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1731| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,返回接口调用是否成功的结果。 |
1732
1733**错误码:**
1734
1735以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1736
1737| 错误码ID | 错误信息 |
1738| ------- | -------- |
1739| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1740| 16000011 | The context does not exist.        |
1741| 16000050 | Internal error. |
1742
1743**示例:**
1744
1745```ts
1746import { ServiceExtensionAbility } from '@kit.AbilityKit';
1747import { rpc } from '@kit.IPCKit';
1748import { BusinessError } from '@kit.BasicServicesKit';
1749
1750let commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放
1751
1752class EntryAbility extends ServiceExtensionAbility {
1753  onCreate() {
1754    // connection为connectServiceExtensionAbility中的返回值
1755    let connection = 1;
1756    try {
1757      this.context.disconnectServiceExtensionAbility(connection, (error: BusinessError) => {
1758        commRemote = null;
1759        if (error.code) {
1760          // 处理业务逻辑错误
1761          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1762          return;
1763        }
1764        // 执行正常业务
1765        console.log('disconnectServiceExtensionAbility succeed');
1766      });
1767    } catch (paramError) {
1768      commRemote = null;
1769      // 处理入参错误异常
1770      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1771    }
1772  }
1773}
1774```
1775
1776## ServiceExtensionContext.disconnectServiceExtensionAbility
1777
1778disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1779
1780将一个Ability与绑定的服务类型的Ability解绑,断开连接之后需要将连接成功时返回的remote对象置空(Promise形式返回结果)。
1781
1782**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1783
1784**系统API**: 此接口为系统接口,三方应用不支持调用。
1785
1786**参数:**
1787
1788| 参数名 | 类型 | 必填 | 说明 |
1789| -------- | -------- | -------- | -------- |
1790| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1791
1792**返回值:**
1793
1794| 类型 | 说明 |
1795| -------- | -------- |
1796| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1797
1798**错误码:**
1799
1800以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1801
1802| 错误码ID | 错误信息 |
1803| ------- | -------- |
1804| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1805| 16000011 | The context does not exist.        |
1806| 16000050 | Internal error. |
1807
1808**示例:**
1809
1810```ts
1811import { ServiceExtensionAbility } from '@kit.AbilityKit';
1812import { rpc } from '@kit.IPCKit';
1813import { BusinessError } from '@kit.BasicServicesKit';
1814
1815let commRemote: rpc.IRemoteObject | null; // 断开连接时需要释放
1816
1817class EntryAbility extends ServiceExtensionAbility {
1818  onCreate() {
1819    // connection为connectServiceExtensionAbility中的返回值
1820    let connection = 1;
1821    try {
1822      this.context.disconnectServiceExtensionAbility(connection)
1823        .then(() => {
1824          commRemote = null;
1825          // 执行正常业务
1826          console.log('disconnectServiceExtensionAbility succeed');
1827        })
1828        .catch((error: BusinessError) => {
1829          commRemote = null;
1830          // 处理业务逻辑错误
1831          console.error(`disconnectServiceExtensionAbility failed, error.code: ${error.code}, error.message: ${error.message}`);
1832        });
1833    } catch (paramError) {
1834      commRemote = null;
1835      // 处理入参错误异常
1836      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1837    }
1838  }
1839}
1840```
1841
1842## ServiceExtensionContext.startAbilityByCall
1843
1844startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1845
1846启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。
1847
1848使用规则:
1849 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1850 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
1851 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
1852
1853**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION
1854
1855**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1856
1857**系统API**:此接口为系统接口,三方应用不支持调用。
1858
1859**参数:**
1860
1861| 参数名 | 类型 | 必填 | 说明 |
1862| -------- | -------- | -------- | -------- |
1863| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId、parameters(可选),parameters缺省或为空表示后台启动Ability。 |
1864
1865**返回值:**
1866
1867| 类型 | 说明 |
1868| -------- | -------- |
1869| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |
1870
1871**错误码:**
1872
1873以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1874
1875| 错误码ID | 错误信息 |
1876| ------- | -------- |
1877| 201 | The application does not have permission to call the interface. |
1878| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1879| 16000001 | The specified ability does not exist. |
1880| 16000002 | Incorrect ability type. |
1881| 16000004 | Failed to start the invisible ability. |
1882| 16000005 | Static permission denied. The specified process does not have the permission. |
1883| 16000006 | Cross-user operations are not allowed. |
1884| 16000008 | The crowdtesting application expires. |
1885| 16000011 | The context does not exist. |
1886| 16000050 | Internal error. |
1887| 16200001 | The caller has been released.        |
1888
1889**示例:**
1890
1891后台启动:
1892
1893```ts
1894import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
1895import { BusinessError } from '@kit.BasicServicesKit';
1896
1897class EntryAbility extends ServiceExtensionAbility {
1898  onCreate() {
1899    let caller: Caller;
1900    // 后台启动Ability,不配置parameters
1901    let wantBackground: Want = {
1902      bundleName: 'com.example.myservice',
1903      moduleName: 'entry',
1904      abilityName: 'EntryAbility',
1905      deviceId: ''
1906    };
1907
1908    try {
1909      this.context.startAbilityByCall(wantBackground)
1910        .then((obj: Caller) => {
1911          // 执行正常业务
1912          caller = obj;
1913          console.log('startAbilityByCall succeed');
1914        }).catch((error: BusinessError) => {
1915        // 处理业务逻辑错误
1916        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
1917      });
1918    } catch (paramError) {
1919      // 处理入参错误异常
1920      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1921    }
1922  }
1923}
1924```
1925
1926前台启动:
1927
1928```ts
1929import { ServiceExtensionAbility, Caller, Want } from '@kit.AbilityKit';
1930import { BusinessError } from '@kit.BasicServicesKit';
1931
1932class EntryAbility extends ServiceExtensionAbility {
1933  onCreate() {
1934    let caller: Caller;
1935    // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
1936    let wantForeground: Want = {
1937      bundleName: 'com.example.myservice',
1938      moduleName: 'entry',
1939      abilityName: 'EntryAbility',
1940      deviceId: '',
1941      parameters: {
1942        'ohos.aafwk.param.callAbilityToForeground': true
1943      }
1944    };
1945
1946    try {
1947      this.context.startAbilityByCall(wantForeground)
1948        .then((obj: Caller) => {
1949          // 执行正常业务
1950          caller = obj;
1951          console.log('startAbilityByCall succeed');
1952        }).catch((error: BusinessError) => {
1953        // 处理业务逻辑错误
1954        console.error(`startAbilityByCall failed, error.code: ${error.code}, error.message: ${error.message}`);
1955      });
1956    } catch (paramError) {
1957      // 处理入参错误异常
1958      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
1959    }
1960  }
1961}
1962```
1963## ServiceExtensionContext.startRecentAbility
1964
1965startRecentAbility(want: Want, callback: AsyncCallback\<void>): void;
1966
1967启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
1968
1969> **说明:**
1970>
1971> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
1972
1973**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1974
1975**系统API**: 此接口为系统接口,三方应用不支持调用。
1976
1977**参数:**
1978
1979| 参数名 | 类型 | 必填 | 说明 |
1980| -------- | -------- | -------- | -------- |
1981| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 |
1982| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |
1983
1984**错误码:**
1985
1986以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
1987
1988| 错误码ID | 错误信息 |
1989| ------- | -------- |
1990| 201 | The application does not have permission to call the interface. |
1991| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
1992| 16000001 | The specified ability does not exist. |
1993| 16000002 | Incorrect ability type. |
1994| 16000004 | Failed to start the invisible ability. |
1995| 16000005 | The specified process does not have the permission. |
1996| 16000006 | Cross-user operations are not allowed. |
1997| 16000008 | The crowdtesting application expires. |
1998| 16000009 | An ability cannot be started or stopped in Wukong mode. |
1999| 16000010 | The call with the continuation flag is forbidden. |
2000| 16000011 | The context does not exist. |
2001| 16000050 | Internal error. |
2002| 16000053 | The ability is not on the top of the UI. |
2003| 16000055 | Installation-free timed out. |
2004| 16200001 | The caller has been released. |
2005
2006**示例:**
2007
2008```ts
2009import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2010import { BusinessError } from '@kit.BasicServicesKit';
2011
2012class EntryAbility extends ServiceExtensionAbility {
2013  onCreate() {
2014    let want: Want = {
2015      bundleName: 'com.example.myapplication',
2016      abilityName: 'EntryAbility'
2017    };
2018
2019    try {
2020      this.context.startRecentAbility(want, (err: BusinessError) => {
2021        if (err.code) {
2022          // 处理业务逻辑错误
2023          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2024          return;
2025        }
2026        // 执行正常业务
2027        console.info('startRecentAbility succeed');
2028      });
2029    } catch (err) {
2030      // 处理入参错误异常
2031      let code = (err as BusinessError).code;
2032      let message = (err as BusinessError).message;
2033      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2034    }
2035  }
2036}
2037```
2038## ServiceExtensionContext.startRecentAbility
2039
2040startRecentAbility(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void;
2041
2042启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。启动结果以callback的形式返回开发者。
2043当开发者需要携带启动参数时可以选择此API。
2044
2045> **说明:**
2046>
2047> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
2048
2049**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2050
2051**系统API**: 此接口为系统接口,三方应用不支持调用。
2052
2053**参数:**
2054
2055| 参数名 | 类型 | 必填 | 说明 |
2056| -------- | -------- | -------- | -------- |
2057| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 |
2058| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
2059| callback | AsyncCallback\<void> | 是 | 指定的回调函数的结果。 |
2060
2061**错误码:**
2062
2063以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2064
2065| 错误码ID | 错误信息 |
2066| ------- | -------- |
2067| 201 | The application does not have permission to call the interface. |
2068| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2069| 16000001 | The specified ability does not exist. |
2070| 16000002 | Incorrect ability type. |
2071| 16000004 | Failed to start the invisible ability. |
2072| 16000005 | The specified process does not have the permission. |
2073| 16000006 | Cross-user operations are not allowed. |
2074| 16000008 | The crowdtesting application expires. |
2075| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2076| 16000010 | The call with the continuation flag is forbidden. |
2077| 16000011 | The context does not exist. |
2078| 16000050 | Internal error. |
2079| 16000053 | The ability is not on the top of the UI. |
2080| 16000055 | Installation-free timed out. |
2081| 16200001 | The caller has been released. |
2082
2083**示例:**
2084
2085```ts
2086import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
2087import { BusinessError } from '@kit.BasicServicesKit';
2088
2089class EntryAbility extends ServiceExtensionAbility {
2090  onCreate() {
2091    let want: Want = {
2092      deviceId: '',
2093      bundleName: 'com.example.myapplication',
2094      abilityName: 'EntryAbility'
2095    };
2096    let options: StartOptions = {
2097      windowMode: 0
2098    };
2099
2100    try {
2101      this.context.startRecentAbility(want, options, (err: BusinessError) => {
2102        if (err.code) {
2103          // 处理业务逻辑错误
2104          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2105          return;
2106        }
2107        // 执行正常业务
2108        console.info('startRecentAbility succeed');
2109      });
2110    } catch (err) {
2111      // 处理入参错误异常
2112      let code = (err as BusinessError).code;
2113      let message = (err as BusinessError).message;
2114      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2115    }
2116  }
2117}
2118```
2119## ServiceExtensionContext.startRecentAbility
2120
2121startRecentAbility(want: Want, options?: StartOptions): Promise\<void>;
2122
2123启动一个指定的Ability,如果这个Ability有多个实例,将拉起最近启动的那个实例。
2124当开发者期望启动结果以Promise形式返回时可以选择此API。
2125
2126> **说明:**
2127>
2128> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
2129
2130**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2131
2132**系统API**: 此接口为系统接口,三方应用不支持调用。
2133
2134**参数:**
2135
2136| 参数名 | 类型 | 必填 | 说明 |
2137| -------- | -------- | -------- | -------- |
2138| want | [Want](js-apis-app-ability-want.md) | 是 | 需要启动Ability的want信息。 |
2139| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
2140
2141**错误码:**
2142
2143以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2144
2145| 错误码ID | 错误信息 |
2146| ------- | -------- |
2147| 201 | The application does not have permission to call the interface. |
2148| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2149| 16000001 | The specified ability does not exist. |
2150| 16000002 | Incorrect ability type. |
2151| 16000004 | Failed to start the invisible ability. |
2152| 16000005 | The specified process does not have the permission. |
2153| 16000006 | Cross-user operations are not allowed. |
2154| 16000008 | The crowdtesting application expires. |
2155| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2156| 16000010 | The call with the continuation flag is forbidden. |
2157| 16000011 | The context does not exist. |
2158| 16000050 | Internal error. |
2159| 16000053 | The ability is not on the top of the UI. |
2160| 16000055 | Installation-free timed out. |
2161| 16200001 | The caller has been released. |
2162
2163**示例:**
2164
2165```ts
2166import { ServiceExtensionAbility, Want, StartOptions } from '@kit.AbilityKit';
2167import { BusinessError } from '@kit.BasicServicesKit';
2168
2169class EntryAbility extends ServiceExtensionAbility {
2170  onCreate() {
2171    let want: Want = {
2172      bundleName: 'com.example.myapplication',
2173      abilityName: 'EntryAbility'
2174    };
2175    let options: StartOptions = {
2176      windowMode: 0,
2177    };
2178
2179    try {
2180      this.context.startRecentAbility(want, options)
2181        .then(() => {
2182          // 执行正常业务
2183          console.info('startRecentAbility succeed');
2184        })
2185        .catch((err: BusinessError) => {
2186          // 处理业务逻辑错误
2187          console.error(`startRecentAbility failed, code is ${err.code}, message is ${err.message}`);
2188        });
2189    } catch (err) {
2190      // 处理入参错误异常
2191      let code = (err as BusinessError).code;
2192      let message = (err as BusinessError).message;
2193      console.error(`startRecentAbility failed, code is ${code}, message is ${message}`);
2194    }
2195  }
2196}
2197```
2198
2199## ServiceExtensionContext.startAbilityByCallWithAccount<sup>10+</sup>
2200
2201startAbilityByCallWithAccount(want: Want, accountId: number): Promise&lt;Caller&gt;;
2202
2203根据accountId对指定的Ability进行call调用,并且可以使用返回的Caller通信接口与被调用方进行通信。
2204
2205使用规则:
2206 - 跨用户场景下,Call调用目标Ability时,调用方应用需同时申请`ohos.permission.ABILITY_BACKGROUND_COMMUNICATION`与`ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS`权限
2207 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
2208 - 跨应用场景下,目标Ability的exported属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
2209 - 同设备与跨设备场景下,该接口的使用规则存在差异,详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
2210
2211**需要权限**: ohos.permission.ABILITY_BACKGROUND_COMMUNICATION, ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
2212
2213**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2214
2215**系统API**:此接口为系统接口,三方应用不支持调用。
2216
2217**参数:**
2218
2219| 参数名 | 类型 | 必填 | 说明 |
2220| -------- | -------- | -------- | -------- |
2221| want | [Want](js-apis-app-ability-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
2222| accountId | number | 是 | 系统账号的账号ID,-1表示当前活动用户,详情参考[getCreatedOsAccountsCount](../apis-basic-services-kit/js-apis-osAccount.md#getcreatedosaccountscountdeprecated-1)。 |
2223
2224**返回值:**
2225
2226| 类型 | 说明 |
2227| -------- | -------- |
2228| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |
2229
2230**错误码:**
2231
2232以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2233
2234| 错误码ID | 错误信息 |
2235| ------- | -------- |
2236| 201 | The application does not have permission to call the interface. |
2237| 202 | The application is not system-app, can not use system-api. |
2238| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2239| 16000001 | The specified ability does not exist. |
2240| 16000002 | Incorrect ability type. |
2241| 16000004 | Failed to start the invisible ability. |
2242| 16000005 | Static permission denied. The specified process does not have the permission. |
2243| 16000006 | Cross-user operations are not allowed. |
2244| 16000008 | The crowdtesting application expires. |
2245| 16000011 | The context does not exist. |
2246| 16000012 | The application is controlled.        |
2247| 16000013 | The application is controlled by EDM.       |
2248| 16000050 | Internal error. |
2249| 16200001 | The caller has been released.        |
2250
2251**示例:**
2252
2253```ts
2254import { ServiceExtensionAbility, Want, Caller } from '@kit.AbilityKit';
2255import { BusinessError } from '@kit.BasicServicesKit';
2256
2257class EntryAbility extends ServiceExtensionAbility {
2258  onCreate() {
2259    let caller: Caller;
2260    // 系统账号的账号ID, -1表示当前激活用户
2261    let accountId = -1;
2262    // 指定启动的Ability
2263    let want: Want = {
2264      bundleName: 'com.acts.actscalleeabilityrely',
2265      moduleName: 'entry',
2266      abilityName: 'EntryAbility',
2267      deviceId: '',
2268      parameters: {
2269        // 'ohos.aafwk.param.callAbilityToForeground' 值设置为true时为前台启动, 设置false或不设置为后台启动
2270        'ohos.aafwk.param.callAbilityToForeground': true
2271      }
2272    };
2273
2274    try {
2275      this.context.startAbilityByCallWithAccount(want, accountId)
2276        .then((obj: Caller) => {
2277          // 执行正常业务
2278          caller = obj;
2279          console.log('startAbilityByCallWithAccount succeed');
2280        }).catch((error: BusinessError) => {
2281        // 处理业务逻辑错误
2282        console.error(`startAbilityByCallWithAccount failed, error.code: ${error.code}, error.message: ${error.message}`);
2283      });
2284    } catch (paramError) {
2285      // 处理入参错误异常
2286      console.error(`error.code: ${(paramError as BusinessError).code}, error.message: ${(paramError as BusinessError).message}`);
2287    }
2288  }
2289}
2290```
2291
2292## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup>
2293
2294requestModalUIExtension(pickerWant: Want): Promise\<void>
2295
2296请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用promise形式异步回调。
2297
2298在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。
2299
2300> **说明:**
2301>
2302> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。 
2303
2304**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2305
2306**系统接口**: 此接口为系统接口。
2307
2308**参数:**
2309
2310| 参数名 | 类型 | 必填 | 说明 |
2311| -------- | -------- | -------- | -------- |
2312| pickerWant | [Want](js-apis-app-ability-want.md)  | 是 | 拉起UIExtension的want信息。 |
2313
2314**返回值:**
2315
2316| 类型 | 说明 |
2317| -------- | -------- |
2318| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
2319
2320**错误码:**
2321
2322以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2323
2324| 错误码ID | 错误信息 |
2325| ------- | -------- |
2326| 202 | The application is not system-app, can not use system-api. |
2327| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2328| 16000050 | Internal error. |
2329
2330**示例:**
2331
2332```ts
2333import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2334import { BusinessError } from '@kit.BasicServicesKit';
2335
2336class ServiceExtension extends ServiceExtensionAbility {
2337  onCreate() {
2338    let pickerWant: Want = {
2339      bundleName: 'com.example.myapplication',
2340      abilityName: 'UIExtAbility',
2341      moduleName: 'entry_test',
2342      parameters: {
2343        'bundleName': 'com.example.myapplication',
2344        //与com.example.myapplication.UIExtAbility配置的type相同
2345        'ability.want.params.uiExtensionType': 'sys/commonUI'
2346      }
2347    };
2348
2349    try {
2350      this.context.requestModalUIExtension(pickerWant)
2351        .then(() => {
2352          // 执行正常业务
2353          console.info('requestModalUIExtension succeed');
2354        })
2355        .catch((err: BusinessError) => {
2356          // 处理业务逻辑错误
2357          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
2358        });
2359    } catch (err) {
2360      // 处理入参错误异常
2361      let code = (err as BusinessError).code;
2362      let message = (err as BusinessError).message;
2363      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
2364    }
2365  }
2366}
2367```
2368
2369## ServiceExtensionContext.requestModalUIExtension<sup>11+<sup>
2370
2371requestModalUIExtension(pickerWant: Want, callback: AsyncCallback\<void>): void
2372
2373请求在指定的前台应用上拉起对应类型的UIExtensionAbility。其中,前台应用通过want.parameters中bundleName来指定,如果未指定前台应用、bundleName指定的应用未在前台或指定的前台应用的bundleName不正确,则在系统界面上直接拉起UIExtensionAbility;被拉起的UIExtensionAbility通过want中bundleName、abilityName、moduleName字段共同确定,同时需要通过want.parameters中的ability.want.params.uiExtensionType字段配置UIExtensionAbility的类型。使用callback形式异步回调。
2374
2375在前台应用上拉起UIExtensionAility之前,必须确保该应用已完成页面初始化,否则将导致拉起失败、并出现"uiContent is nullptr"的报错信息。应用可通过监听页面加载状态来判断拉起UIExtensionAbility的时机,页面初始化成功后会出现关键日志信息"UIContentImpl: focus again"。
2376
2377> **说明:**
2378>
2379> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
2380
2381**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2382
2383**系统接口**:此接口为系统接口。
2384
2385**参数:**
2386
2387| 参数名 | 类型 | 必填 | 说明 |
2388| -------- | -------- | -------- | -------- |
2389| pickerWant | [Want](js-apis-app-ability-want.md)  | 是 | 拉起UIExtension的want信息。 |
2390| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当拉起UIExtension成功,err为undefined,否则为错误对象。 |
2391
2392**错误码:**
2393
2394以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2395
2396| 错误码ID | 错误信息 |
2397| ------- | -------- |
2398| 202 | The application is not system-app, can not use system-api. |
2399| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2400| 16000050 | Internal error. |
2401
2402**示例:**
2403
2404```ts
2405import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2406import { BusinessError } from '@kit.BasicServicesKit';
2407
2408class ServiceExtension extends ServiceExtensionAbility {
2409  onCreate() {
2410    let pickerWant: Want = {
2411      bundleName: 'com.example.myapplication',
2412      abilityName: 'com.example.myapplication.UIExtAbility',
2413      moduleName: 'entry_test',
2414      parameters: {
2415        'bundleName': 'com.example.myapplication',
2416        //与com.example.myapplication.UIExtAbility配置的type相同
2417        'ability.want.params.uiExtensionType': 'sys/commonUI'
2418      }
2419    };
2420
2421    try {
2422      this.context.requestModalUIExtension(pickerWant, (err: BusinessError) => {
2423        if (err.code) {
2424          // 处理业务逻辑错误
2425          console.error(`requestModalUIExtension failed, code is ${err.code}, message is ${err.message}`);
2426          return;
2427        }
2428        // 执行正常业务
2429        console.info('requestModalUIExtension succeed');
2430      });
2431    } catch (err) {
2432      // 处理入参错误异常
2433      let code = (err as BusinessError).code;
2434      let message = (err as BusinessError).message;
2435      console.error(`requestModalUIExtension failed, code is ${code}, message is ${message}`);
2436    }
2437  }
2438}
2439```
2440
2441## ServiceExtensionContext.openLink<sup>12+<sup>
2442openLink(link:string, options?: OpenLinkOptions): Promise&lt;void&gt;
2443
2444通过AppLinking启动UIAbility,使用Promise异步回调。
2445
2446通过在link字段中传入标准格式的URL,基于隐式want匹配规则拉起目标UIAbility。目标方必须具备以下过滤器特征,才能处理AppLinking链接:
2447- "actions"列表中包含"ohos.want.action.viewData"。
2448- "entities"列表中包含"entity.system.browsable"。
2449- "uris"列表中包含"scheme"为"https"且"domainVerify"为true的元素。
2450
2451传入的参数不合法时,如未设置必选参数或link字符串不是标准格式的URL,接口会直接抛出异常。参数校验通过,拉起目标方时出现的错误通过promise返回错误信息。
2452
2453> **说明:**
2454>
2455> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
2456
2457**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2458
2459**系统接口**: 此接口为系统接口。
2460
2461**参数:**
2462
2463| 参数名 | 类型 | 必填 | 说明 |
2464| -------- | -------- | -------- | -------- |
2465| link | string | 是 | 指示要打开的标准格式URL。 |
2466| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | 否 | 打开URL的选项参数。 |
2467
2468**返回值:**
2469
2470| 类型 | 说明 |
2471| -------- | -------- |
2472| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
2473
2474**错误码:**
2475
2476以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2477
2478| 错误码ID | 错误信息 |
2479| ------- | -------- |
2480| 201 | The application does not have permission to call the interface. |
2481| 202 | The application is not system-app, can not use system-api. |
2482| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2483| 16000001 | The specified ability does not exist. |
2484| 16000002 | Incorrect ability type. |
2485| 16000004 | Failed to start the invisible ability. |
2486| 16000005 | The specified process does not have the permission. |
2487| 16000006 | Cross-user operations are not allowed. |
2488| 16000008 | The crowdtesting application expires. |
2489| 16000009 | An ability cannot be started or stopped in Wukong mode. |
2490| 16000010 | The call with the continuation flag is forbidden.        |
2491| 16000011 | The context does not exist.        |
2492| 16000012 | The application is controlled.        |
2493| 16000013 | The application is controlled by EDM.       |
2494| 16000019 | No matching ability is found. |
2495| 16200001 | The caller has been released. |
2496
2497**示例:**
2498
2499```ts
2500import { ServiceExtensionAbility, Want, OpenLinkOptions } from '@kit.AbilityKit';
2501import { BusinessError } from '@kit.BasicServicesKit';
2502
2503function log(info: string) {
2504  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
2505}
2506
2507export default class ServiceExtAbility extends ServiceExtensionAbility {
2508  onCreate(want: Want) {
2509    log(`ServiceExtAbility OnCreate`);
2510  }
2511
2512  onRequest(want: Want, startId: number) {
2513    log(`ServiceExtAbility onRequest`);
2514    let link: string = 'https://www.example.com';
2515    let openLinkOptions: OpenLinkOptions = {
2516      appLinkingOnly: false
2517    };
2518    try {
2519      this.context.openLink(
2520        link,
2521        openLinkOptions
2522      ).then(() => {
2523        log(`open link success.`);
2524      }).catch((err: BusinessError) => {
2525        log(`open link failed, errCode ${JSON.stringify(err.code)}`);
2526      });
2527    }
2528    catch (e) {
2529      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
2530    }
2531  }
2532
2533  onDestroy() {
2534    log(`ServiceExtAbility onDestroy`);
2535  }
2536}
2537```
2538
2539## ServiceExtensionContext.preStartMission<sup>12+<sup>
2540preStartMission(bundleName:string, moduleName: string, abilitName: string, startTime: string): Promise&lt;void&gt;
2541
2542打开原子化服务跳过loading框并预打开窗口,使用Promise异步回调。
2543
2544参数校验通过,拉起目标方时出现的错误需要通过异常机制捕获。
2545
2546**需要权限**:ohos.permission.PRE_START_ATOMIC_SERVICE
2547
2548**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2549
2550**系统接口**: 此接口为系统接口。
2551
2552**参数:**
2553
2554| 参数名 | 类型 | 必填 | 说明 |
2555| -------- | -------- | -------- | -------- |
2556| bundleName | string | 是 | 打开原子化服务对应的包名。 |
2557| moduleName | string | 是 | 打开原子化服务对应的模块名。 |
2558| abilityName | string | 是 | 打开原子化服务对应的能力名。 |
2559| startTime | string | 是 | 打开原子化服务对应的开始时间,单位为毫秒级的时间戳。 |
2560
2561
2562**返回值:**
2563
2564| 类型 | 说明 |
2565| -------- | -------- |
2566| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
2567
2568**错误码:**
2569
2570以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2571
2572| 错误码ID | 错误信息 |
2573| ------- | -------- |
2574| 201 | The application does not have permission to call the interface. |
2575| 202 | The application is not system-app, can not use system-api. |
2576| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2577| 16300007 | The target free install task does not exist. |
2578| 16000011 | The context does not exist.        |
2579
2580**示例:**
2581
2582```ts
2583import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2584import { BusinessError } from '@kit.BasicServicesKit';
2585
2586function log(info: string) {
2587  console.error(`[ServiceExtApp]:: ${JSON.stringify(info)}`);
2588}
2589
2590export default class ServiceExtAbility extends ServiceExtensionAbility {
2591  onCreate(want: Want) {
2592    log(`ServiceExtAbility OnCreate`);
2593  }
2594
2595  onRequest(want: Want, startId: number) {
2596    log(`ServiceExtAbility onRequest`);
2597    try {
2598      this.context.preStartMission(
2599        want.bundleName,
2600        want.moduleName,
2601        want.abilityName,
2602        want.parameters["ohos.aafwk.param.startTime"]
2603      ).then(() => {
2604        log(`pre-start mission success.`);
2605      }).catch((err: BusinessError) => {
2606        log(`pre-start mission failed, errCode ${JSON.stringify(err.code)}`);
2607      });
2608    }
2609    catch (e) {
2610      log(`exception occured, errCode ${JSON.stringify(e.code)}`);
2611    }
2612  }
2613
2614  onDestroy() {
2615    log(`ServiceExtAbility onDestroy`);
2616  }
2617}
2618```
2619
2620## ServiceExtensionContext.startUIServiceExtensionAbility<sup>13+<sup>
2621startUIServiceExtensionAbility(want: Want): Promise&lt;void&gt;
2622
2623启动一个新的[UIServiceExtensionAbility](js-apis-app-ability-uiServiceExtensionAbility-sys.md)(Promise形式)。
2624
2625
2626> **说明:**
2627>
2628> 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)。
2629>
2630
2631**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
2632
2633**系统接口**: 此接口为系统接口。
2634
2635**参数:**
2636| 参数名 | 类型 | 只读 | 可选 | 说明                 |
2637| ------ | ---- | ---- | -------------------- | -------------------- |
2638| want   | [Want](js-apis-app-ability-want.md) | 是  | 否 | [Want](js-apis-app-ability-want.md)类型参数,传入需要启动的Ability的信息,如Ability名称,Bundle名称等。 |
2639
2640**返回值:**
2641
2642| 类型                | 说明                                   |
2643| ------------------- | -------------------------------------- |
2644| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
2645
2646**错误码:**
2647
2648以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
2649| 错误码ID | 错误信息                                                              |
2650| -------- | ---------------------------------------------------------------------|
2651| 201      | The application does not have permission to call the interface.      |
2652| 202      | The application is not system-app, can not use system-api.           |
2653| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
2654| 801      | The Ability is not supported.                       |
2655| 16000001 | The specified ability does not exist.               |
2656| 16000002 | Incorrect ability type.                             |
2657| 16000004 | Failed to start the invisible ability.              |
2658| 16000005 | The specified process does not have the permission. |
2659| 16000006 | Cross-user operations are not allowed.              |
2660| 16000008 | The crowdtesting application expires.               |
2661| 16000011 | The context does not exist.                         |
2662| 16000012 | The application is controlled.                      |
2663| 16000013 | The application is controlled by EDM.               |
2664| 16000019 | No matching ability is found.                       |
2665| 16000050 | Internal error.                                     |
2666| 16200001 | The caller has been released.                       |
2667
2668**示例:**
2669
2670```ts
2671import { BusinessError } from '@ohos.base';
2672import { ServiceExtensionAbility, Want } from '@kit.AbilityKit';
2673
2674export default class MyServiceExtensionAbility extends ServiceExtensionAbility {
2675  onRequest(want: Want, startId: number) {
2676    const startWant: Want = {
2677      bundleName: 'com.example.myapplication',
2678      abilityName: 'UIServiceExtensionAbility'
2679    }
2680    // 启动一个UIServiceExtensionAbility
2681    this.context.startUIServiceExtensionAbility(startWant).then(() => {
2682      console.info('succeeded');
2683    }).catch((error: BusinessError) => {
2684      console.error(`error code: ${error.code}, error essage : ${error.message}`);
2685    })
2686  }
2687}
2688```