• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Creating a ServiceAbility
2
3
41. Create a ServiceAbility.
5
6     Override the ServiceAbility lifecycle callbacks to implement your own logic for processing interaction requests.
7
8   ```ts
9     import rpc from "@ohos.rpc"
10
11     class FirstServiceAbilityStub extends rpc.RemoteObject {
12       constructor(des: any) {
13         if (typeof des === 'string') {
14           super(des)
15         } else {
16           return
17         }
18       }
19     }
20
21     export default {
22       onStart() {
23         console.info('ServiceAbility onStart')
24       },
25       onStop() {
26         console.info('ServiceAbility onStop')
27       },
28       onCommand(want, startId) {
29         console.info('ServiceAbility onCommand')
30       },
31       onConnect(want) {
32         console.info('ServiceAbility onConnect' + want)
33         return new FirstServiceAbilityStub('test')
34       },
35       onDisconnect(want) {
36         console.info('ServiceAbility onDisconnect' + want)
37       }
38     }
39   ```
40
412. Register the ServiceAbility.
42
43    Declare the ServiceAbility in the **config.json** file by setting its **type** attribute to **service**. The **visible** attribute specifies whether the ServiceAbility can be called by other applications. The value **true** means that the ServiceAbility can be called by other applications, and **false** means that the ServiceAbility can be called only within the application. To enable the ServiceAbility to be called by other applications, set **visible** to **true** when registering the ServiceAbility and enable associated startup. For details about the startup rules, see [Component Startup Rules](component-startup-rules.md).
44
45    ```json
46         {
47           "module": {
48             "abilities": [
49               {
50                 "name": ".ServiceAbility",
51                 "srcLanguage": "ets",
52                 "srcPath": "ServiceAbility",
53                 "icon": "$media:icon",
54                 "description": "hap sample empty service",
55                 "type": "service",
56                 "visible": true
57               }
58             ]
59           }
60         }
61    ```
62