• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 # @ohos.app.ability.ActionExtensionAbility (ExtensionAbility for Custom Actions)
2 
3 The **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 
12 The 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
17 import 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 
30 onCreate(): void
31 
32 Called to initialize the service logic when an ActionExtensionAbility is being created.
33 
34 **System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
35 
36 ## ActionExtensionAbility.onSessionCreate
37 
38 onSessionCreate(want: Want, session: UIExtensionContentSession): void
39 
40 Called 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 
53 onSessionDestroy(session: UIExtensionContentSession): void
54 
55 Called 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 
67 onForeground(): void;
68 
69 Called when this ActionExtensionAbility is switched from the background to the foreground.
70 
71 **System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
72 
73 ## ActionExtensionAbility.onBackground
74 
75 onBackground(): void;
76 
77 Called when this ActionExtensionAbility is switched from the foreground to the background.
78 
79 **System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
80 
81 ## ActionExtensionAbility.onDestroy
82 
83 onDestroy(): void | Promise<void>;
84 
85 Called when this ActionExtensionAbility is destroyed to clear resources.
86 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.
87 
88 **System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
89 
90 ## Creating an ActionExtensionAbility
91 
92 To manually create an ActionExtensionAbility in the DevEco Studio project, perform the following steps:
93 
94 1. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **ActionExtAbility**.
95 
96 2. In the **ActionExtAbility** directory, right-click and choose **New > ArkTS File** to create a file named **ActionExtAbility.ets**.
97 
98     ```text
99     ├── ets
100     │ ├── ActionExtAbility
101     │ │   ├── ActionExtAbility.ets
102 103     ```
104 
105 3. In the **ActionExtAbility.ets** 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    import Want from '@ohos.app.ability.Want';
110    import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
111 
112    const TAG: string = "[ActionExtAbility]";
113 
114    export default class ActionExtAbility extends ActionExtensionAbility {
115      storage: LocalStorage;
116      message: string;
117      onCreate() {
118        console.info(TAG, `onCreate`);
119      }
120 
121      onForeground() {
122        console.info(TAG, `ononForeground`);
123      }
124 
125      onBackground() {
126        console.info(TAG, `onBackground`);
127      }
128 
129      onSessionCreate(want: Want, session: UIExtensionContentSession) {
130        console.info(TAG, `onSessionCreate, want: ${want.abilityName}`);
131        this.message = want.parameters.shareMessages.toString();
132        let localStorageData: Record<string, UIExtensionContentSession | string> = {
133          'session': session,
134          'messages': this.message
135        };
136        this.storage = new LocalStorage(localStorageData);
137        session.loadContent('pages/Index', this.storage);
138      }
139 
140      onSessionDestroy(session: UIExtensionContentSession) {
141        console.info(TAG, `onSessionDestroy`);
142      }
143 
144      onDestroy() {
145        console.info(TAG, `onDestroy`);
146      }
147    }
148    ```
149 
150 4. 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.
151 
152    ```json
153    {
154      "module": {
155        ...
156        "extensionAbilities": [
157          {
158            "name": "ActionExtAbility",
159            "icon": "$media:icon",
160            "description": "action",
161            "type": "action",
162            "exported": true,
163            "srcEntry": "./ets/ActionExtAbility/ActionExtAbility.ets"
164          }
165        ]
166      }
167    }
168    ```
169