windowextensionability.md 4.4 KB
Newer Older
1 2
# WindowExtensionAbility

3

4 5 6 7 8 9
[WindowExtensionAbility](../reference/apis/js-apis-application-windowExtensionAbility.md) is a type of ExtensionAbility component that allows a system application to be embedded in and displayed over another application.


The WindowExtensionAbility component must be used together with the [AbilityComponent](../reference/arkui-ts/ts-container-ability-component.md) to process services of the started application. WindowExtensionAbility is run in connection mode. A system application must use the AbilityComponent to start the WindowExtensionAbility component.

Each ExtensionAbility has its own context. For WindowExtensionAbility,
10
the context is [WindowExtensionContext](../reference/apis/js-apis-inner-application-windowExtensionContext.md).  
11 12 13

> **NOTE**
>
G
Gloria 已提交
14
> **WindowExtensionAbility** is a system API. To embed a third-party application in another application and display it over the application, switch to the full SDK by following the instructions provided in [Guide to Switching to Full SDK](../faqs/full-sdk-switch-guide.md).
15 16 17
>


18
## Setting an Embedded UIAbility (for System Applications Only)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

The **WindowExtensionAbility** class provides **onConnect()**, **onDisconnect()**, and **onWindowReady()** lifecycle callbacks, which can be overridden.

- The **onWindowReady()** callback is invoked when a window is created for the ability.

- The **onConnect()** callback is invoked when the AbilityComponent corresponding to the window connects to the ability.

- The **onDisconnect()** callback is invoked when the AbilityComponent disconnects from the ability.


**How to Develop**

To implement an embedded application, manually create a WindowExtensionAbility in DevEco Studio as follows:

1. In the **ets** directory of the **Module** project, right-click and choose **New > Directory** to create a directory named **WindowExtAbility**.

2. Right-click the **WindowExtAbility** directory, and choose **New > TypeScript File** to create a file named **WindowExtAbility.ts**.

3. Open the **WindowExtAbility.ts** file and import the dependency package of **WindowExtensionAbility**. Customize a class that inherits from **WindowExtensionAbility** and implement the **onWindowReady()**, **onConnect()**, and **onDisconnect()** lifecycle callbacks.

   ```ts
   import Extension from '@ohos.application.WindowExtensionAbility'

    export default class WindowExtAbility extends Extension {
        onWindowReady(window) {
            window.loadContent('WindowExtAbility/pages/index1').then(() => {
                window.getProperties().then((pro) => {
G
Gloria 已提交
46
                    console.info("WindowExtension " + JSON.stringify(pro));
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
                })
                window.show();
            })
        }

        onConnect(want) {
            console.info('JSWindowExtension onConnect ' + want.abilityName);
        }

        onDisconnect(want) {
            console.info('JSWindowExtension onDisconnect ' + want.abilityName);
        }
    }
   ```

62
4. Register the WindowExtensionAbility in the [module.json5 file](../quick-start/module-configuration-file.md) corresponding to the **Module** project. Set **type** to **"window"** and **srcEntry** to the code path of the ExtensionAbility component.
63 64 65 66 67 68 69

   ```json
   {
     "module": {
       "extensionAbilities": [
            {
                "name": "WindowExtAbility",
70
                "srcEntry": "./ets/WindowExtAbility/WindowExtAbility.ts",
71 72 73
                "icon": "$media:icon",
                "description": "WindowExtension",
                "type": "window",
74
                "exported": true,
75 76 77 78 79 80 81
            }
        ],
     }
   }
   ```


82
## Starting an Embedded UIAbility (for System Applications Only)
83 84 85 86 87 88 89 90 91 92 93

System applications can load the created WindowExtensionAbility through the AbilityComponent.

**How to Develop**

1. To connect to an embedded application, add the AbilityComponent to the corresponding pages in the DevEco Studio project.

2. Set **bundleName** and **abilityName** in the AbilityComponent.

3. Set the width and height. The sample code is as follows:

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
   ```ts
   @Entry
   @Component
   struct Index {
     @State message: string = 'Hello World'
   
     build() {
       Row() {
         Column() {
           AbilityComponent({ abilityName: "WindowExtAbility", bundleName: "com.example.WindowExtAbility"})
             .width(500)
             .height(500)
         }
         .width('100%')
       }
       .height('100%')
       .backgroundColor(0x64BB5c)
     }
   }
G
Gloria 已提交
113 114
   ```