1e41f4b71Sopenharmony_ci# @ohos.app.ability.ActionExtensionAbility (ExtensionAbility for Custom Actions) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **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. For details about the inheritance relationship of each ability, see [Inheritance Relationship](./js-apis-app-ability-ability.md#ability-inheritance-relationship). 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## When to Use 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciThe 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. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Modules to Import 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ciimport { ActionExtensionAbility } from '@kit.AbilityKit'; 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## Properties 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name| Type| Read-only| Mandatory| Description| 26e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 27e41f4b71Sopenharmony_ci| context | [UIExtensionContext](js-apis-inner-application-uiExtensionContext.md) | No| No| Context.| 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci## ActionExtensionAbility.onCreate 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_cionCreate(): void 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciCalled to initialize the service logic when an ActionExtensionAbility is being created. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Example** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci## ActionExtensionAbility.onSessionCreate 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_cionSessionCreate(want: Want, session: UIExtensionContentSession): void 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ciCalled when a **UIExtensionContentSession** instance is created for this ActionExtensionAbility. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Parameters** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 52e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 53e41f4b71Sopenharmony_ci| want | [Want](js-apis-app-ability-want.md) | Yes| Want information related to the ActionExtensionAbility, including the ability name and bundle name.| 54e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ActionExtensionAbility.| 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**Example** 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci## ActionExtensionAbility.onForeground 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_cionForeground(): void 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciCalled when this ActionExtensionAbility is switched from the background to the foreground. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci**Example** 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci## ActionExtensionAbility.onBackground 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_cionBackground(): void 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciCalled when this ActionExtensionAbility is switched from the foreground to the background. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**Example** 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci## ActionExtensionAbility.onSessionDestroy 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_cionSessionDestroy(session: UIExtensionContentSession): void 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciCalled when a **UIExtensionContentSession** instance is destroyed for this ActionExtensionAbility. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Parameters** 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 95e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 96e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the ActionExtensionAbility.| 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**Example** 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci## ActionExtensionAbility.onDestroy 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_cionDestroy(): void | Promise<void> 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ciCalled when this ActionExtensionAbility is destroyed to clear resources. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciAfter 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. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**Returns** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci| Type | Description | 115e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------- | 116e41f4b71Sopenharmony_ci| void \| Promise<void> | No return value or a Promise object that returns no result.| 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Example** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciSee [Creating an ActionExtensionAbility](#creating-an-actionextensionability). 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci## Creating an ActionExtensionAbility 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciTo manually create an ActionExtensionAbility in the DevEco Studio project, perform the following steps: 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci1. In the **ets** directory of a module in the project, right-click and choose **New > Directory** to create a directory named **ActionExtAbility**. 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci2. In the **actionextability** directory, right-click and choose **New > ArkTS File** to create a file named **ActionExtAbility.ets**. 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci ```text 131e41f4b71Sopenharmony_ci ├── ets 132e41f4b71Sopenharmony_ci │ ├── actionextability 133e41f4b71Sopenharmony_ci │ │ ├── ActionExtAbility.ets 134e41f4b71Sopenharmony_ci └ 135e41f4b71Sopenharmony_ci ``` 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci3. In the **ActionExtAbility.ets** file, import the ActionExtensionAbility module. Customize a class that inherits from ActionExtensionAbility and implement the lifecycle callbacks. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci ```ts 140e41f4b71Sopenharmony_ci import { ActionExtensionAbility, Want, UIExtensionContentSession } from '@kit.AbilityKit'; 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci const TAG: string = "[ActionExtAbility]"; 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci export default class ActionExtAbility extends ActionExtensionAbility { 145e41f4b71Sopenharmony_ci onCreate() { 146e41f4b71Sopenharmony_ci console.info(TAG, `onCreate`); 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 149e41f4b71Sopenharmony_ci console.info(TAG, `onSessionCreate, want: ${want.abilityName}`); 150e41f4b71Sopenharmony_ci if (want.parameters) { 151e41f4b71Sopenharmony_ci let obj: Record<string, UIExtensionContentSession | object> = { 152e41f4b71Sopenharmony_ci 'session': session, 153e41f4b71Sopenharmony_ci 'messages': want.parameters.shareMessages 154e41f4b71Sopenharmony_ci }; 155e41f4b71Sopenharmony_ci let storage: LocalStorage = new LocalStorage(obj); 156e41f4b71Sopenharmony_ci session.loadContent('pages/Index', storage); 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci } 159e41f4b71Sopenharmony_ci onForeground() { 160e41f4b71Sopenharmony_ci console.info(TAG, `ononForeground`); 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci onBackground() { 163e41f4b71Sopenharmony_ci console.info(TAG, `onBackground`); 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 166e41f4b71Sopenharmony_ci console.info(TAG, `onSessionDestroy`); 167e41f4b71Sopenharmony_ci } 168e41f4b71Sopenharmony_ci onDestroy() { 169e41f4b71Sopenharmony_ci console.info(TAG, `onDestroy`); 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci ``` 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci4. 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. 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci ```json 177e41f4b71Sopenharmony_ci { 178e41f4b71Sopenharmony_ci "module": { 179e41f4b71Sopenharmony_ci ... 180e41f4b71Sopenharmony_ci "extensionAbilities": [ 181e41f4b71Sopenharmony_ci { 182e41f4b71Sopenharmony_ci "name": "ActionExtAbility", 183e41f4b71Sopenharmony_ci "icon": "$media:icon", 184e41f4b71Sopenharmony_ci "description": "action", 185e41f4b71Sopenharmony_ci "type": "action", 186e41f4b71Sopenharmony_ci "exported": true, 187e41f4b71Sopenharmony_ci "srcEntry": "./ets/actionextability/ActionExtAbility.ets" 188e41f4b71Sopenharmony_ci } 189e41f4b71Sopenharmony_ci ] 190e41f4b71Sopenharmony_ci } 191e41f4b71Sopenharmony_ci } 192e41f4b71Sopenharmony_ci ``` 193