js-apis-inner-application-extensionContext.md 5.1 KB
Newer Older
W
wusongqing 已提交
1 2
# ExtensionContext

3 4
The **ExtensionContext** module, inherited from **Context**, implements the context for Extension abilities.

5
This module provides APIs for accessing resources of a specific Extension ability. An Extension ability can use the context directly provided by **ExtensionContext** or that extended from **ExtensionContext**. For example, **ServiceExtension** uses [ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md), which extends the capabilities of starting, stopping, binding, and unbinding abilities based on **ExtensionContext**.
6

W
wusongqing 已提交
7
> **NOTE**
8
>
G
Gloria 已提交
9 10
>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>  - The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
11

W
wusongqing 已提交
12
## Attributes
W
wusongqing 已提交
13

W
wusongqing 已提交
14 15
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

16
| Name| Type| Readable| Writable| Description| 
W
wusongqing 已提交
17
| -------- | -------- | -------- | -------- | -------- |
18 19
| currentHapModuleInfo | [HapModuleInfo](js-apis-bundle-HapModuleInfo.md) | Yes| No| Information about the HAP file<br>(See **api\bundle\hapModuleInfo.d.ts** in the **SDK** directory.) |
| config   | [Configuration](js-apis-app-ability-configuration.md) | Yes| No| Module configuration information.<br>(See **api\@ohos.app.ability.Configuration.d.ts** in the **SDK** directory.)|
G
Gloria 已提交
20
| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundleManager-extensionAbilityInfo.md) | Yes| No| Extension ability information.<br>(See **api\bundle\extensionAbilityInfo.d.ts** in the **SDK** directory.)|
G
Gloria 已提交
21 22 23 24 25 26 27 28 29 30 31 32

## When to Use
**ExtensionContext** provides information about an Extension ability, module, and HAP file. You can use the information based on service requirements. The following uses **ServiceExtension** as an example to describe a use case of **ExtensionContext**.

**Scenario description**
To adapt to devices with different performance, an application provides three modules: highPerformance, midPerformance, and lowPerformance. Each of them provides a Service Extension ability for the entry. During application installation, the application market installs the HAP file of the entry and the HAP file of the module that matches the device performance. During application running, the entry parses **ServiceExtensionContext.HapModuleInfo** to obtain the HAP file information and executes service logic based on this file.

![Example](figures/en_us_image_ExtensionContext_Example.png)

**Example**

Define a **ServiceExtension** with the same name for the three modules.
33
```ts
34 35
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.application.Want';
G
Gloria 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
export default class TheServiceExtension extends ServiceExtension {
    onCreate(want:Want) {
        console.log('ServiceAbility onCreate, want: ' + want.abilityName);
        // Pass ExtensionContext to entry via globalThis.
        globalThis.ExtensionContext = this.context;
    }

    onRequest(want, startId) {
        console.log('ServiceAbility onRequest, want: ' + want.abilityName + ', startId: ' + startId);
    }

    onConnect(want) {
        console.log('ServiceAbility onConnect, want:' + want.abilityName);
        return null;
    }

    onDisconnect(want) {
        console.log('ServiceAbility onDisconnect, want:' + want.abilityName);
    }

    onDestroy() {
        console.log('ServiceAbility onDestroy');
    }
};
```

Start **ServiceExtension** within the **onCreate** callback of the main ability of the entry.
63
```ts
64 65
import Ability from '@ohos.app.ability.Ability';
export default class MainAbility extends Ability {
G
Gloria 已提交
66
    onCreate(want, launchParam) {
67
        console.log('[Demo] MainAbility onCreate');
G
Gloria 已提交
68
        let wantExt = {
69 70 71
            deviceId: '',
            bundleName: 'com.example.TheServiceExtension',
            abilityName: 'TheServiceExtension',
G
Gloria 已提交
72 73 74 75 76 77 78
        };
        this.context.startServiceExtensionAbility(wantExt);
    }
};
```

Create a **ServiceModule.ts** file in the entry to execute service logic.
79
```ts
G
Gloria 已提交
80 81 82 83 84 85
export default class ServiceModel {
    moduleName: string;

    constructor() {}

    executeTask() {
86 87
        if (globalThis.ExtensionContext === undefined) {
            console.log('ERROR, ServiceExtension does not exist');
G
Gloria 已提交
88 89 90
            return;
        }

91
        let moduleInfo = globalThis.ExtensionContext.currentHapModuleInfo;
G
Gloria 已提交
92 93 94
        this.moduleName = moduleInfo.name;
        // Execute service logic based on the module name, which differentiates devices with different performance.
        switch (this.moduleName) {
95 96
            case 'highPerformance':
                console.log('This is high performance device.');
G
Gloria 已提交
97 98
                // Execute the corresponding service logic.
                break;
99 100
            case 'midPerformance':
                console.log('This is mid performance device.');
G
Gloria 已提交
101 102
                // Execute the corresponding service logic.
                break;
103 104
            case 'lowPerformance':
                console.log('This is low performance device.');
G
Gloria 已提交
105 106 107
                // Execute the corresponding service logic.
                break;
            default:
108
                console.log('ERROR, invalid moduleName.');
G
Gloria 已提交
109 110 111 112 113
                break;
        }
    }
};
```