harmony 鸿蒙Starting a ServiceAbility

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

Starting a ServiceAbility

A ServiceAbility is started in the same way other abilities. You can start a ServiceAbility by calling featureAbility.startAbility() in the PageAbility or calling particleAbility.startAbility() in another ServiceAbility. For details about the startup rules, see Component Startup Rules.

The following example shows how to use startAbility() to start the ServiceAbility whose bundleName is com.example.myapplication and abilityName is ServiceAbility in a PageAbility. When starting the ServiceAbility, concatenate the bundleName string before abilityName.

import featureAbility from '@ohos.ability.featureAbility'
import Want from '@ohos.app.ability.Want';

async function startServiceAbility() {
  try {
    console.info('Begin to start ability')
    let want: Want = {
      bundleName: "com.example.myapplication",
      abilityName: "com.example.myapplication.ServiceAbility"
    }
    await featureAbility.startAbility({want})
    console.info(`Start ability succeed`)
  } catch (error) {
    console.error('Start ability failed with ' + error)
  }
}

In the preceding code, startAbility() is used to start the ServiceAbility.

  • If the ServiceAbility is not running, the system calls onStart() to initialize the ServiceAbility, and then calls onCommand() on the ServiceAbility.

  • If the ServiceAbility is running, the system directly calls onCommand() on the ServiceAbility.

你可能感兴趣的鸿蒙文章

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  赞