创建ServiceAbility
-
创建ServiceAbility。
重写ServiceAbility的生命周期方法,添加其他Ability请求与ServiceAbility交互时的处理方法。
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) } }
-
注册ServiceAbility。
ServiceAbility需要在应用配置文件config.json中进行注册,注册类型type需要设置为service。"visible"属性表示ServiceAbility是否可以被其他应用调用,true表示可以被其他应用调用,false表示不能被其他应用调用(仅应用内可以调用)。若ServiceAbility需要被其他应用调用,注册ServiceAbility时需要设置"visible"为true,同时需要设置支持关联启动。ServiceAbility的启动规则详见组件启动规则章节。
{ "module": { "abilities": [ { "name": ".ServiceAbility", "srcLanguage": "ets", "srcPath": "ServiceAbility", "icon": "$media:icon", "description": "hap sample empty service", "type": "service", "visible": true } ] } }