harmony 鸿蒙ConnectOptions

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

ConnectOptions

ConnectOptions can be used as an input parameter to receive status changes during the connection to a background service. For example, it is used as an input parameter of connectServiceExtensionAbility to connect to a ServiceExtensionAbility.

NOTE

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

Modules to Import

import common from '@ohos.app.ability.common';

Attributes

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Type Mandatory Description
onConnect7+ function Yes Callback invoked when a connection is set up.
onDisconnect7+ function Yes Callback invoked when a connection is interrupted.
onFailed7+ function Yes Callback invoked when a connection fails.

Example

  import common from '@ohos.app.ability.common';
  import Want from '@ohos.app.ability.Want';

  let want: Want = {
    bundleName: 'com.example.myapp',
    abilityName: 'MyAbility'
  };

  let connectOptions: common.ConnectOptions = {
    onConnect(elementName, remote) { 
        console.log('onConnect elementName: ${elementName}');
    },
    onDisconnect(elementName) { 
        console.log('onDisconnect elementName: ${elementName}');
    },
    onFailed(code) { 
        console.error('onFailed code: ${code}');
    }
  };

  let connection: number = this.context.connectAbility(want, connectOptions);

你可能感兴趣的鸿蒙文章

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  赞