• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ServiceExtensionContext
2
3ServiceExtensionContext模块是ServiceExtensionAbility的上下文环境,继承自ExtensionContext。
4
5ServiceExtensionContext模块提供ServiceExtensionAbility具有的能力和接口,包括启动、停止、绑定、解绑Ability。
6
7> **说明:**
8>
9>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
10>  - 本模块接口仅可在Stage模型下使用。
11
12## 使用说明
13
14在使用ServiceExtensionContext的功能前,需要通过ServiceExtensionAbility子类实例获取。
15
16```ts
17  import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
18
19  let context = undefined;
20  class MainAbility extends ServiceExtensionAbility {
21    onCreate() {
22      context = this.context; // 获取ServiceExtensionContext
23    }
24  }
25```
26
27## ServiceExtensionContext.startAbility
28
29startAbility(want: Want, callback: AsyncCallback<void>): void;
30
31启动Ability。
32
33**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
34
35**系统API**: 此接口为系统接口,三方应用不支持调用。
36
37**参数:**
38
39| 参数名 | 类型 | 必填 | 说明 |
40| -------- | -------- | -------- | -------- |
41| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 |
42| callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 |
43
44**错误码:**
45
46| 错误码ID | 错误信息 |
47| ------- | -------------------------------- |
48| 201 | The application does not have permission to call the interface. |
49| 401 | Invalid input parameter. |
50| 16000001 | Input error. The specified ability name does not exist. |
51| 16000004 | Visibility verification failed. |
52| 16000005 | Static permission denied. The specified process does not have the permission. |
53| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
54| 16000008 | Crowdtest App Expiration. |
55| 16000009 | Can not start ability in wukong mode. |
56| 16000010 | Can not operation with continue flag.        |
57| 16000011 | Context does not exist.        |
58| 16000017 | The previous ability is starting, wait start later.        |
59| 16000051 | Network error. The network is abnormal. |
60| 16000052 | Free install not support. The application does not support freeinstall |
61| 16000053 | Not top ability. The application is not top ability. |
62| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
63| 16000055 | Free install timeout. |
64| 16000056 | Can not free install other ability. |
65| 16000057 | Not support cross device free install. |
66| 16200001 | Caller released. The caller has been released. |
67| 16000050 | Internal Error. |
68
69**示例:**
70
71  ```ts
72  let want = {
73    bundleName: 'com.example.myapp',
74    abilityName: 'MyAbility'
75  };
76
77  try {
78    this.context.startAbility(want, (error) => {
79      if (error.code) {
80        // 处理业务逻辑错误
81        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
82          ' error.message: ' + JSON.stringify(error.message));
83        return;
84      }
85      // 执行正常业务
86      console.log('startAbility succeed');
87    });
88  } catch (paramError) {
89    // 处理入参错误异常
90    console.log('error.code: ' + JSON.stringify(paramError.code) +
91      ' error.message: ' + JSON.stringify(paramError.message));
92  }
93  ```
94
95## ServiceExtensionContext.startAbility
96
97startAbility(want: Want, options?: StartOptions): Promise\<void>;
98
99启动Ability。通过Promise返回结果。
100
101**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
102
103**系统API**: 此接口为系统接口,三方应用不支持调用。
104
105**参数:**
106
107| 参数名 | 类型 | 必填 | 说明 |
108| -------- | -------- | -------- | -------- |
109| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 |
110| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
111
112**返回值:**
113
114| 类型 | 说明 |
115| -------- | -------- |
116| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
117
118**错误码:**
119
120| 错误码ID | 错误信息 |
121| ------- | -------------------------------- |
122| 201 | The application does not have permission to call the interface. |
123| 401 | Invalid input parameter. |
124| 16000001 | Input error. The specified ability name does not exist. |
125| 16000004 | Visibility verification failed. |
126| 16000005 | Static permission denied. The specified process does not have the permission. |
127| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
128| 16000008 | Crowdtest App Expiration. |
129| 16000009 | Can not start ability in wukong mode. |
130| 16000010 | Can not operation with continue flag.        |
131| 16000011 | Context does not exist.        |
132| 16000017 | The previous ability is starting, wait start later.        |
133| 16000051 | Network error. The network is abnormal. |
134| 16000052 | Free install not support. The application does not support freeinstall |
135| 16000053 | Not top ability. The application is not top ability. |
136| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
137| 16000055 | Free install timeout. |
138| 16000056 | Can not free install other ability. |
139| 16000057 | Not support cross device free install. |
140| 16200001 | Caller released. The caller has been released. |
141| 16000050 | Internal Error. |
142
143**示例:**
144
145  ```ts
146  let want = {
147    bundleName: 'com.example.myapp',
148    abilityName: 'MyAbility'
149  };
150  let options = {
151  	windowMode: 0,
152  };
153
154  try {
155    this.context.startAbility(want, options)
156      .then((data) => {
157        // 执行正常业务
158        console.log('startAbility succeed');
159      })
160      .catch((error) => {
161        // 处理业务逻辑错误
162        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
163          ' error.message: ' + JSON.stringify(error.message));
164      });
165  } catch (paramError) {
166    // 处理入参错误异常
167    console.log('error.code: ' + JSON.stringify(paramError.code) +
168      ' error.message: ' + JSON.stringify(paramError.message));
169  }
170  ```
171
172## ServiceExtensionContext.startAbility
173
174startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
175
176启动Ability。
177
178**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
179
180**系统API**: 此接口为系统接口,三方应用不支持调用。
181
182**参数:**
183
184| 参数名 | 类型 | 必填 | 说明 |
185| -------- | -------- | -------- | -------- |
186| want | [Want](js-apis-application-want.md)  | 是 | 启动Ability的want信息。 |
187| options | [StartOptions](js-apis-app-ability-startOptions.md) | 是 | 启动Ability所携带的参数。 |
188| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回启动结果。 |
189
190**错误码:**
191
192| 错误码ID | 错误信息 |
193| ------- | -------------------------------- |
194| 201 | The application does not have permission to call the interface. |
195| 401 | Invalid input parameter. |
196| 16000001 | Input error. The specified ability name does not exist. |
197| 16000004 | Visibility verification failed. |
198| 16000005 | Static permission denied. The specified process does not have the permission. |
199| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
200| 16000008 | Crowdtest App Expiration. |
201| 16000009 | Can not start ability in wukong mode. |
202| 16000010 | Can not operation with continue flag.        |
203| 16000011 | Context does not exist.        |
204| 16000017 | The previous ability is starting, wait start later.        |
205| 16000051 | Network error. The network is abnormal. |
206| 16000052 | Free install not support. The application does not support freeinstall |
207| 16000053 | Not top ability. The application is not top ability. |
208| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
209| 16000055 | Free install timeout. |
210| 16000056 | Can not free install other ability. |
211| 16000057 | Not support cross device free install. |
212| 16200001 | Caller released. The caller has been released. |
213| 16000050 | Internal Error. |
214
215**示例:**
216
217  ```ts
218  let want = {
219    deviceId: '',
220    bundleName: 'com.extreme.test',
221    abilityName: 'MainAbility'
222  };
223  let options = {
224    windowMode: 0
225  };
226
227  try {
228    this.context.startAbility(want, options, (error) => {
229      if (error.code) {
230        // 处理业务逻辑错误
231        console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
232          ' error.message: ' + JSON.stringify(error.message));
233        return;
234      }
235      // 执行正常业务
236      console.log('startAbility succeed');
237    });
238  } catch (paramError) {
239    // 处理入参错误异常
240    console.log('error.code: ' + JSON.stringify(paramError.code) +
241      ' error.message: ' + JSON.stringify(paramError.message));
242  }
243  ```
244
245## ServiceExtensionContext.startAbilityWithAccount
246
247startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
248
249根据account启动Ability(callback形式)。
250
251使用规则:
252 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
253 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
254 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
255
256**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
257
258**系统API**: 此接口为系统接口,三方应用不支持调用。
259
260**参数:**
261
262| 参数名 | 类型 | 必填 | 说明 |
263| -------- | -------- | -------- | -------- |
264| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
265| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
266| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
267
268**错误码:**
269
270| 错误码ID | 错误信息 |
271| ------- | -------------------------------- |
272| 201 | The application does not have permission to call the interface. |
273| 401 | Invalid input parameter. |
274| 16000001 | Input error. The specified ability name does not exist. |
275| 16000004 | Visibility verification failed. |
276| 16000005 | Static permission denied. The specified process does not have the permission. |
277| 16000006 | Can not cross user operations. |
278| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
279| 16000008 | Crowdtest App Expiration. |
280| 16000009 | Can not start ability in wukong mode. |
281| 16000010 | Can not operation with continue flag.        |
282| 16000011 | Context does not exist.        |
283| 16000017 | The previous ability is starting, wait start later.        |
284| 16000051 | Network error. The network is abnormal. |
285| 16000052 | Free install not support. The application does not support freeinstall |
286| 16000053 | Not top ability. The application is not top ability. |
287| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
288| 16000055 | Free install timeout. |
289| 16000056 | Can not free install other ability. |
290| 16000057 | Not support cross device free install. |
291| 16200001 | Caller released. The caller has been released. |
292| 16000050 | Internal Error. |
293
294**示例:**
295
296  ```ts
297  let want = {
298    deviceId: '',
299    bundleName: 'com.extreme.test',
300    abilityName: 'MainAbility'
301  };
302  let accountId = 100;
303
304  try {
305    this.context.startAbilityWithAccount(want, accountId, (error) => {
306      if (error.code) {
307        // 处理业务逻辑错误
308        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
309          ' error.message: ' + JSON.stringify(error.message));
310        return;
311      }
312      // 执行正常业务
313      console.log('startAbilityWithAccount succeed');
314    });
315  } catch (paramError) {
316    // 处理入参错误异常
317    console.log('error.code: ' + JSON.stringify(paramError.code) +
318      ' error.message: ' + JSON.stringify(paramError.message));
319  }
320  ```
321
322## ServiceExtensionContext.startAbilityWithAccount
323
324startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
325
326根据account启动Ability(callback形式)。
327
328使用规则:
329 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
330 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
331 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
332
333**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
334
335**系统API**: 此接口为系统接口,三方应用不支持调用。
336
337**参数:**
338
339| 参数名 | 类型 | 必填 | 说明 |
340| -------- | -------- | -------- | -------- |
341| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
342| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
343| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
344| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
345
346**错误码:**
347
348| 错误码ID | 错误信息 |
349| ------- | -------------------------------- |
350| 201 | The application does not have permission to call the interface. |
351| 401 | Invalid input parameter. |
352| 16000001 | Input error. The specified ability name does not exist. |
353| 16000004 | Visibility verification failed. |
354| 16000005 | Static permission denied. The specified process does not have the permission. |
355| 16000006 | Can not cross user operations. |
356| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
357| 16000008 | Crowdtest App Expiration. |
358| 16000009 | Can not start ability in wukong mode. |
359| 16000010 | Can not operation with continue flag.        |
360| 16000011 | Context does not exist.        |
361| 16000017 | The previous ability is starting, wait start later.        |
362| 16000051 | Network error. The network is abnormal. |
363| 16000052 | Free install not support. The application does not support freeinstall |
364| 16000053 | Not top ability. The application is not top ability. |
365| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
366| 16000055 | Free install timeout. |
367| 16000056 | Can not free install other ability. |
368| 16000057 | Not support cross device free install. |
369| 16200001 | Caller released. The caller has been released. |
370| 16000050 | Internal Error. |
371
372**示例:**
373
374  ```ts
375  let want = {
376    deviceId: '',
377    bundleName: 'com.extreme.test',
378    abilityName: 'MainAbility'
379  };
380  let accountId = 100;
381  let options = {
382    windowMode: 0
383  };
384
385  try {
386    this.context.startAbilityWithAccount(want, accountId, options, (error) => {
387      if (error.code) {
388        // 处理业务逻辑错误
389        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
390          ' error.message: ' + JSON.stringify(error.message));
391        return;
392      }
393      // 执行正常业务
394      console.log('startAbilityWithAccount succeed');
395    });
396  } catch (paramError) {
397    // 处理入参错误异常
398    console.log('error.code: ' + JSON.stringify(paramError.code) +
399      ' error.message: ' + JSON.stringify(paramError.message));
400  }
401  ```
402
403
404## ServiceExtensionContext.startAbilityWithAccount
405
406startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void>;
407
408根据account启动Ability(Promise形式)。
409
410使用规则:
411 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
412 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
413 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
414
415**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
416
417**系统API**: 此接口为系统接口,三方应用不支持调用。
418
419**参数:**
420
421| 参数名 | 类型 | 必填 | 说明 |
422| -------- | -------- | -------- | -------- |
423| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
424| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。。 |
425| options | [StartOptions](js-apis-app-ability-startOptions.md) | 否 | 启动Ability所携带的参数。 |
426
427**返回值:**
428
429| 类型 | 说明 |
430| -------- | -------- |
431| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
432
433**错误码:**
434
435| 错误码ID | 错误信息 |
436| ------- | -------------------------------- |
437| 201 | The application does not have permission to call the interface. |
438| 401 | Invalid input parameter. |
439| 16000001 | Input error. The specified ability name does not exist. |
440| 16000004 | Visibility verification failed. |
441| 16000005 | Static permission denied. The specified process does not have the permission. |
442| 16000006 | Can not cross user operations. |
443| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
444| 16000008 | Crowdtest App Expiration. |
445| 16000009 | Can not start ability in wukong mode. |
446| 16000010 | Can not operation with continue flag.        |
447| 16000011 | Context does not exist.        |
448| 16000017 | The previous ability is starting, wait start later.        |
449| 16000051 | Network error. The network is abnormal. |
450| 16000052 | Free install not support. The application does not support freeinstall |
451| 16000053 | Not top ability. The application is not top ability. |
452| 16000054 | Free install busyness. There are concurrent tasks, waiting for retry. |
453| 16000055 | Free install timeout. |
454| 16000056 | Can not free install other ability. |
455| 16000057 | Not support cross device free install. |
456| 16200001 | Caller released. The caller has been released. |
457| 16000050 | Internal Error. |
458
459**示例:**
460
461  ```ts
462  let want = {
463    deviceId: '',
464    bundleName: 'com.extreme.test',
465    abilityName: 'MainAbility'
466  };
467  let accountId = 100;
468  let options = {
469    windowMode: 0
470  };
471
472  try {
473    this.context.startAbilityWithAccount(want, accountId, options)
474      .then((data) => {
475        // 执行正常业务
476        console.log('startAbilityWithAccount succeed');
477      })
478      .catch((error) => {
479        // 处理业务逻辑错误
480        console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
481          ' error.message: ' + JSON.stringify(error.message));
482      });
483  } catch (paramError) {
484    // 处理入参错误异常
485    console.log('error.code: ' + JSON.stringify(paramError.code) +
486      ' error.message: ' + JSON.stringify(paramError.message));
487  }
488  ```
489
490## ServiceExtensionContext.startServiceExtensionAbility
491
492startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
493
494启动一个新的ServiceExtensionAbility(callback形式)。
495
496**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
497
498**系统API**: 此接口为系统接口,三方应用不支持调用。
499
500**参数:**
501
502| 参数名 | 类型 | 必填 | 说明 |
503| -------- | -------- | -------- | -------- |
504| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
505| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
506
507**错误码:**
508
509| 错误码ID | 错误信息 |
510| ------- | -------------------------------- |
511| 201 | The application does not have permission to call the interface. |
512| 401 | Invalid input parameter. |
513| 16000001 | Input error. The specified ability name does not exist. |
514| 16000002 | Ability type error. The specified ability type is wrong. |
515| 16000004 | Visibility verification failed. |
516| 16000005 | Static permission denied. The specified process does not have the permission. |
517| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
518| 16000008 | Crowdtest App Expiration. |
519| 16000009 | Can not start ability in wukong mode. |
520| 16000011 | Context does not exist.        |
521| 16200001 | Caller released. The caller has been released. |
522| 16000050 | Internal Error. |
523
524**示例:**
525
526  ```ts
527  let want = {
528    deviceId: '',
529    bundleName: 'com.extreme.test',
530    abilityName: 'MainAbility'
531  };
532
533  try {
534    this.context.startServiceExtensionAbility(want, (error) => {
535      if (error.code) {
536        // 处理业务逻辑错误
537        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
538          ' error.message: ' + JSON.stringify(error.message));
539        return;
540      }
541      // 执行正常业务
542      console.log('startServiceExtensionAbility succeed');
543    });
544  } catch (paramError) {
545    // 处理入参错误异常
546    console.log('error.code: ' + JSON.stringify(paramError.code) +
547      ' error.message: ' + JSON.stringify(paramError.message));
548  }
549  ```
550
551## ServiceExtensionContext.startServiceExtensionAbility
552
553startServiceExtensionAbility(want: Want): Promise\<void>;
554
555启动一个新的ServiceExtensionAbility(Promise形式)。
556
557**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
558
559**系统API**: 此接口为系统接口,三方应用不支持调用。
560
561**参数:**
562
563| 参数名 | 类型 | 必填 | 说明 |
564| -------- | -------- | -------- | -------- |
565| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
566
567**返回值:**
568
569| 类型 | 说明 |
570| -------- | -------- |
571| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
572
573**错误码:**
574
575| 错误码ID | 错误信息 |
576| ------- | -------------------------------- |
577| 201 | The application does not have permission to call the interface. |
578| 401 | Invalid input parameter. |
579| 16000001 | Input error. The specified ability name does not exist. |
580| 16000002 | Ability type error. The specified ability type is wrong. |
581| 16000004 | Visibility verification failed. |
582| 16000005 | Static permission denied. The specified process does not have the permission. |
583| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
584| 16000008 | Crowdtest App Expiration. |
585| 16000009 | Can not start ability in wukong mode. |
586| 16000011 | Context does not exist.        |
587| 16200001 | Caller released. The caller has been released. |
588| 16000050 | Internal Error. |
589
590**示例:**
591
592  ```ts
593  let want = {
594    deviceId: '',
595    bundleName: 'com.extreme.test',
596    abilityName: 'MainAbility'
597  };
598
599  try {
600    this.context.startServiceExtensionAbility(want)
601      .then((data) => {
602        // 执行正常业务
603        console.log('startServiceExtensionAbility succeed');
604      })
605      .catch((error) => {
606        // 处理业务逻辑错误
607        console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
608          ' error.message: ' + JSON.stringify(error.message));
609      });
610  } catch (paramError) {
611    // 处理入参错误异常
612    console.log('error.code: ' + JSON.stringify(paramError.code) +
613      ' error.message: ' + JSON.stringify(paramError.message));
614  }
615  ```
616
617## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
618
619startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
620
621启动一个新的ServiceExtensionAbility(callback形式)。
622
623**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
624
625**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
626
627**系统API**: 此接口为系统接口,三方应用不支持调用。
628
629**参数:**
630
631| 参数名 | 类型 | 必填 | 说明 |
632| -------- | -------- | -------- | -------- |
633| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
634| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
635| callback | AsyncCallback\<void\> | 是 | 启动Ability的回调函数。 |
636
637**错误码:**
638
639| 错误码ID | 错误信息 |
640| ------- | -------------------------------- |
641| 201 | The application does not have permission to call the interface. |
642| 401 | Invalid input parameter. |
643| 16000001 | Input error. The specified ability name does not exist. |
644| 16000002 | Ability type error. The specified ability type is wrong. |
645| 16000004 | Visibility verification failed. |
646| 16000005 | Static permission denied. The specified process does not have the permission. |
647| 16000006 | Can not cross user operations. |
648| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
649| 16000008 | Crowdtest App Expiration. |
650| 16000009 | Can not start ability in wukong mode. |
651| 16000011 | Context does not exist.        |
652| 16200001 | Caller released. The caller has been released. |
653| 16000050 | Internal Error. |
654
655
656**示例:**
657
658  ```ts
659  let want = {
660    deviceId: '',
661    bundleName: 'com.extreme.test',
662    abilityName: 'MainAbility'
663  };
664  let accountId = 100;
665
666  try {
667    this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
668      if (error.code) {
669        // 处理业务逻辑错误
670        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
671          ' error.message: ' + JSON.stringify(error.message));
672        return;
673      }
674      // 执行正常业务
675      console.log('startServiceExtensionAbilityWithAccount succeed');
676    });
677  } catch (paramError) {
678    // 处理入参错误异常
679    console.log('error.code: ' + JSON.stringify(paramError.code) +
680      ' error.message: ' + JSON.stringify(paramError.message));
681  }
682  ```
683
684## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
685
686startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
687
688启动一个新的ServiceExtensionAbility(Promise形式)。
689
690**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
691
692**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
693
694**系统API**: 此接口为系统接口,三方应用不支持调用。
695
696**参数:**
697
698| 参数名 | 类型 | 必填 | 说明 |
699| -------- | -------- | -------- | -------- |
700| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
701| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
702
703**返回值:**
704
705| 类型 | 说明 |
706| -------- | -------- |
707| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
708
709**错误码:**
710
711| 错误码ID | 错误信息 |
712| ------- | -------------------------------- |
713| 201 | The application does not have permission to call the interface. |
714| 401 | Invalid input parameter. |
715| 16000001 | Input error. The specified ability name does not exist. |
716| 16000002 | Ability type error. The specified ability type is wrong. |
717| 16000004 | Visibility verification failed. |
718| 16000005 | Static permission denied. The specified process does not have the permission. |
719| 16000006 | Can not cross user operations. |
720| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
721| 16000008 | Crowdtest App Expiration. |
722| 16000009 | Can not start ability in wukong mode. |
723| 16000011 | Context does not exist.        |
724| 16200001 | Caller released. The caller has been released. |
725| 16000050 | Internal Error. |
726
727**示例:**
728
729  ```ts
730  let want = {
731    deviceId: '',
732    bundleName: 'com.extreme.test',
733    abilityName: 'MainAbility'
734  };
735  let accountId = 100;
736
737  try {
738    this.context.startServiceExtensionAbilityWithAccount(want, accountId)
739      .then((data) => {
740        // 执行正常业务
741        console.log('startServiceExtensionAbilityWithAccount succeed');
742      })
743      .catch((error) => {
744        // 处理业务逻辑错误
745        console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
746          ' error.message: ' + JSON.stringify(error.message));
747      });
748  } catch (paramError) {
749    // 处理入参错误异常
750    console.log('error.code: ' + JSON.stringify(paramError.code) +
751      ' error.message: ' + JSON.stringify(paramError.message));
752  }
753  ```
754
755## ServiceExtensionContext.stopServiceExtensionAbility
756
757stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void;
758
759停止同一应用程序内的服务(callback形式)。
760
761**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
762
763**系统API**: 此接口为系统接口,三方应用不支持调用。
764
765**参数:**
766
767| 参数名 | 类型 | 必填 | 说明 |
768| -------- | -------- | -------- | -------- |
769| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
770| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
771
772**错误码:**
773
774| 错误码ID | 错误信息 |
775| ------- | -------------------------------- |
776| 201 | The application does not have permission to call the interface. |
777| 401 | Invalid input parameter. |
778| 16000001 | Input error. The specified ability name does not exist. |
779| 16000002 | Ability type error. The specified ability type is wrong. |
780| 16000004 | Visibility verification failed. |
781| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
782| 16000011 | Context does not exist.        |
783| 16200001 | Caller released. The caller has been released. |
784| 16000050 | Internal Error. |
785
786**示例:**
787
788  ```ts
789  let want = {
790    deviceId: '',
791    bundleName: 'com.extreme.test',
792    abilityName: 'MainAbility'
793  };
794
795  try {
796    this.context.stopServiceExtensionAbility(want, (error) => {
797      if (error.code) {
798        // 处理业务逻辑错误
799        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
800          ' error.message: ' + JSON.stringify(error.message));
801        return;
802      }
803      // 执行正常业务
804      console.log('stopServiceExtensionAbility succeed');
805    });
806  } catch (paramError) {
807    // 处理入参错误异常
808    console.log('error.code: ' + JSON.stringify(paramError.code) +
809      ' error.message: ' + JSON.stringify(paramError.message));
810  }
811  ```
812
813## ServiceExtensionContext.stopServiceExtensionAbility
814
815stopServiceExtensionAbility(want: Want): Promise\<void>;
816
817停止同一应用程序内的服务(Promise形式)。
818
819**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
820
821**系统API**: 此接口为系统接口,三方应用不支持调用。
822
823**参数:**
824
825| 参数名 | 类型 | 必填 | 说明 |
826| -------- | -------- | -------- | -------- |
827| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
828
829**返回值:**
830
831| 类型 | 说明 |
832| -------- | -------- |
833| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
834
835**错误码:**
836
837| 错误码ID | 错误信息 |
838| ------- | -------------------------------- |
839| 201 | The application does not have permission to call the interface. |
840| 401 | Invalid input parameter. |
841| 16000001 | Input error. The specified ability name does not exist. |
842| 16000002 | Ability type error. The specified ability type is wrong. |
843| 16000004 | Visibility verification failed. |
844| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
845| 16000011 | Context does not exist.        |
846| 16200001 | Caller released. The caller has been released. |
847| 16000050 | Internal Error. |
848
849**示例:**
850
851  ```ts
852  let want = {
853    deviceId: '',
854    bundleName: 'com.extreme.test',
855    abilityName: 'MainAbility'
856  };
857
858  try {
859    this.context.stopServiceExtensionAbility(want)
860      .then((data) => {
861        // 执行正常业务
862        console.log('stopServiceExtensionAbility succeed');
863      })
864      .catch((error) => {
865        // 处理业务逻辑错误
866        console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
867          ' error.message: ' + JSON.stringify(error.message));
868      });
869  } catch (paramError) {
870    // 处理入参错误异常
871    console.log('error.code: ' + JSON.stringify(paramError.code) +
872      ' error.message: ' + JSON.stringify(paramError.message));
873  }
874  ```
875
876## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
877
878stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
879
880使用帐户停止同一应用程序内的服务(callback形式)。
881
882**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
883
884**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
885
886**系统API**: 此接口为系统接口,三方应用不支持调用。
887
888**参数:**
889
890| 参数名 | 类型 | 必填 | 说明 |
891| -------- | -------- | -------- | -------- |
892| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
893| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
894| callback | AsyncCallback\<void\> | 是 | 停止Ability的回调函数。 |
895
896**错误码:**
897
898| 错误码ID | 错误信息 |
899| ------- | -------------------------------- |
900| 201 | The application does not have permission to call the interface. |
901| 401 | Invalid input parameter. |
902| 16000001 | Input error. The specified ability name does not exist. |
903| 16000002 | Ability type error. The specified ability type is wrong. |
904| 16000004 | Visibility verification failed. |
905| 16000006 | Can not cross user operations. |
906| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
907| 16000011 | Context does not exist.        |
908| 16200001 | Caller released. The caller has been released. |
909| 16000050 | Internal Error. |
910
911**示例:**
912
913  ```ts
914  let want = {
915    deviceId: '',
916    bundleName: 'com.extreme.test',
917    abilityName: 'MainAbility'
918  };
919  let accountId = 100;
920
921  try {
922    this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
923      if (error.code) {
924        // 处理业务逻辑错误
925        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
926          ' error.message: ' + JSON.stringify(error.message));
927        return;
928      }
929      // 执行正常业务
930      console.log('stopServiceExtensionAbilityWithAccount succeed');
931    });
932  } catch (paramError) {
933    // 处理入参错误异常
934    console.log('error.code: ' + JSON.stringify(paramError.code) +
935      ' error.message: ' + JSON.stringify(paramError.message));
936  }
937  ```
938
939## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
940
941stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>;
942
943使用帐户停止同一应用程序内的服务(Promise形式)。
944
945**需要权限**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS
946
947**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
948
949**系统API**: 此接口为系统接口,三方应用不支持调用。
950
951**参数:**
952
953| 参数名 | 类型 | 必填 | 说明 |
954| -------- | -------- | -------- | -------- |
955| want | [Want](js-apis-application-want.md) | 是 | 停止Ability的want信息。 |
956| accountId | number | 是 | 需要停止的系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
957
958**返回值:**
959
960| 类型 | 说明 |
961| -------- | -------- |
962| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
963
964**错误码:**
965
966| 错误码ID | 错误信息 |
967| ------- | -------------------------------- |
968| 201 | The application does not have permission to call the interface. |
969| 401 | Invalid input parameter. |
970| 16000001 | Input error. The specified ability name does not exist. |
971| 16000002 | Ability type error. The specified ability type is wrong. |
972| 16000004 | Visibility verification failed. |
973| 16000006 | Can not cross user operations. |
974| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
975| 16000011 | Context does not exist.        |
976| 16200001 | Caller released. The caller has been released. |
977| 16000050 | Internal Error. |
978
979**示例:**
980
981  ```ts
982  let want = {
983    deviceId: '',
984    bundleName: 'com.extreme.test',
985    abilityName: 'MainAbility'
986  };
987  let accountId = 100;
988
989  try {
990    this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
991      .then((data) => {
992        // 执行正常业务
993        console.log('stopServiceExtensionAbilityWithAccount succeed');
994      })
995      .catch((error) => {
996        // 处理业务逻辑错误
997        console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
998          ' error.message: ' + JSON.stringify(error.message));
999      });
1000  } catch (paramError) {
1001    // 处理入参错误异常
1002    console.log('error.code: ' + JSON.stringify(paramError.code) +
1003      ' error.message: ' + JSON.stringify(paramError.message));
1004  }
1005  ```
1006
1007## ServiceExtensionContext.terminateSelf
1008
1009terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
1010
1011停止Ability自身。
1012
1013**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1014
1015**系统API**: 此接口为系统接口,三方应用不支持调用。
1016
1017**参数:**
1018
1019| 参数名 | 类型 | 必填 | 说明 |
1020| -------- | -------- | -------- | -------- |
1021| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 |
1022
1023**错误码:**
1024
1025| 错误码ID | 错误信息 |
1026| ------- | -------------------------------- |
1027| 201 | The application does not have permission to call the interface. |
1028| 401 | Invalid input parameter. |
1029| 16000001 | Input error. The specified ability name does not exist. |
1030| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
1031| 16000011 | Context does not exist.        |
1032| 16000050 | Internal Error. |
1033
1034**示例:**
1035
1036  ```ts
1037  this.context.terminateSelf((error) => {
1038    if (error.code) {
1039      // 处理业务逻辑错误
1040      console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1041        ' error.message: ' + JSON.stringify(error.message));
1042      return;
1043    }
1044    // 执行正常业务
1045    console.log('terminateSelf succeed');
1046  });
1047  ```
1048
1049## ServiceExtensionContext.terminateSelf
1050
1051terminateSelf(): Promise&lt;void&gt;;
1052
1053停止自身。通过Promise返回结果。
1054
1055**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1056
1057**系统API**: 此接口为系统接口,三方应用不支持调用。
1058
1059**返回值:**
1060
1061| 类型 | 说明 |
1062| -------- | -------- |
1063| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1064
1065**错误码:**
1066
1067| 错误码ID | 错误信息 |
1068| ------- | -------------------------------- |
1069| 201 | The application does not have permission to call the interface. |
1070| 401 | Invalid input parameter. |
1071| 16000001 | Input error. The specified ability name does not exist. |
1072| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
1073| 16000011 | Context does not exist.        |
1074| 16000050 | Internal Error. |
1075
1076**示例:**
1077
1078  ```ts
1079  this.context.terminateSelf().then((data) => {
1080    // 执行正常业务
1081    console.log('terminateSelf succeed');
1082  }).catch((error) => {
1083    // 处理业务逻辑错误
1084    console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
1085      ' error.message: ' + JSON.stringify(error.message));
1086  });
1087  ```
1088
1089## ServiceExtensionContext.connectServiceExtensionAbility
1090
1091connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
1092
1093将一个Ability与服务类型的Ability绑定。
1094
1095**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1096
1097**系统API**: 此接口为系统接口,三方应用不支持调用。
1098
1099**参数:**
1100
1101| 参数名 | 类型 | 必填 | 说明 |
1102| -------- | -------- | -------- | -------- |
1103| want | [Want](js-apis-application-want.md)  | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 |
1104| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 |
1105
1106**返回值:**
1107
1108| 类型 | 说明 |
1109| -------- | -------- |
1110| number | 返回一个number,后续根据这个number去断开连接。 |
1111
1112**错误码:**
1113
1114| 错误码ID | 错误信息 |
1115| ------- | -------------------------------- |
1116| 201 | The application does not have permission to call the interface. |
1117| 401 | Invalid input parameter. |
1118| 16000001 | Input error. The specified ability name does not exist. |
1119| 16000002 | Ability type error. The specified ability type is wrong. |
1120| 16000004 | Visibility verification failed. |
1121| 16000011 | Context does not exist.        |
1122| 16000050 | Internal Error. |
1123
1124**示例:**
1125
1126  ```ts
1127  let want = {
1128    bundleName: 'com.example.myapp',
1129    abilityName: 'MyAbility'
1130  };
1131  let options = {
1132    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
1133    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
1134    onFailed(code) { console.log('----------- onFailed -----------') }
1135  };
1136
1137  let connection = null;
1138  try {
1139    connection = this.context.connectServiceExtensionAbility(want, options);
1140  } catch (paramError) {
1141    // 处理入参错误异常
1142    console.log('error.code: ' + JSON.stringify(paramError.code) +
1143      ' error.message: ' + JSON.stringify(paramError.message));
1144  }
1145  ```
1146
1147## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
1148
1149connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
1150
1151使用AbilityInfo.AbilityType.SERVICE模板和account将当前能力连接到一个能力。
1152
1153**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1154
1155**系统API**: 此接口为系统接口,三方应用不支持调用。
1156
1157**参数:**
1158
1159| 参数名 | 类型 | 必填 | 说明 |
1160| -------- | -------- | -------- | -------- |
1161| want | [Want](js-apis-application-want.md) | 是 | 启动Ability的want信息。 |
1162| accountId | number | 是 | 系统帐号的帐号ID,详情参考[getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess)。 |
1163| options | ConnectOptions | 否 | 远端对象实例。 |
1164
1165**返回值:**
1166
1167| 类型 | 说明 |
1168| -------- | -------- |
1169| number | 返回Ability连接的结果code。 |
1170
1171**错误码:**
1172
1173| 错误码ID | 错误信息 |
1174| ------- | -------------------------------- |
1175| 201 | The application does not have permission to call the interface. |
1176| 401 | Invalid input parameter. |
1177| 16000001 | Input error. The specified ability name does not exist. |
1178| 16000002 | Ability type error. The specified ability type is wrong. |
1179| 16000004 | Visibility verification failed. |
1180| 16000006 | Can not cross user operations. |
1181| 16000011 | Context does not exist.        |
1182| 16000050 | Internal Error. |
1183
1184**示例:**
1185
1186  ```ts
1187  let want = {
1188    deviceId: '',
1189    bundleName: 'com.extreme.test',
1190    abilityName: 'MainAbility'
1191  };
1192  let accountId = 100;
1193  let options = {
1194    onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
1195    onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
1196    onFailed(code) { console.log('----------- onFailed -----------') }
1197  }
1198
1199  let connection = null;
1200  try {
1201    connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
1202  } catch (paramError) {
1203    // 处理入参错误异常
1204    console.log('error.code: ' + JSON.stringify(paramError.code) +
1205      ' error.message: ' + JSON.stringify(paramError.message));
1206  }
1207  ```
1208
1209## ServiceExtensionContext.disconnectServiceExtensionAbility
1210
1211disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
1212
1213将一个Ability与绑定的服务类型的Ability解绑。
1214
1215**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1216
1217**系统API**: 此接口为系统接口,三方应用不支持调用。
1218
1219**参数:**
1220
1221| 参数名 | 类型 | 必填 | 说明 |
1222| -------- | -------- | -------- | -------- |
1223| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1224| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 |
1225
1226**错误码:**
1227
1228| 错误码ID | 错误信息 |
1229| ------- | -------------------------------- |
1230| 201 | The application does not have permission to call the interface. |
1231| 401 | Invalid input parameter. |
1232| 16000001 | Input error. The specified ability name does not exist. |
1233| 16000003 | Input error. The specified id does not exist. |
1234| 16000011 | Context does not exist.        |
1235| 16000050 | Internal Error. |
1236
1237**示例:**
1238
1239  ```ts
1240  // connection为connectServiceExtensionAbility中的返回值
1241  let connection = 1;
1242
1243  try {
1244    this.context.disconnectServiceExtensionAbility(connection, (error) => {
1245      if (error.code) {
1246        // 处理业务逻辑错误
1247        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1248          ' error.message: ' + JSON.stringify(error.message));
1249        return;
1250      }
1251      // 执行正常业务
1252      console.log('disconnectServiceExtensionAbility succeed');
1253    });
1254  } catch (paramError) {
1255    // 处理入参错误异常
1256    console.log('error.code: ' + JSON.stringify(paramError.code) +
1257      ' error.message: ' + JSON.stringify(paramError.message));
1258  }
1259  ```
1260
1261## ServiceExtensionContext.disconnectServiceExtensionAbility
1262
1263disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
1264
1265将一个Ability与绑定的服务类型的Ability解绑。通过Promise返回结果。
1266
1267**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1268
1269**系统API**: 此接口为系统接口,三方应用不支持调用。
1270
1271**参数:**
1272
1273| 参数名 | 类型 | 必填 | 说明 |
1274| -------- | -------- | -------- | -------- |
1275| connection | number | 是 | 在connectServiceExtensionAbility中返回的number。 |
1276
1277**返回值:**
1278
1279| 类型 | 说明 |
1280| -------- | -------- |
1281| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 |
1282
1283**错误码:**
1284
1285| 错误码ID | 错误信息 |
1286| ------- | -------------------------------- |
1287| 201 | The application does not have permission to call the interface. |
1288| 401 | Invalid input parameter. |
1289| 16000001 | Input error. The specified ability name does not exist. |
1290| 16000003 | Input error. The specified id does not exist. |
1291| 16000011 | Context does not exist.        |
1292| 16000050 | Internal Error. |
1293
1294**示例:**
1295
1296  ```ts
1297  // connection为connectServiceExtensionAbility中的返回值
1298  let connection = 1;
1299
1300  try {
1301    this.context.disconnectServiceExtensionAbility(connection)
1302      .then((data) => {
1303        // 执行正常业务
1304        console.log('disconnectServiceExtensionAbility succeed');
1305      })
1306      .catch((error) => {
1307        // 处理业务逻辑错误
1308        console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
1309          ' error.message: ' + JSON.stringify(error.message));
1310      });
1311  } catch (paramError) {
1312    // 处理入参错误异常
1313    console.log('error.code: ' + JSON.stringify(paramError.code) +
1314      ' error.message: ' + JSON.stringify(paramError.message));
1315  }
1316  ```
1317
1318## ServiceExtensionContext.startAbilityByCall
1319
1320startAbilityByCall(want: Want): Promise&lt;Caller&gt;;
1321
1322启动指定Ability至前台或后台,同时获取其Caller通信接口,调用方可使用Caller与被启动的Ability进行通信。
1323
1324使用规则:
1325 - 调用方应用位于后台时,使用该接口启动Ability需申请`ohos.permission.START_ABILITIES_FROM_BACKGROUND`权限
1326 - 目标Ability的visible属性若配置为false,调用方应用需申请`ohos.permission.START_INVISIBLE_ABILITY`权限
1327 - 组件启动规则详见:[组件启动规则(Stage模型)](../../application-models/component-startup-rules.md)
1328
1329**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
1330
1331**系统API**:此接口为系统接口,三方应用不支持调用。
1332
1333**参数:**
1334
1335| 参数名 | 类型 | 必填 | 说明 |
1336| -------- | -------- | -------- | -------- |
1337| want | [Want](js-apis-application-want.md) | 是 | 传入需要启动的Ability的信息,包含abilityName、moduleName、bundleName、deviceId(可选)、parameters(可选),其中deviceId缺省或为空表示启动本地Ability,parameters缺省或为空表示后台启动Ability。 |
1338
1339**返回值:**
1340
1341| 类型 | 说明 |
1342| -------- | -------- |
1343| Promise&lt;Caller&gt; | 获取要通讯的caller对象。 |
1344
1345**错误码:**
1346
1347| 错误码ID | 错误信息 |
1348| ------- | -------------------------------- |
1349| 201 | The application does not have permission to call the interface. |
1350| 401 | Invalid input parameter. |
1351| 16000001 | Input error. The specified ability name does not exist. |
1352| 16000004 | Visibility verification failed. |
1353| 16000005 | Static permission denied. The specified process does not have the permission. |
1354| 16000007 | Service busyness. There are concurrent tasks, waiting for retry. |
1355| 16000008 | Crowdtest App Expiration. |
1356| 16000009 | Can not start ability in wukong mode. |
1357| 16000017 | The previous ability is starting, wait start later.        |
1358| 16000050 | Internal Error. |
1359
1360**示例:**
1361
1362  后台启动:
1363
1364  ```ts
1365  let caller = undefined;
1366
1367  // 后台启动Ability,不配置parameters
1368  let wantBackground = {
1369      bundleName: 'com.example.myservice',
1370      moduleName: 'entry',
1371      abilityName: 'MainAbility',
1372      deviceId: ''
1373  };
1374
1375  try {
1376    this.context.startAbilityByCall(wantBackground)
1377      .then((obj) => {
1378        // 执行正常业务
1379        caller = obj;
1380        console.log('startAbilityByCall succeed');
1381      }).catch((error) => {
1382        // 处理业务逻辑错误
1383        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1384          ' error.message: ' + JSON.stringify(error.message));
1385      });
1386  } catch (paramError) {
1387    // 处理入参错误异常
1388    console.log('error.code: ' + JSON.stringify(paramError.code) +
1389      ' error.message: ' + JSON.stringify(paramError.message));
1390  }
1391  ```
1392
1393  前台启动:
1394
1395  ```ts
1396  let caller = undefined;
1397
1398  // 前台启动Ability,将parameters中的'ohos.aafwk.param.callAbilityToForeground'配置为true
1399  let wantForeground = {
1400      bundleName: 'com.example.myservice',
1401      moduleName: 'entry',
1402      abilityName: 'MainAbility',
1403      deviceId: '',
1404      parameters: {
1405        'ohos.aafwk.param.callAbilityToForeground': true
1406      }
1407  };
1408
1409  try {
1410    this.context.startAbilityByCall(wantForeground)
1411      .then((obj) => {
1412        // 执行正常业务
1413        caller = obj;
1414        console.log('startAbilityByCall succeed');
1415      }).catch((error) => {
1416        // 处理业务逻辑错误
1417        console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
1418          ' error.message: ' + JSON.stringify(error.message));
1419      });
1420  } catch (paramError) {
1421    // 处理入参错误异常
1422    console.log('error.code: ' + JSON.stringify(paramError.code) +
1423      ' error.message: ' + JSON.stringify(paramError.message));
1424  }
1425  ```