harmony 鸿蒙@ohos.app.ability.ShareExtensionAbility (ExtensionAbility for Sharing)

  • 2023-10-30
  • 浏览 (490)

@ohos.app.ability.ShareExtensionAbility (ExtensionAbility for Sharing)

The ShareExtensionAbility module, inherited from UIExtensionAbility, provides a share service template. The ShareExtensionAbility provides a convenient way for people to share current contextual information through applications, social media accounts, and other services.

NOTE

The initial APIs of this module are supported since API version 10. 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.

When to Use

The following uses text sharing as an example. A user selects a piece of text and starts the application to be shared. The application starts the sharing template based on the sharing information and displays the data based on the sharing template content.

Modules to Import

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

Attributes

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Name Type Readable Writable Description
context UIExtensionContext Yes No Context.

ShareExtensionAbility.onCreate

onCreate(): void

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

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

ShareExtensionAbility.onSessionCreate

onSessionCreate(want: Want, session: UIExtensionContentSession): void

Called when a UIExtensionContentSession instance is created for this ShareExtensionAbility.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
want Want Yes Want information related to the ShareExtensionAbility, including the ability name and bundle name.
session UIExtensionContentSession Yes UI content information related to the ShareExtensionAbility.

ShareExtensionAbility.onSessionDestroy

onSessionDestroy(session: UIExtensionContentSession): void

Called when a UIExtensionContentSession instance is destroyed for this ShareExtensionAbility.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Parameters

Name Type Mandatory Description
session UIExtensionContentSession Yes UI content information related to the ShareExtensionAbility.

ShareExtensionAbility.onForeground

onForeground(): void;

Called when this ShareExtensionAbility is switched from the background to the foreground.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

ShareExtensionAbility.onBackground

onBackground(): void;

Called when this ShareExtensionAbility is switched from the foreground to the background.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

ShareExtensionAbility.onDestroy

onDestroy(): void|Promise<void>;

Called when this ShareExtensionAbility is destroyed to clear resources. After the onDestroy() lifecycle callback is executed, the application may exit. Consequently, the asynchronous function (for example, asynchronously writing data to the database) in onDestroy() may fail to be executed. You can use the asynchronous lifecycle to ensure that the subsequent lifecycle continues only after the asynchronous function in onDestroy() finishes the execution.

System capability: SystemCapability.Ability.AbilityRuntime.AbilityCore

Creating a ShareExtensionAbility

To manually create a ShareExtensionAbility in the DevEco Studio project, perform the following steps:

  1. In the ets directory of a module in the project, right-click and choose New > Directory to create a directory named ShareExtAbility.

  2. In the ShareExtAbility directory, right-click and choose New > TypeScript File to create a file named ShareExtAbility.ts.

    ├── ets
    │ ├── ShareExtAbility
    │ │   ├── ShareExtAbility.ts
    └
    
  3. In the ShareExtAbility.ts file, import the ShareExtensionAbility module. Customize a class that inherits from ShareExtensionAbility and implement the lifecycle callbacks.

  import ShareExtensionAbility from '@ohos.app.ability.ShareExtensionAbility';
  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
  import Want from '@ohos.app.ability.Want';

  const TAG: string = "[ShareExtAbility]";

  export default class ShareExtAbility extends ShareExtensionAbility {
    onCreate() {
      console.info(TAG, `onCreate`);
    }

    onForeground() {
      console.info(TAG, `ononForeground`);
    }

    onBackground() {
      console.info(TAG, `onBackground`);
    }

    onSessionCreate(want: Want, session: UIExtensionContentSession) {
      console.info(TAG, `onSessionCreate, want: ${want.abilityName}`);
      if (want.parameters) {
        let obj: Record<string, UIExtensionContentSession|object> = {
          'session': session,
          'messages': want.parameters.shareMessages
        }
        let storage: LocalStorage = new LocalStorage(obj);
        session.loadContent('pages/Index', storage);
        session.loadContent('pages/Index', storage);
      }
    }

    onSessionDestroy() {
      console.info(TAG, `onSessionDestroy`);
    }

    onDestroy() {
      console.info(TAG, `onDestroy`);
    }
  }
  1. Register the ShareExtensionAbility in the module.json5 file of the module in the project. Set type to share and srcEntry to the code path of the ShareExtensionAbility component.
   {
     "module": {
       ...
       "extensionAbilities": [
         {
           "name": "ShareExtAbility",
           "icon": "$media:icon",
           "description": "share",
           "type": "share",
           "exported": true,
           "srcEntry": "./ets/ShareExtAbility/ShareExtAbility.ts"
         }
       ]
     }
   }

你可能感兴趣的鸿蒙文章

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  赞