• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Starting a ServiceAbility
2
3
4A 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](component-startup-rules.md).
5
6
7The 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**.
8
9```ts
10import featureAbility from '@ohos.ability.featureAbility'
11import Want from '@ohos.app.ability.Want';
12
13async function startServiceAbility() {
14  try {
15    console.info('Begin to start ability')
16    let want: Want = {
17      bundleName: "com.example.myapplication",
18      abilityName: "com.example.myapplication.ServiceAbility"
19    }
20    await featureAbility.startAbility({want})
21    console.info(`Start ability succeed`)
22  } catch (error) {
23    console.error('Start ability failed with ' + error)
24  }
25}
26```
27
28
29In the preceding code, [startAbility()](../reference/apis/js-apis-inner-application-uiAbilityContext.md#uiabilitycontextstartability) is used to start the ServiceAbility.
30
31
32- If the ServiceAbility is not running, the system calls **onStart()** to initialize the ServiceAbility, and then calls **onCommand()** on the ServiceAbility.
33
34- If the ServiceAbility is running, the system directly calls **onCommand()** on the ServiceAbility.
35