• 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 Want from '@ohos.app.ability.Want';
10     import rpc from "@ohos.rpc"
11
12     class FirstServiceAbilityStub extends rpc.RemoteObject {
13       constructor(des: string) {
14         super(des);
15       }
16     }
17
18     class ServiceAbility {
19       onStart() {
20         console.info('ServiceAbility onStart')
21       }
22       onStop() {
23         console.info('ServiceAbility onStop')
24       }
25       onCommand(want: Want, startId: number) {
26         console.info('ServiceAbility onCommand')
27       }
28       onConnect(want: Want) {
29         console.info('ServiceAbility onConnect' + want)
30         return new FirstServiceAbilityStub('test')
31       }
32       onDisconnect(want: Want) {
33         console.info('ServiceAbility onDisconnect' + want)
34       }
35     }
36
37     export default new ServiceAbility()
38   ```
39
402. Register the ServiceAbility.
41
42    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).
43
44    ```json
45         {
46           "module": {
47             "abilities": [
48               {
49                 "name": ".ServiceAbility",
50                 "srcLanguage": "ets",
51                 "srcPath": "ServiceAbility",
52                 "icon": "$media:icon",
53                 "description": "hap sample empty service",
54                 "type": "service",
55                 "visible": true
56               }
57             ]
58           }
59         }
60    ```
61