harmony 鸿蒙Creating a ServiceAbility

  • 2023-02-03
  • 浏览 (607)

Creating a ServiceAbility

  1. Create a ServiceAbility.

    Override the ServiceAbility lifecycle callbacks to implement your own logic for processing interaction requests.

     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. Register the ServiceAbility.

    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.

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

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞