harmony 鸿蒙ServiceAbility Switching

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

ServiceAbility Switching

The ServiceAbility component in the FA model corresponds to the ServiceExtensionAbility component in the stage model. The ServiceExtensionAbility class provides system APIs. Only system applications can create ServiceExtensionAbility instances. Therefore, ServiceAbility switching adopts different policies for system applications and third-party applications.

Switching a ServiceAbility for a System Application

The procedure for switching a ServiceAbility for a system application is similar to the procedure of PageAbility switching.

  1. Create a ServiceExtensionAbility in the stage model.

  2. Migrate the ServiceAbility code to the ServiceExtensionAbility.

The table below describes the lifecycle comparison of the ServiceAbility and ServiceExtensionAbility.

|ServiceAbility|ServiceExtensionAbility|Comparison Description| |——–|——–|——–| |onStart(): void|onCreate(want: Want): void|The two methods have the same invoking time. In the stage model, the want parameter is added so that you can obtain parameters during creation.| |onCommand(want: Want, startId: number): void|onRequest(want: Want, startId: number): void|The two methods have the same meaning, invoking time, and parameters.| |onConnect(want: Want): rpc.RemoteObject|onConnect(want: Want): rpc.RemoteObject|The two methods have the same meaning, invoking time, and parameters.| |onDisconnect(want: Want): void|onDisconnect(want: Want): void|The two methods have the same meaning, invoking time, and parameters.| |onReconnect(want: Want): void|onReconnect(want: Want): void|The two methods have the same meaning, invoking time, and parameters.| |onStop(): void|onDestroy(): void|The two methods have the same meaning, invoking time, and parameters.|

Switching a ServiceAbility for a Third-Party Application

In the stage model, third-party applications cannot provide services for other third-party applications. You can select a switching solution based on your service requirements.

Service Type Switching Solution
Providing services for other third-party applications Match a scenario-specific ExtensionAbility.
In-application: providing public use when it is running in the foreground Extract the component code as a common module for other components to use.
In-application: continuing running when it switches to the background Switch the service to a background service.

你可能感兴趣的鸿蒙文章

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  赞