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';
import promptAction from '@ohos.promptAction';
import hilog from '@ohos.hilog';
const LOG_DOMAIN: number = 0x0000;
const LOG_TAG: string = 'startServiceAbility';
const startServiceAbility = async () => {
  try {
    hilog.info(LOG_DOMAIN, LOG_TAG, 'Begin to start ability');
    let want: Want = {
      bundleName: 'com.samples.famodelabilitydevelop',
      abilityName: 'com.samples.famodelabilitydevelop.ServiceAbility'
    };
    await featureAbility.startAbility({ want });
    promptAction.showToast({
      message: 'start service success'
    });
    hilog.info(LOG_DOMAIN, LOG_TAG, 'Start ability succeed');
  } catch (error) {
    hilog.info(LOG_DOMAIN, LOG_TAG, '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.