create-serviceability.md 1.8 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
# 创建ServiceAbility


1. 创建ServiceAbility。
重写ServiceAbility的生命周期方法,添加其他Ability请求与ServiceAbility交互时的处理方法。

   ```ts
   import rpc from "@ohos.rpc"

   class FirstServiceAbilityStub extends rpc.RemoteObject {
     constructor(des: any) {
       if (typeof des === 'string') {
         super(des)
       } else {
         return
       }
     }
   }

   export default {
     onStart() {
       console.info('ServiceAbility onStart')
     },
     onStop() {
       console.info('ServiceAbility onStop')
     },
     onCommand(want, startId) {
       console.info('ServiceAbility onCommand')
     },
     onConnect(want) {
       console.info('ServiceAbility onConnect' + want)
       return new FirstServiceAbilityStub('test')
     },
     onDisconnect(want) {
       console.info('ServiceAbility onDisconnect' + want)
     }
   }
   ```

2. 注册ServiceAbility。
  ServiceAbility需要在应用配置文件config.json中进行注册,注册类型type需要设置为service。"visible"属性表示ServiceAbility是否可以被其他应用调用,true表示可以被其他应用调用,false表示不能被其他应用调用(仅应用内可以调用)。若ServiceAbility需要被其他应用调用,注册ServiceAbility时需要设置"visible"为true,同时需要设置支持关联启动。ServiceAbility的启动规则详见[组件启动规则](component-startup-rules.md)章节。

  ```json
     {
       "module": {
         "abilities": [
           {
             "name": ".ServiceAbility",
             "srcLanguage": "ets",
             "srcPath": "ServiceAbility",
             "icon": "$media:icon",
             "description": "hap sample empty service",
             "type": "service",
             "visible": true
           }
         ]
       }
     }
  ```