diff --git a/en/application-dev/reference/apis/js-apis-ability-context.md b/en/application-dev/reference/apis/js-apis-ability-context.md new file mode 100644 index 0000000000000000000000000000000000000000..5d85731dc2ee337581c5fa38d7d52d60170675d4 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-ability-context.md @@ -0,0 +1,217 @@ +# AbilityContext + +> ![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. + + +Implements the ability context. This module is inherited from **Context**. + + +## Attributes + +| Name| Type| Readable| Writable| Description| +| -------- | -------- | -------- | -------- | -------- | +| abilityInfo | AbilityInfo | Yes| No| Ability information.| +| currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP.| + + +## startAbility + +startAbility(want: Want, callback: AsyncCallback<void>): void + +Starts an ability. This method uses a callback to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| + | callback | AsyncCallback<void> | Yes| Callback used to return the result.| + +- Example + ``` + var want = { + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "com.extreme.test.MainAbility" + }; + this.context.startAbility(want, (error) => { + console.log("error.code = " + error.code) + }) + ``` + + +## startAbility + +startAbility(want: Want): Promise<void>; + +Starts an ability. This method uses a promise to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the **Want** used for starting an ability.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<void> | Promise used to return the result.| + +- Example + ``` + var want = { + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "com.extreme.test.MainAbility" + }; + this.context.startAbility(want) + .then((data) => { + console.log('Operation successful.') + }).catch((error) => { + console.log('Operation failed.'); + }) + ``` + + +## startAbilityForResult + +startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void; + +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)| Yes| Information about the **Want** used for starting an ability.| + | callback | Callback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Yes| Callback used to return the result.| + + +- Example + ``` + this.context.startAbilityForResult( + {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, + (error, result) => { + console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code) + console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode) + } + ); + ``` + + +## startAbilityForResult + +startAbilityForResult(want: Want): Promise<AbilityResult>; + +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)| Yes| Information about the **Want** used for starting an ability.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | Promise used to return the result.| + +- Example + ``` + this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}).then((result) => { + console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode) + }, (error) => { + console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code) + }) + ``` + + +## terminateSelf + +terminateSelf(callback: AsyncCallback<void>): void; + +Terminates this ability. This method uses a callback to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | callback | AsyncCallback<void> | No| Callback used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.terminateSelf((err) => { + console.log('terminateSelf result:' + JSON.stringfy(err); + } + ``` + + +## terminateSelf + +terminateSelf(): Promise<void>; + +Terminates this ability. This method uses a promise to return the result. + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<void> | Promise used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.terminateSelf(want).then((data) => { + console.log('success:' + JSON.stringfy(data)); + )).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + + +## terminateSelfWithResult + +terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void; + +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) | Yes| Information returned to the caller.| + | callback | Callback<void> | No| Callback used to return the information.| + +- Example + ``` + this.context.terminateSelfWithResult( + { + want: {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo"}, + resultCode: 100 + }, (error) => { + console.log("terminateSelfWithResult is called = " + error.code) + } + ); + ``` + + +## terminateSelfWithResult + +terminateSelfWithResult(parameter: AbilityResult): Promise<void>; + +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) | Yes| Information returned to the caller.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<void> | Promise used to return the result.| + +- Example + ``` + this.context.terminateSelfWithResult( + { + want: {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo"}, + resultCode: 100 + }).then((result) => { + console.log("terminateSelfWithResult") + } + ) + ``` diff --git a/en/application-dev/reference/apis/js-apis-extension-context.md b/en/application-dev/reference/apis/js-apis-extension-context.md new file mode 100644 index 0000000000000000000000000000000000000000..be430dce1a7275fd27d7c387199c0d353b8e8d02 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-extension-context.md @@ -0,0 +1,14 @@ +# ExtensionContext + +> ![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. + + +Implements the extension context. This module is inherited from **Context**. + + +## Attributes + +| Name| Type| Readable| Writable| Description| +| -------- | -------- | -------- | -------- | -------- | +| currentHapModuleInfo | HapModuleInfo | Yes| No| Information about the current HAP.| diff --git a/en/application-dev/reference/apis/js-apis-service-extension-context.md b/en/application-dev/reference/apis/js-apis-service-extension-context.md new file mode 100644 index 0000000000000000000000000000000000000000..fa46ef6df13d0e7bd7b4c8dd788fbe4184417488 --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-service-extension-context.md @@ -0,0 +1,193 @@ +# ServiceExtensionContext + +> ![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. + + +Implements the context that provides the capabilities and APIs of **ServiceExtension**. This class is inherited from **ExtensionContext**. + + +## startAbility + + +startAbility(want: Want, callback: AsyncCallback<void>): void; + + +Starts an ability. This method uses a callback to return the result. + + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information about the ability to start, such as the ability name and bundle name.| + | callback | AsyncCallback<void> | No| Callback used to return the result indicating whether the method is successfully called.| + +- Example + ``` + let want = { + "bundleName": "com.example.myapp", + "abilityName": "com.example.myapp.MyAbility" + }; + this.context.startAbility(want, (err) => { + console.log('startAbility result:' + JSON.stringfy(err); + } + ``` + + +## startAbility + +startAbility(want: Want): Promise<void>; + +Starts an ability. This method uses a promise to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | 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<void> | Promise used to return the result indicating whether the method is successfully called.| + +- Example + ``` + let want = { + "bundleName": "com.example.myapp", + "abilityName": "com.example.myapp.MyAbility" + }; + this.context.startAbility(want).then((data) => { + console.log('success:' + JSON.stringfy(data)); + )).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + + +## terminateSelf + +terminateSelf(callback: AsyncCallback<void>): void; + +Terminates this ability. This method uses a callback to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | callback | AsyncCallback<void> | No| Callback used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.terminateSelf((err) => { + console.log('terminateSelf result:' + JSON.stringfy(err); + } + ``` + + +## terminateSelf + +terminateSelf(): Promise<void>; + +Terminates this ability. This method uses a promise to return the result. + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<void> | Promise used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.terminateSelf(want).then((data) => { + console.log('success:' + JSON.stringfy(data)); + )).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + + +## connectAbility + +connectAbility(want: Want, options: ConnectOptions): number; + +Connects this ability to a Service ability. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | 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) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.| + +- Return value + | Type| Description| + | -------- | -------- | + | number | A number, based on which the connection will be interrupted.| + +- Example + ``` + let want = { + "bundleName": "com.example.myapp", + "abilityName": "com.example.myapp.MyAbility" + }; + let options = { + onConnect: function(elementName, proxy) {} + onDisConnect: function(elementName) {} + onFailed: function(code) {} + } + let connection = this.context.connectAbility(want,options); + ``` + + +## disconnectAbility + +disconnectAbility(connection: number, callback:AsyncCallback<void>): void; + +Disconnects this ability from the Service ability. This method uses a callback to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | connection | number | Yes| Number returned after **connectAbility** is called.| + | callback | AsyncCallback<void> | No| Callback used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.disconnectAbility(connection, (err) => { // connection is the return value of connectAbility. + console.log('terminateSelf result:' + JSON.stringfy(err); + } + ``` + + +## disconnectAbility + +disconnectAbility(connection: number): Promise<void>; + +Disconnects this ability from the Service ability. This method uses a promise to return the result. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | connection | number | Yes| Number returned after **connectAbility** is called.| + +- Return value + | Type| Description| + | -------- | -------- | + | Promise<void> | Promise used to return the result indicating whether the method is successfully called.| + +- Example + ``` + this.context.disconnectAbility(connection).then((data) => { // connection is the return value of connectAbility. + console.log('success:' + JSON.stringfy(data)); + )).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + + +## ConnectOptions + +Defines the **ConnectOptions** data structure. + +| Name| Description| +| -------- | -------- | +| onConnect(elementName:ElementName, remote:IRemoteObject) | Called when this ability is connected to a Service ability.| +| onDisconnect(elementName:ElementName) | Called when the peer service is abnormal or killed.| +| onFailed(code: number) | Called when the connection fails.| diff --git a/en/application-dev/reference/apis/js-apis-service-extension.md b/en/application-dev/reference/apis/js-apis-service-extension.md new file mode 100644 index 0000000000000000000000000000000000000000..22f46948c07041a90488fef30df310ddb2f1294f --- /dev/null +++ b/en/application-dev/reference/apis/js-apis-service-extension.md @@ -0,0 +1,133 @@ +# 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. + + +Provides APIs related to **ServiceExtension**. + + +## Modules to Import + +``` +import ServiceExtension from '@ohos.application.ServiceExtension'; +``` + + +## Required Permissions + +None + + +## Attributes + +| Name| Type| Readable| Writable| Description| +| -------- | -------- | -------- | -------- | -------- | +| context | [ServiceExtensionContext](js-apis-service-extension-context.md) | Yes| No| Service extension context, which is inherited from **ExtensionContext**.| + + +## onCreate + +onCreate(want: Want): void; + +Called when an extension is created to initialize the service logic. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.| + +- Example + ``` + onCreate(want) { + console.log('onCreate, want:' + want.abilityName); + } + ``` + + +## onDestroy + +onDestroy(): void; + +Called when this extension is destroyed to clear resources. + +- Example + ``` + onDestroy() { + console.log('onDestroy'); + destory(); + } + ``` + + +## onRequest + +onRequest(want: Want, startId: number): void; + +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)| Yes| Information related to this extension, including the ability name and bundle name.| + | 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) { + console.log('onRequest, want:' + want.abilityName); + } + ``` + + +## onConnect + +onConnect(want: Want): rpc.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)| Yes| Information related to this extension, including the ability name and bundle name.| + +- Return value + | Type| Description| + | -------- | -------- | + | rpc.RemoteObject | A **RemoteObject** object used for communication with the client.| + +- Example + ``` + import rpc from '@ohos.rpc' + class StubTest extends rpc.RemoteObject{ + constructor(des) { + super(des); + } + onRemoteRequest(code, data, reply, option) { + } + } + ... + onConnect(want) { + console.log('onConnect , want:' + want.abilityName); + return new StubTest("test"); + } + ``` + + +## onDisconnect + +onDisconnect(want: Want): void; + +Called when the ability is disconnected. + +- Parameters + | Name| Type| Mandatory| Description| + | -------- | -------- | -------- | -------- | + | want |[Want](js-apis-featureAbility.md#Want)| Yes| Information related to this extension, including the ability name and bundle name.| + +- Example + ``` + onDisconnect(want) { + console.log('onDisconnect, want:' + want.abilityName); + } + ```