• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.ShareExtensionAbility (ExtensionAbility for Sharing)
2
3The **ShareExtensionAbility** module, inherited from [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md), 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.
4
5> **NOTE**
6>
7> 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.
8> The APIs of this module can be used only in the stage model.
9
10## When to Use
11
12The 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.
13
14## Modules to Import
15
16```ts
17import ShareExtensionAbility from '@ohos.app.ability.ShareExtensionAbility';
18```
19
20## Attributes
21
22**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
23
24| Name| Type| Readable| Writable| Description|
25| -------- | -------- | -------- | -------- | -------- |
26| context | [UIExtensionContext](js-apis-inner-application-uiExtensionContext.md) | Yes| No| Context.|
27
28## ShareExtensionAbility.onCreate
29
30onCreate(): void
31
32Called to initialize the service logic when a ShareExtensionAbility is being created.
33
34**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
35
36## ShareExtensionAbility.onSessionCreate
37
38onSessionCreate(want: Want, session: UIExtensionContentSession): void
39
40Called when a **UIExtensionContentSession** instance is created for this ShareExtensionAbility.
41
42**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
43
44**Parameters**
45
46| Name| Type| Mandatory| Description|
47| -------- | -------- | -------- | -------- |
48| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to the ShareExtensionAbility, including the ability name and bundle name.|
49| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ShareExtensionAbility.|
50
51## ShareExtensionAbility.onSessionDestroy
52
53onSessionDestroy(session: UIExtensionContentSession): void
54
55Called when a **UIExtensionContentSession** instance is destroyed for this ShareExtensionAbility.
56
57**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
58
59**Parameters**
60
61| Name| Type| Mandatory| Description|
62| -------- | -------- | -------- | -------- |
63| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ShareExtensionAbility.|
64
65## ShareExtensionAbility.onForeground
66
67onForeground(): void;
68
69Called when this ShareExtensionAbility is switched from the background to the foreground.
70
71**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
72
73## ShareExtensionAbility.onBackground
74
75onBackground(): void;
76
77Called when this ShareExtensionAbility is switched from the foreground to the background.
78
79**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
80
81## ShareExtensionAbility.onDestroy
82
83onDestroy(): void | Promise<void>;
84
85Called when this ShareExtensionAbility is destroyed to clear resources.
86After 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.
87
88**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
89
90## Creating a ShareExtensionAbility
91
92To manually create a ShareExtensionAbility in the DevEco Studio project, perform the following steps:
93
941. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **ShareExtAbility**.
95
962. In the **ShareExtAbility** directory, right-click and choose **New > ArkTS File** to create a file named **ShareExtAbility.ets**.
97
98    ```text
99    ├── ets
100    │ ├── ShareExtAbility
101    │ │   ├── ShareExtAbility.ets
102103    ```
104
1053. In the **ShareExtAbility.ets** file, import the ShareExtensionAbility module. Customize a class that inherits from **ShareExtensionAbility** and implement the lifecycle callbacks.
106
107  ```ts
108  import ShareExtensionAbility from '@ohos.app.ability.ShareExtensionAbility';
109  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
110  import Want from '@ohos.app.ability.Want';
111
112  const TAG: string = "[ShareExtAbility]";
113
114  export default class ShareExtAbility extends ShareExtensionAbility {
115    onCreate() {
116      console.info(TAG, `onCreate`);
117    }
118
119    onForeground() {
120      console.info(TAG, `ononForeground`);
121    }
122
123    onBackground() {
124      console.info(TAG, `onBackground`);
125    }
126
127    onSessionCreate(want: Want, session: UIExtensionContentSession) {
128      console.info(TAG, `onSessionCreate, want: ${want.abilityName}`);
129      if (want.parameters) {
130        let obj: Record<string, UIExtensionContentSession | object> = {
131          'session': session,
132          'messages': want.parameters.shareMessages
133        }
134        let storage: LocalStorage = new LocalStorage(obj);
135        session.loadContent('pages/Index', storage);
136        session.loadContent('pages/Index', storage);
137      }
138    }
139
140    onSessionDestroy() {
141      console.info(TAG, `onSessionDestroy`);
142    }
143
144    onDestroy() {
145      console.info(TAG, `onDestroy`);
146    }
147  }
148  ```
149
1504. Register the ShareExtensionAbility in the [**module.json5** file](../../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **share** and **srcEntry** to the code path of the ShareExtensionAbility component.
151
152   ```json
153   {
154     "module": {
155       ...
156       "extensionAbilities": [
157         {
158           "name": "ShareExtAbility",
159           "icon": "$media:icon",
160           "description": "share",
161           "type": "share",
162           "exported": true,
163           "srcEntry": "./ets/ShareExtAbility/ShareExtAbility.ets"
164         }
165       ]
166     }
167   }
168   ```
169