harmony 鸿蒙创建ServiceAbility

  • 2022-12-13
  • 浏览 (627)

创建ServiceAbility

  1. 创建ServiceAbility。

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

     import Want from '@ohos.app.ability.Want';
     import rpc from "@ohos.rpc"
     
     class FirstServiceAbilityStub extends rpc.RemoteObject {
       constructor(des: string) {
         super(des);
       }
     }
     
     class ServiceAbility {
       onStart() {
         console.info('ServiceAbility onStart')
       }
       onStop() {
         console.info('ServiceAbility onStop')
       }
       onCommand(want: Want, startId: number) {
         console.info('ServiceAbility onCommand')
       }
       onConnect(want: Want) {
         console.info('ServiceAbility onConnect' + want)
         return new FirstServiceAbilityStub('test')
       }
       onDisconnect(want: Want) {
         console.info('ServiceAbility onDisconnect' + want)
       }
     }

     export default new ServiceAbility()
  1. 注册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
               }
             ]
           }
         }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙应用模型

harmony 鸿蒙使用显式Want启动应用组件

harmony 鸿蒙使用隐式Want打开网址

harmony 鸿蒙AbilityStage组件容器

harmony 鸿蒙访问DataAbility

harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙常见action与entities

harmony 鸿蒙API切换概述

harmony 鸿蒙app和deviceConfig的切换

0  赞