提交 62c6ec8f 编写于 作者: W wusongqing

updated docs

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 9eaa794a
# AbilityContext # AbilityContext
- [属性](#属性) > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
- [startAbility](#startAbility) > The initial APIs of this module are supported since API 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
- [startAbility](#startAbility)
- [startAbilityForResult](#startAbilityForResult)
- [startAbilityForResult](#startAbilityForResult)
- [terminateSelf](#terminateSelf)
- [terminateSelf](#terminateSelf)
- [terminateSelfWithResult](#terminateSelfWithResult)
- [terminateSelfWithResult](#terminateSelfWithResult)
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Ability的上下文环境,继承自Context。 Implements ability context. This module is inherited from **Context**.
## 属性 ## Attributes
| 名称 | 参数类型 | 可读 | 可写 | 说明 | | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| abilityInfo | AbilityInfo | 是 | 否 | Abilityinfo相关信息 | | abilityInfo | AbilityInfo | Yes| No| Ability information.|
| currentHapModuleInfo | HapModuleInfo | 是 | 否 | 当前hap包的信息 | | currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP.|
## startAbility ## startAbility
startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void
启动Ability。 Starts an ability. This method uses a callback to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.|
| callback | AsyncCallback&lt;void&gt; | 是 | callback形式返回启动结果 | | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
- 示例: - Example
``` ```
var want = { var want = {
"deviceId": "", "deviceId": "",
...@@ -53,19 +44,19 @@ startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void ...@@ -53,19 +44,19 @@ startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void
startAbility(want: Want): Promise&lt;void&gt;; startAbility(want: Want): Promise&lt;void&gt;;
启动Ability。通过Promise返回结果。 Starts an ability. This method uses a promise to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.|
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | Promise形式返回启动结果。 | | Promise&lt;void&gt; | Promise used to return the result.|
- 示例: - Example
``` ```
var want = { var want = {
"deviceId": "", "deviceId": "",
...@@ -85,16 +76,16 @@ startAbility(want: Want): Promise&lt;void&gt;; ...@@ -85,16 +76,16 @@ startAbility(want: Want): Promise&lt;void&gt;;
startAbilityForResult(want: Want, callback: AsyncCallback&lt;AbilityResult&gt;): void; startAbilityForResult(want: Want, callback: AsyncCallback&lt;AbilityResult&gt;): void;
启动Ability并在结束的时候返回执行结果。 Starts an ability. This method uses a callback to return the execution result when the ability is terminated.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | | want |[Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.|
| callback | Callback&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult类型说明)&gt; | 是 | 执行结果回调函数。 | | callback | Callback&lt;[AbilityResult](js-apis-featureAbility.md#abilityresult)&gt; | Yes| Callback used to return the result.|
- 示例: - Example
``` ```
this.context.startAbilityForResult( this.context.startAbilityForResult(
{bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"},
...@@ -110,19 +101,19 @@ startAbilityForResult(want: Want, callback: AsyncCallback&lt;AbilityResult&gt;): ...@@ -110,19 +101,19 @@ startAbilityForResult(want: Want, callback: AsyncCallback&lt;AbilityResult&gt;):
startAbilityForResult(want: Want): Promise&lt;AbilityResult&gt;; startAbilityForResult(want: Want): Promise&lt;AbilityResult&gt;;
启动Ability并在结束的时候返回执行结果。 Starts an ability. This method uses a promise to return the execution result when the ability is terminated.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.|
- 返回值 - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;[AbilityResult](js-apis-featureAbility.md#AbilityResult类型说明)&gt; | Promise形式返回执行结果。 | | Promise&lt;[AbilityResult](js-apis-featureAbility.md#abilityresult)&gt; | Promise used to return the result.|
- 示例: - Example
``` ```
this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}).then((result) => { this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}).then((result) => {
console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode) console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode)
...@@ -136,14 +127,14 @@ startAbilityForResult(want: Want): Promise&lt;AbilityResult&gt;; ...@@ -136,14 +127,14 @@ startAbilityForResult(want: Want): Promise&lt;AbilityResult&gt;;
terminateSelf(callback: AsyncCallback&lt;void&gt;): void; terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
停止Ability自身。 Terminates this ability. This method uses a callback to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 | | callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.terminateSelf((err) => { this.context.terminateSelf((err) => {
console.log('terminateSelf result:' + JSON.stringfy(err); console.log('terminateSelf result:' + JSON.stringfy(err);
...@@ -155,14 +146,14 @@ terminateSelf(callback: AsyncCallback&lt;void&gt;): void; ...@@ -155,14 +146,14 @@ terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
terminateSelf(): Promise&lt;void&gt;; terminateSelf(): Promise&lt;void&gt;;
停止Ability自身。通过Promise返回结果。 Terminates this ability. This method uses a promise to return the result.
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 | | Promise&lt;void&gt; | Promise used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.terminateSelf(want).then((data) => { this.context.terminateSelf(want).then((data) => {
console.log('success:' + JSON.stringfy(data)); console.log('success:' + JSON.stringfy(data));
...@@ -176,15 +167,15 @@ terminateSelf(): Promise&lt;void&gt;; ...@@ -176,15 +167,15 @@ terminateSelf(): Promise&lt;void&gt;;
terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void; terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;void&gt;): void;
停止Ability,并返回给调用startAbilityForResult 接口调用方的相关信息。 Terminates this ability. This method uses a callback to return the information to the caller of **startAbilityForResult**.
- 参数 - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#AbilityResult类型说明) | 是 | 返回给调用startAbilityForResult&nbsp;接口调用方的相关信息。 | | parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
| callback | Callback&lt;void&gt; | 否 | callback形式返回停止结果 | | callback | Callback&lt;void&gt; | No| Callback used to return the information.|
- 示例: - Example
``` ```
this.context.terminateSelfWithResult( this.context.terminateSelfWithResult(
{ {
...@@ -201,19 +192,19 @@ terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;voi ...@@ -201,19 +192,19 @@ terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback&lt;voi
terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;; terminateSelfWithResult(parameter: AbilityResult): Promise&lt;void&gt;;
停止Ability,并返回给调用startAbilityForResult 接口相关信息。 Terminates this ability. This method uses a promise to return information to the caller of **startAbilityForResult**.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| parameter | [AbilityResult](js-apis-featureAbility.md#AbilityResult类型说明) | 是 | 返回给startAbilityForResult&nbsp;调用方的信息。 | | parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | promise形式返回停止结果 | | Promise&lt;void&gt; | Promise used to return the result.|
- 示例: - Example
``` ```
this.context.terminateSelfWithResult( this.context.terminateSelfWithResult(
{ {
......
# ExtensionContext # ExtensionContext
- [属性](#属性) > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** > The initial APIs of this module are supported since API 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> 本模块首批接口从API 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Extension的上下文环境,继承自Context。 Implements extension context. This module is inherited from **Context**.
## 属性 ## Attributes
| 名称 | 参数类型 | 可读 | 可写 | 说明 | | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| currentHapModuleInfo | HapModuleInfo | 是 | 否 | 当前Hap包的信息。 | | currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP.|
# ServiceExtensionContext # ServiceExtensionContext
- [startAbility](#startAbility) > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
- [startAbility](#startAbility) > The initial APIs of this module are supported since API 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
- [terminateSelf](#terminateSelf)
- [terminateSelf](#terminateSelf)
- [connectAbility](#connectAbility)
- [disconnectAbility](#disconnectAbility)
- [disconnectAbility](#disconnectAbility)
- [ConnectOptions](#ConnectOptions)
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
ServiceExtension的上下文环境,提供ServiceExtension具有的能力和接口,继承自ExtensionContext。 Implements the context that provides the capabilities and APIs of **ServiceExtension**. This class is inherited from **ExtensionContext**.
## startAbility ## startAbility
...@@ -21,16 +13,16 @@ ServiceExtension的上下文环境,提供ServiceExtension具有的能力和接 ...@@ -21,16 +13,16 @@ ServiceExtension的上下文环境,提供ServiceExtension具有的能力和接
startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void; startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
启动Ability。 Starts an ability. This method uses a callback to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the ability to start, such as the ability name and bundle name.|
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 | | callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
let want = { let want = {
"bundleName": "com.example.myapp", "bundleName": "com.example.myapp",
...@@ -46,19 +38,19 @@ startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void; ...@@ -46,19 +38,19 @@ startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
startAbility(want: Want): Promise&lt;void&gt;; startAbility(want: Want): Promise&lt;void&gt;;
启动Ability。通过Promise返回结果。 Starts an ability. This method uses a promise to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the ability to start, such as the ability name and bundle name.|
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 | | Promise&lt;void&gt; | Promise used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
let want = { let want = {
"bundleName": "com.example.myapp", "bundleName": "com.example.myapp",
...@@ -76,14 +68,14 @@ startAbility(want: Want): Promise&lt;void&gt;; ...@@ -76,14 +68,14 @@ startAbility(want: Want): Promise&lt;void&gt;;
terminateSelf(callback: AsyncCallback&lt;void&gt;): void; terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
停止Ability自身。 Terminates this ability. This method uses a callback to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 | | callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.terminateSelf((err) => { this.context.terminateSelf((err) => {
console.log('terminateSelf result:' + JSON.stringfy(err); console.log('terminateSelf result:' + JSON.stringfy(err);
...@@ -95,14 +87,14 @@ terminateSelf(callback: AsyncCallback&lt;void&gt;): void; ...@@ -95,14 +87,14 @@ terminateSelf(callback: AsyncCallback&lt;void&gt;): void;
terminateSelf(): Promise&lt;void&gt;; terminateSelf(): Promise&lt;void&gt;;
停止自身。通过Promise返回结果。 Terminates this ability. This method uses a promise to return the result.
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 | | Promise&lt;void&gt; | Promise used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.terminateSelf(want).then((data) => { this.context.terminateSelf(want).then((data) => {
console.log('success:' + JSON.stringfy(data)); console.log('success:' + JSON.stringfy(data));
...@@ -116,20 +108,20 @@ terminateSelf(): Promise&lt;void&gt;; ...@@ -116,20 +108,20 @@ terminateSelf(): Promise&lt;void&gt;;
connectAbility(want: Want, options: ConnectOptions): number; connectAbility(want: Want, options: ConnectOptions): number;
将一个Ability与服务类型的Ability绑定。 Connects this ability to a Service ability.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the ability to connect to, such as the ability name and bundle name.|
| options | [ConnectOptions](#connectoptions) | 是 | ConnectOptions类型的回调函数,返回服务连接成功、断开或连接失败后的信息。 | | options | [ConnectOptions](#connectoptions) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
- 返回值 - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | 返回一个number,后续根据这个number去断开连接。 | | number | A number, based on which the connection will be interrupted.|
- 示例: - Example
``` ```
let want = { let want = {
"bundleName": "com.example.myapp", "bundleName": "com.example.myapp",
...@@ -148,17 +140,17 @@ connectAbility(want: Want, options: ConnectOptions): number; ...@@ -148,17 +140,17 @@ connectAbility(want: Want, options: ConnectOptions): number;
disconnectAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void; disconnectAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
将一个Ability与绑定的服务类型的Ability解绑。 Disconnects this ability from the Service ability. This method uses a callback to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| connection | number | 是 | 在connectAbility中返回的number。 | | connection | number | Yes| Number returned after **connectAbility** is called.|
| callback | AsyncCallback&lt;void&gt; | 否 | 回调函数,返回接口调用是否成功的结果。 | | callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.disconnectAbility(connection, (err) => { // connection为connectAbility中的返回值 this.context.disconnectAbility(connection, (err) => { // connection is the return value of connectAbility.
console.log('terminateSelf result:' + JSON.stringfy(err); console.log('terminateSelf result:' + JSON.stringfy(err);
} }
``` ```
...@@ -168,21 +160,21 @@ disconnectAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void; ...@@ -168,21 +160,21 @@ disconnectAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
disconnectAbility(connection: number): Promise&lt;void&gt;; disconnectAbility(connection: number): Promise&lt;void&gt;;
将一个Ability与绑定的服务类型的Ability解绑。通过Promise返回结果。 Disconnects this ability from the Service ability. This method uses a promise to return the result.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| connection | number | 是 | 在connectAbility中返回的number。 | | connection | number | Yes| Number returned after **connectAbility** is called.|
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;void&gt; | 返回一个Promise,包含接口的结果。 | | Promise&lt;void&gt; | Promise used to return the result indicating whether the method is successfully called.|
- 示例: - Example
``` ```
this.context.disconnectAbility(connection).then((data) => { // connection为connectAbility中的返回值 this.context.disconnectAbility(connection).then((data) => { // connection is the return value of connectAbility.
console.log('success:' + JSON.stringfy(data)); console.log('success:' + JSON.stringfy(data));
)).catch((error) => { )).catch((error) => {
console.log('failed:' + JSON.stringfy(error)); console.log('failed:' + JSON.stringfy(error));
...@@ -192,10 +184,10 @@ disconnectAbility(connection: number): Promise&lt;void&gt;; ...@@ -192,10 +184,10 @@ disconnectAbility(connection: number): Promise&lt;void&gt;;
## ConnectOptions ## ConnectOptions
ConnectOptions数据结构。 Defines the **ConnectOptions** data structure.
| 名称 | 说明 | | Name| Description|
| -------- | -------- | | -------- | -------- |
| onConnect(elementName:ElementName,&nbsp;remote:IRemoteObject) | Ability成功连接一个服务类型Ability的回调接口。 | | onConnect(elementName:ElementName,&nbsp;remote:IRemoteObject) | Called when this ability is connected to a Service ability.|
| onDisconnect(elementName:ElementName) | 对端服务发生异常或者被杀死回调该接口。 | | onDisconnect(elementName:ElementName) | Called when the peer service is abnormal or killed.|
| onFailed(code:&nbsp;number) | 连接失败时回调该接口。 | | onFailed(code:&nbsp;number) | Called when the connection fails.|
# ServiceExtension # ServiceExtension
- [导入模块](#导入模块) > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:**
- [权限](#权限) > The initial APIs of this module are supported since API 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
- [属性](#属性)
- [onCreate](#onCreate)
- [onDestroy](#onDestroy)
- [onRequest](#onRequest)
- [onConnect](#onConnect)
- [onDisconnect](#onDisconnect)
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
提供ServiceExtension服务扩展相关接口。 Provides APIs related to **ServiceExtension**.
## 导入模块 ## Modules to Import
``` ```
import ServiceExtension from '@ohos.application.ServiceExtension'; import ServiceExtension from '@ohos.application.ServiceExtension';
``` ```
## 权限 ## Required Permissions
None
## 属性 ## Attributes
| 名称 | 参数类型 | 可读 | 可写 | 说明 | | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| context | [ServiceExtensionContext](js-apis-service-extension-context.md) | 是 | 否 | ServiceExtension的上下文环境,继承自ExtensionContext。 | | context | [ServiceExtensionContext](js-apis-service-extension-context.md) | Yes| No| Service extension context, which is inherited from **ExtensionContext**.|
## onCreate ## onCreate
onCreate(want: Want): void; onCreate(want: Want): void;
Extension生命周期回调,在创建时回调,执行初始化业务逻辑操作。 Called when an extension is created to initialize the service logic.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
- 示例: - Example
``` ```
onCreate(want) { onCreate(want) {
console.log('onCreate, want:' + want.abilityName); console.log('onCreate, want:' + want.abilityName);
...@@ -57,9 +49,9 @@ Extension生命周期回调,在创建时回调,执行初始化业务逻辑 ...@@ -57,9 +49,9 @@ Extension生命周期回调,在创建时回调,执行初始化业务逻辑
onDestroy(): void; onDestroy(): void;
Extension生命周期回调,在销毁时回调,执行资源清理等操作。 Called when this extension is destroyed to clear resources.
- 示例: - Example
``` ```
onDestroy() { onDestroy() {
console.log('onDestroy'); console.log('onDestroy');
...@@ -72,15 +64,15 @@ Extension生命周期回调,在销毁时回调,执行资源清理等操作 ...@@ -72,15 +64,15 @@ Extension生命周期回调,在销毁时回调,执行资源清理等操作
onRequest(want: Want, startId: number): void; onRequest(want: Want, startId: number): void;
Extension生命周期回调,如果是startAbility拉起的服务,会在onCreate之后回调。每次拉起服务都会回调,startId会递增。 Called after **onCreate** is invoked when an ability is started by calling **startAbility**. The value of **startId** is incremented for each ability that is started.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
| startId | number | 是 | 返回拉起次数。首次拉起初始值返回1,多次之后自动递增。 | | startId | number | Yes| Number of ability start times. The initial value is **1**, and the value is automatically incremented for each ability started.|
- 示例: - Example
``` ```
onRequest(want: Want, startId: number) { onRequest(want: Want, startId: number) {
console.log('onRequest, want:' + want.abilityName); console.log('onRequest, want:' + want.abilityName);
...@@ -92,19 +84,19 @@ Extension生命周期回调,如果是startAbility拉起的服务,会在onCre ...@@ -92,19 +84,19 @@ Extension生命周期回调,如果是startAbility拉起的服务,会在onCre
onConnect(want: Want): rpc.RemoteObject; onConnect(want: Want): rpc.RemoteObject;
Extension生命周期回调,如果是connectAbility拉起的服务,会在onCreate之后回调。返回一个RemoteObject对象,用于和客户端进行通信。 Called after **onCreate** is invoked when an ability is started by calling **connectAbility**. A **RemoteObject** object is returned for communication with the client.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want | [Want](js-apis-featureAbility.md#Want类型说明)| 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
- 返回值: - Return value
| 类型 | 说明 | | Type| Description|
| -------- | -------- | | -------- | -------- |
| rpc.RemoteObject | 一个RemoteObject对象,用于和客户端进行通信。 | | rpc.RemoteObject | A **RemoteObject** object used for communication with the client.|
- 示例: - Example
``` ```
import rpc from '@ohos.rpc' import rpc from '@ohos.rpc'
class StubTest extends rpc.RemoteObject{ class StubTest extends rpc.RemoteObject{
...@@ -126,14 +118,14 @@ Extension生命周期回调,如果是connectAbility拉起的服务,会在onC ...@@ -126,14 +118,14 @@ Extension生命周期回调,如果是connectAbility拉起的服务,会在onC
onDisconnect(want: Want): void; onDisconnect(want: Want): void;
Extension的生命周期,断开服务连接时回调。 Called when the ability is disconnected.
- 参数: - Parameters
| 参数名 | 类型 | 必填 | 说明 | | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| want |[Want](js-apis-featureAbility.md#Want类型说明)| 是 | 当前Extension相关的Want类型信息,包括ability名称、bundle名称等。 | | want |[Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.|
- 示例: - Example
``` ```
onDisconnect(want) { onDisconnect(want) {
console.log('onDisconnect, want:' + want.abilityName); console.log('onDisconnect, want:' + want.abilityName);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册