harmony 鸿蒙@ohos.app.ability.ServiceExtensionAbility (ServiceExtensionAbility)

  • 2022-12-22
  • 浏览 (708)

@ohos.app.ability.ServiceExtensionAbility (ServiceExtensionAbility)

The ServiceExtensionAbility module provides lifecycle callbacks when a ServiceExtensionAbility (background service) is created, destroyed, connected, or disconnected.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

The APIs of this module can be used only in the stage model.

Modules to Import

import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';

Required Permissions

None.

Attributes

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Name Type Readable Writable Description
context ServiceExtensionContext Yes No ServiceExtensionContext, which is inherited from ExtensionContext.

ServiceExtensionAbility.onCreate

onCreate(want: Want): void;

Called to initialize the service logic when a ServiceExtensionAbility is being created.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
want Want Yes Want information related to this ServiceExtensionAbility, including the ability name and bundle name.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import Want from '@ohos.app.ability.Want';

  class ServiceExt extends ServiceExtension {
    onCreate(want: Want) {
      console.log('onCreate, want: ${want.abilityName}');
    }
  }

ServiceExtensionAbility.onDestroy

onDestroy(): void;

Called to clear resources when this ServiceExtensionAbility is being destroyed.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';

  class ServiceExt extends ServiceExtension {
    onDestroy() {
      console.log('onDestroy');
    }
  }

ServiceExtensionAbility.onRequest

onRequest(want: Want, startId: number): void;

Called following onCreate() when a ServiceExtensionAbility is started by calling startAbility() or startServiceExtensionAbility(). The value of startId is incremented for each ServiceExtensionAbility that is started.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
want Want Yes Want information related to this ServiceExtensionAbility, including the ability name and bundle name.
startId number Yes Number of ServiceExtensionAbility start times. The initial value is 1, and the value is automatically incremented for each ServiceExtensionAbility started.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import Want from '@ohos.app.ability.Want';

  class ServiceExt extends ServiceExtension {
    onRequest(want: Want, startId: number) {
      console.log('onRequest, want: ${want.abilityName}');
    }
  }

ServiceExtensionAbility.onConnect

onConnect(want: Want): rpc.RemoteObject|Promise;

Called following onCreate() when a ServiceExtensionAbility is started by calling connectAbility(). A RemoteObject object is returned for communication between the server and client.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
want Want Yes Want information related to this ServiceExtensionAbility, including the ability name and bundle name.

Return value

Type Description
rpc.RemoteObject A RemoteObject object used for communication between the server and client.

Example

  import rpc from '@ohos.rpc';
  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import Want from '@ohos.app.ability.Want';

  class StubTest extends rpc.RemoteObject{
      constructor(des: string) {
          super(des);
      }
      onConnect(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
      }
  }
  class ServiceExt extends ServiceExtension {
    onConnect(want: Want) {
      console.log('onConnect , want: ${want.abilityName}');
      return new StubTest('test');
    }
  }

If the returned RemoteObject object depends on an asynchronous API, you can use the asynchronous lifecycle.

import rpc from '@ohos.rpc';
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';

class StubTest extends rpc.RemoteObject{
    constructor(des: string) {
        super(des);
    }
    onConnect(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption) {
    }
}
async function getDescriptor() {
    // Call the asynchronous function.
    return "asyncTest"
}
class ServiceExt extends ServiceExtension {
  async onConnect(want: Want) {
    console.log(`onConnect , want: ${want.abilityName}`);
    let descriptor = await getDescriptor();
    return new StubTest(descriptor);
  }
}

ServiceExtensionAbility.onDisconnect

onDisconnect(want: Want): void|Promise<void>;

Called when a client is disconnected from this ServiceExtensionAbility.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
want Want Yes Want information related to this ServiceExtensionAbility, including the ability name and bundle name.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import Want from '@ohos.app.ability.Want';

  class ServiceExt extends ServiceExtension {
    onDisconnect(want: Want) {
      console.log('onDisconnect, want: ${want.abilityName}');
    }
  }

After the onDisconnect() lifecycle callback is executed, the application may exit. Consequently, the asynchronous function (for example, asynchronously writing data to the database) in onDisconnect() may fail to be executed. The asynchronous lifecycle can be used to ensure that the subsequent lifecycle continues after the asynchronous onDisconnect() is complete.

import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';

class ServiceExt extends ServiceExtension {
  async onDisconnect(want: Want) {
    console.log('onDisconnect, want: ${want.abilityName}');
    // Call the asynchronous function.
  }
}

ServiceExtensionAbility.onReconnect

onReconnect(want: Want): void;

Called when a new client attempts to connect to this ServiceExtensionAbility after all previous clients are disconnected. This capability is reserved.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
want Want Yes Want information related to this ServiceExtensionAbility, including the ability name and bundle name.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import Want from '@ohos.app.ability.Want';

  class ServiceExt extends ServiceExtension {
    onReconnect(want: Want) {
      console.log('onReconnect, want: ${want.abilityName}');
    }
  }

ServiceExtensionAbility.onConfigurationUpdate

onConfigurationUpdate(newConfig: Configuration): void;

Called when the configuration of this ServiceExtensionAbility is updated.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
newConfig Configuration Yes New configuration.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
  import { Configuration } from '@ohos.app.ability.Configuration';

  class ServiceExt extends ServiceExtension {
      onConfigurationUpdate(config: Configuration) {
          console.log(`onConfigurationUpdate, config: ${JSON.stringify(config)}`);
      }
  }

ServiceExtensionAbility.onDump

onDump(params: Array<string>): Array<string>;

Dumps the client information.

System capability: SystemCapability.Ability.AbilityRuntime.Core

System API: This is a system API and cannot be called by third-party applications.

Parameters

Name Type Mandatory Description
params Array<string> Yes Parameters in the form of a command.

Example

  import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';

  class ServiceExt extends ServiceExtension {
      onDump(params: Array<string>) {
          console.log(`dump, params: ${JSON.stringify(params)}`);
          return ['params'];
      }
  }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞