• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.ActionExtensionAbility (ExtensionAbility for Custom Actions)
2
3The **ActionExtensionAbility** module, inherited from [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md), provides a custom action service template. An ActionExtensionAbility is used to view and process the content in a host application. For example, you can add a bookmark, translate the selected text into another language, or edit an image on the current page.
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 translation as an example. You must create a request initiator and then an ActionExtensionAbility. The request initiator sends the text to be translated to the ActionExtensionAbility. The ActionExtensionAbility translates the text and sends the translated text to the request initiator.
13
14## Modules to Import
15
16```ts
17import ActionExtensionAbility from '@ohos.app.ability.ActionExtensionAbility';
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## ActionExtensionAbility.onCreate
29
30onCreate(): void
31
32Called to initialize the service logic when an ActionExtensionAbility is being created.
33
34**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
35
36## ActionExtensionAbility.onSessionCreate
37
38onSessionCreate(want: Want, session: UIExtensionContentSession): void
39
40Called when a **UIExtensionContentSession** instance is created for this ActionExtensionAbility.
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 ActionExtensionAbility, including the ability name and bundle name.|
49| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ActionExtensionAbility.|
50
51## ActionExtensionAbility.onSessionDestroy
52
53onSessionDestroy(session: UIExtensionContentSession): void
54
55Called when a **UIExtensionContentSession** instance is destroyed for this ActionExtensionAbility.
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 ActionExtensionAbility.|
64
65## ActionExtensionAbility.onForeground
66
67onForeground(): void;
68
69Called when this ActionExtensionAbility is switched from the background to the foreground.
70
71**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
72
73## ActionExtensionAbility.onBackground
74
75onBackground(): void;
76
77Called when this ActionExtensionAbility is switched from the foreground to the background.
78
79**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
80
81## ActionExtensionAbility.onDestroy
82
83onDestroy(): void | Promise<void>;
84
85Called when this ActionExtensionAbility 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 an ActionExtensionAbility
91
92To manually create an ActionExtensionAbility 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 **ActionExtAbility**.
95
962. In the **ActionExtAbility** directory, right-click and choose **New > TypeScript File** to create a file named **ActionExtAbility.ts**.
97
98    ```
99    ├── ets
100    │ ├── ActionExtAbility
101    │ │   ├── ActionExtAbility.ts
102103    ```
104
1053. In the **ActionExtAbility.ts** file, import the ActionExtensionAbility module. Customize a class that inherits from ActionExtensionAbility and implement the lifecycle callbacks.
106
107   ```ts
108   import ActionExtensionAbility from '@ohos.app.ability.ActionExtensionAbility';
109   const TAG: string = "[ActionExtAbility]";
110
111   export default class ActionExtAbility extends ActionExtensionAbility {
112     storage: LocalStorage;
113     message: string;
114     onCreate() {
115       console.info(TAG, `onCreate`);
116     }
117
118     onForeground() {
119       console.info(TAG, `ononForeground`);
120     }
121
122     onBackground() {
123       console.info(TAG, `onBackground`);
124     }
125
126     onSessionCreate(want, session) {
127       console.info(TAG, `onSessionCreate, want: ${want.abilityName}`);
128       this.message = want.parameters.shareMessages;
129       this.storage = new LocalStorage(
130        {
131          'session': session,
132          'messages': this.message
133        });
134       session.loadContent('pages/Index', this.storage);
135     }
136
137     onSessionDestroy(session) {
138       console.info(TAG, `onSessionDestroy`);
139     }
140
141     onDestroy() {
142       console.info(TAG, `onDestroy`);
143     }
144   }
145   ```
146
1474. Register the ActionExtensionAbility in the [**module.json5** file](../../quick-start/module-configuration-file.md) of the module in the project. Set **type** to **action** and **srcEntry** to the code path of the ActionExtensionAbility component.
148
149   ```json
150   {
151     "module": {
152       ...
153       "extensionAbilities": [
154         {
155           "name": "ActionExtAbility",
156           "icon": "$media:icon",
157           "description": "action",
158           "type": "action",
159           "exported": true,
160           "srcEntry": "./ets/ActionExtAbility/ActionExtAbility.ts"
161         }
162       ]
163     }
164   }
165   ```
166