• 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'
11
12async function startServiceAbility() {
13  try {
14    console.info('Begin to start ability')
15    let param = {
16      want: {
17        bundleName: "com.example.myapplication",
18        abilityName: "com.example.myapplication.ServiceAbility"
19      }
20    }
21    await featureAbility.startAbility(param)
22    console.info(`Start ability succeed`)
23  } catch (error) {
24    console.error('Start ability failed with ' + error)
25  }
26}
27```
28
29
30In the preceding code, **startAbility()** is used to start the ServiceAbility.
31
32
33- If the ServiceAbility is not running, the system calls **onStart()** to initialize the ServiceAbility, and then calls **onCommand()** on the ServiceAbility.
34
35- If the ServiceAbility is running, the system directly calls **onCommand()** on the ServiceAbility.
36