提交 5afd130d 编写于 作者: G Gloria

Update docs against 9627+10582

Signed-off-by: wusongqing<wusongqing@huawei.com>
上级 ac819e96
......@@ -14,7 +14,7 @@ This module provides APIs for accessing ability-specific resources. You can use
Before using the **AbilityContext** module, you must define a child class that inherits from **Ability**.
```js
import Ability from '@ohos.application.Ability'
import Ability from '@ohos.application.Ability';
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
......@@ -34,7 +34,7 @@ class MainAbility extends Ability {
## AbilityContext.startAbility
startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void
startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
Starts an ability. This API uses an asynchronous callback to return the result.
......@@ -63,9 +63,9 @@ Starts an ability. This API uses an asynchronous callback to return the result.
## AbilityContext.startAbility
startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void;
Starts an ability with start options specified. This API uses an asynchronous callback to return the result.
Starts an ability with the start options specified. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
......@@ -1143,7 +1143,7 @@ Sets a label for this ability in the mission. This API uses an asynchronous call
## AbilityContext.setMissionLabel
setMissionLabel(label: string): Promise&lt;void&gt;
setMissionLabel(label: string): Promise&lt;void&gt;;
Sets a label for this ability in the mission. This API uses a promise to return the result.
......@@ -1190,7 +1190,7 @@ Sets an icon for this ability in the mission. This API uses an asynchronous call
**Example**
```js
import image from '@ohos.multimedia.image'
import image from '@ohos.multimedia.image';
var imagePixelMap;
var color = new ArrayBuffer(0);
var initializationOptions = {
......@@ -1237,7 +1237,7 @@ Sets an icon for this ability in the mission. This API uses a promise to return
**Example**
```js
import image from '@ohos.multimedia.image'
import image from '@ohos.multimedia.image';
var imagePixelMap;
var color = new ArrayBuffer(0);
var initializationOptions = {
......
......@@ -2,12 +2,12 @@
The **ExtensionContext** module, inherited from **Context**, implements the context for Extension abilities.
This module allows access to Extension abilities.
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-service-extension-context.md)**, which extends the capabilities of starting, stopping, binding, and unbinding abilities based on **ExtensionContext**.
> **NOTE**
>
> 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.
> - 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.
## Attributes
......@@ -15,6 +15,99 @@ This module allows access to Extension abilities.
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP. |
| config | Configuration | Yes| No| Module configuration information.|
| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundle-ExtensionAbilityInfo.md) | Yes| No| Extension ability information.|
| currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the HAP file<br>(See **api\bundle\hapModuleInfo.d.ts** in the **SDK** directory.) |
| config | Configuration | Yes| No| Module configuration information.<br>(See **api\@ohos.application.Configuration.d.ts** in the **SDK** directory.)|
| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundle-ExtensionAbilityInfo.md) | Yes| No| Extension ability information.<br>(See **api\bundle\extensionAbilityInfo.d.ts** in the **SDK** directory.)|
## 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.
``` js
import ServiceExtension from '@ohos.application.ServiceExtensionAbility'
import Want from '@ohos.application.Want'
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.
``` js
import Ability from '@ohos.application.Ability'
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
console.log("[Demo] MainAbility onCreate");
let wantExt = {
deviceId: "",
bundleName: "com.example.TheServiceExtension",
abilityName: "TheServiceExtension",
};
this.context.startServiceExtensionAbility(wantExt);
}
};
```
Create a **ServiceModule.ts** file in the entry to execute service logic.
``` js
export default class ServiceModel {
moduleName: string;
constructor() {}
executeTask() {
if (globalThis.ExtensionContext == undefined) {
console.log("ERROR, ServiceExtension does not exist");
return;
}
var moduleInfo = globalThis.ExtensionContext.currentHapModuleInfo;
this.moduleName = moduleInfo.name;
// Execute service logic based on the module name, which differentiates devices with different performance.
switch (this.moduleName) {
case "highPerformance":
console.log("This is high performance device.");
// Execute the corresponding service logic.
break;
case "midPerformance":
console.log("This is mid performance device.");
// Execute the corresponding service logic.
break;
case "lowPerformance":
console.log("This is low performance device.");
// Execute the corresponding service logic.
break;
default:
console.log("ERROR, invalid moduleName.");
break;
}
}
};
```
......@@ -6,8 +6,8 @@ You can use the APIs of this module to start, terminate, connect, and disconnect
> **NOTE**
>
> 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.
> - 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.
## Usage
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册