• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Connecting to a ServiceAbility from the Stage Model
2
3
4This topic describes how the two application components of the stage model connect to the ServiceAbility component of the FA model.
5
6
7## UIAbility Accessing a ServiceAbility
8
9A UIAbility accesses a ServiceAbility in the same way as it accesses a ServiceExtensionAbility.
10
11
12```ts
13import UIAbility from '@ohos.app.ability.UIAbility';
14
15export default class EntryAbility extends UIAbility {
16    onCreate(want, launchParam) {
17        console.info("EntryAbility onCreate");
18    }
19    onDestroy() {
20        console.info("EntryAbility onDestroy")
21    }
22    onWindowStageCreate(windowStage) {
23        console.info("EntryAbility onWindowStageCreate")
24        let want = {
25            bundleName: "com.ohos.fa",
26            abilityName: "ServiceAbility",
27        };
28
29        let options = {
30            onConnect:function (elementName, proxy) {
31                console.info("onConnect called.");
32            },
33            onDisconnect:function (elementName) {
34                console.info("onDisconnect called.");
35            },
36            onFailed:function (code) {
37                console.info("onFailed code is: " + code);
38            }
39        };
40        let connectionId = this.context.connectServiceExtensionAbility(want, options);
41    }
42    onWindowStageDestroy() {
43        console.info("EntryAbility onWindowStageDestroy")
44    }
45    onForeground() {
46        console.info("EntryAbility onForeground")
47    }
48    onBackground() {
49        console.info("EntryAbility onBackground")
50    }
51}
52```
53
54
55## ExtensionAbility Accessing a ServiceAbility
56
57The following uses the ServiceExtensionAbility component as an example to describe how an ExtensionAbility accesses a ServiceAbility. A ServiceExtensionAbility accesses a ServiceAbility in the same way as it accesses another ServiceExtensionAbility.
58
59
60```ts
61import Extension from '@ohos.app.ability.ServiceExtensionAbility'
62
63export default class ServiceExtension extends Extension {
64    onCreate(want) {
65        console.info("ServiceExtension onCreate")
66    }
67    onDestroy() {
68        console.info("ServiceExtension onDestroy")
69    }
70    onRequest(want, startId) {
71        console.info("ServiceExtension onRequest")
72        let wantFA = {
73            bundleName: "com.ohos.fa",
74            abilityName: "ServiceAbility",
75        };
76        let options = {
77            onConnect:function (elementName, proxy) {
78                console.info("onConnect called.");
79            },
80            onDisconnect:function (elementName) {
81                console.info("onDisconnect called.");
82            },
83            onFailed:function (code) {
84                console.info("onFailed code is: " + code);
85            }
86        };
87        let connectionId = this.context.connectServiceExtensionAbility(wantFA, options);
88    }
89}
90```
91