未验证 提交 01eb32a0 编写于 作者: O openharmony_ci 提交者: Gitee

!12212 Translation finished: 11150【元能力】Stage模型ServiceExtensionContext API错误码资料更新

Merge pull request !12212 from wusongqing/TR11150
......@@ -2,9 +2,9 @@
## When to Use
Ability call is an extension of the ability capability. It enables an ability to be invoked by and communicate with external systems. The ability invoked can be either started in the foreground or created and run in the background. You can use the ability call to implement data sharing between two abilities (caller ability and callee ability) through inter-process communication (IPC).
The core API used for the ability call is `startAbilityByCall`, which differs from `startAbility` in the following ways:
- `startAbilityByCall` supports ability startup in the foreground and background, whereas `startAbility` supports ability startup in the foreground only.
- The caller ability can use the `Caller` object returned by `startAbilityByCall` to communicate with the callee ability, but `startAbility` does not provide the communication capability.
The core API used for the ability call is **startAbilityByCall**, which differs from **startAbility** in the following ways:
- **startAbilityByCall** supports ability startup in the foreground and background, whereas **startAbility** supports ability startup in the foreground only.
- The caller ability can use the **Caller** object returned by **startAbilityByCall** to communicate with the callee ability, but **startAbility** does not provide the communication capability.
Ability call is usually used in the following scenarios:
- Communicating with the callee ability
......@@ -15,17 +15,17 @@ Ability call is usually used in the following scenarios:
|:------|:------|
|Caller ability|Ability that triggers the ability call.|
|Callee ability|Ability invoked by the ability call.|
|Caller |Object returned by `startAbilityByCall` and used by the caller ability to communicate with the callee ability.|
|Caller |Object returned by **startAbilityByCall** and used by the caller ability to communicate with the callee ability.|
|Callee |Object held by the callee ability to communicate with the caller ability.|
|IPC |Inter-process communication.|
The ability call process is as follows:
- The caller ability uses `startAbilityByCall` to obtain a `Caller` object and uses `call()` of the `Caller` object to send data to the callee ability.
- The callee ability, which holds a `Callee` object, uses `on()` of the `Callee` object to register a callback. This callback is invoked when the callee ability receives data from the caller ability.
- The caller ability uses **startAbilityByCall** to obtain a **Caller** object and uses **call()** of the **Caller** object to send data to the callee ability.
- The callee ability, which holds a **Callee** object, uses **on()** of the **Callee** object to register a callback. This callback is invoked when the callee ability receives data from the caller ability.
![stage-call](figures/stage-call.png)
> **NOTE**<br/>
> The launch type of the callee ability must be `singleton`.
> The launch type of the callee ability must be **singleton**.
> Currently, only system applications can use the ability call.
## Available APIs
......@@ -34,30 +34,29 @@ The table below describes the ability call APIs. For details, see [Ability](../r
**Table 2** Ability call APIs
|API|Description|
|:------|:------|
|startAbilityByCall(want: Want): Promise\<Caller>|Starts an ability in the foreground (through the `want` configuration) or background (default) and obtains the `Caller` object for communication with the ability. For details, see [AbilityContext](../reference/apis/js-apis-ability-context.md#abilitycontextstartabilitybycall) or [ServiceExtensionContext](../reference/apis/js-apis-service-extension-context.md#serviceextensioncontextstartabilitybycall).|
|startAbilityByCall(want: Want): Promise\<Caller>|Starts an ability in the foreground (through the **want** configuration) or background (default) and obtains the **Caller** object for communication with the ability. For details, see [AbilityContext](../reference/apis/js-apis-ability-context.md#abilitycontextstartabilitybycall) or [ServiceExtensionContext](../reference/apis/js-apis-service-extension-context.md#serviceextensioncontextstartabilitybycall).|
|on(method: string, callback: CalleeCallBack): void|Callback invoked when the callee ability registers a method.|
|off(method: string): void|Callback invoked when the callee ability deregisters a method.|
|call(method: string, data: rpc.Sequenceable): Promise\<void>|Sends agreed sequenceable data to the callee ability.|
|callWithResult(method: string, data: rpc.Sequenceable): Promise\<rpc.MessageParcel>|Sends agreed sequenceable data to the callee ability and obtains the agreed sequenceable data returned by the callee ability.|
|release(): void|Releases the `Caller` object.|
|onRelease(callback: OnReleaseCallBack): void|Callback invoked when the `Caller` object is released.|
|release(): void|Releases the **Caller** object.|
|on(type: "release", callback: OnReleaseCallback): void|Callback invoked when the **Caller** object is released.|
## How to Develop
The procedure for developing the ability call is as follows:
1. Create a callee ability.
2. Access the callee ability.
> **NOTE**
>
> The code snippets provided in the **How to Develop** section are used to show specific development steps. They may not be able to run independently.
### Creating a Callee Ability
For the callee ability, implement the callback to receive data and the methods to marshal and unmarshal data. When data needs to be received, use `on()` to register a listener. When data does not need to be received, use `off()` to deregister the listener.
For the callee ability, implement the callback to receive data and the methods to marshal and unmarshal data. When data needs to be received, use **on()** to register a listener. When data does not need to be received, use **off()** to deregister the listener.
**1. Configure the ability launch type.**
Set `launchType` of the callee ability to `singleton` in the `module.json5` file.
Set **launchType** of the callee ability to **singleton** in the **module.json5** file.
|JSON Field|Description|
|:------|:------|
|"launchType"|Ability launch type. Set this parameter to `singleton`.|
|"launchType"|Ability launch type. Set this parameter to **singleton**.|
An example of the ability configuration is as follows:
```json
......@@ -73,7 +72,7 @@ An example of the ability configuration is as follows:
```
**2. Import the Ability module.**
```ts
import Ability from '@ohos.application.Ability'
import Ability from '@ohos.app.ability.UIAbility'
```
**3. Define the agreed sequenceable data.**
......@@ -101,9 +100,9 @@ export default class MySequenceable {
}
}
```
**4. Implement `Callee.on` and `Callee.off`.**
**4. Implement Callee.on and Callee.off.**
The time to register a listener for the callee ability depends on your application. The data sent and received before the listener is registered and that after the listener is deregistered are not processed. In the following example, the `MSG_SEND_METHOD` listener is registered in `onCreate` of the ability and deregistered in `onDestroy`. After receiving sequenceable data, the application processes the data and returns the data result. You need to implement processing based on service requirements. The code snippet is as follows:
The time to register a listener for the callee ability depends on your application. The data sent and received before the listener is registered and that after the listener is deregistered are not processed. In the following example, the **MSG_SEND_METHOD** listener is registered in **onCreate** of the ability and deregistered in **onDestroy**. After receiving sequenceable data, the application processes the data and returns the data result. You need to implement processing based on service requirements. The code snippet is as follows:
```ts
const TAG: string = '[CalleeAbility]'
const MSG_SEND_METHOD: string = 'CallSendMsg'
......@@ -143,16 +142,16 @@ export default class CalleeAbility extends Ability {
### Accessing the Callee Ability
**1. Import the Ability module.**
```ts
import Ability from '@ohos.application.Ability'
import Ability from '@ohos.app.ability.UIAbility'
```
**2. Obtain the `Caller` object.**
**2. Obtain the Caller object.**
The `context` attribute of the ability implements `startAbilityByCall` to obtain the `Caller` object for communication. The following example uses `this.context` to obtain the `context` attribute of the ability, uses `startAbilityByCall` to start the callee ability, obtain the `Caller` object, and register the `onRelease` listener of the caller ability. You need to implement processing based on service requirements. The code snippet is as follows:
The **context** attribute of the ability implements **startAbilityByCall** to obtain the **Caller** object for communication. The following example uses **this.context** to obtain the **context** attribute of the ability, uses **startAbilityByCall** to start the callee ability, obtain the **Caller** object, and register the **onRelease** listener of the caller ability. You need to implement processing based on service requirements. The code snippet is as follows:
```ts
// Register the onRelease listener of the caller ability.
private regOnRelease(caller) {
try {
caller.onRelease((msg) => {
caller.on("release", (msg) => {
console.log(`caller onRelease is called ${msg}`)
})
console.log('caller register OnRelease succeed')
......@@ -193,7 +192,7 @@ async onButtonGetRemoteCaller() {
caller = data
console.log('get remote caller success')
// Register the onRelease listener of the caller ability.
caller.onRelease((msg) => {
caller.on("release", (msg) => {
console.log(`remote caller onRelease is called ${msg}`)
})
console.log('remote caller register OnRelease succeed')
......@@ -203,7 +202,7 @@ async onButtonGetRemoteCaller() {
})
}
```
Obtain the ID of the peer device from `DeviceManager`. Note that the `getTrustedDeviceListSync` API is open only to system applications. The code snippet is as follows:
Obtain the ID of the peer device from **DeviceManager**. Note that the **getTrustedDeviceListSync** API is open only to system applications. The code snippet is as follows:
```ts
import deviceManager from '@ohos.distributedHardware.deviceManager';
var dmClass;
......@@ -248,7 +247,7 @@ async onButtonCall() {
}
```
In the following, `CallWithResult` is used to send data `originMsg` to the callee ability and assign the data processed by the `CallSendMsg` method to `backMsg`. The code snippet is as follows:
In the following, **CallWithResult** is used to send data **originMsg** to the callee ability and assign the data processed by the **CallSendMsg** method to **backMsg**. The code snippet is as follows:
```ts
const MSG_SEND_METHOD: string = 'CallSendMsg'
originMsg: string = ''
......@@ -268,9 +267,9 @@ async onButtonCallWithResult(originMsg, backMsg) {
}
}
```
**4. Release the `Caller` object.**
**4. Release the Caller object.**
When the `Caller` object is no longer required, use `release()` to release it. The code snippet is as follows:
When the **Caller** object is no longer required, use **release()** to release it. The code snippet is as follows:
```ts
releaseCall() {
try {
......
......@@ -13,11 +13,13 @@ 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';
```ts
import Ability from '@ohos.app.ability.UIAbility';
let context = undefined;
class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
let context = this.context;
context = this.context;
}
}
```
......@@ -44,20 +46,40 @@ Starts an ability. This API uses an asynchronous callback to return the result.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
try {
this.context.startAbility(want, (error) => {
console.log("error.code = " + error.code)
})
if (error.code) {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -73,27 +95,47 @@ Starts an ability with the start options specified. This API uses an asynchronou
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var options = {
windowMode: 0,
windowMode: 0
};
try {
this.context.startAbility(want, options, (error) => {
console.log("error.code = " + error.code)
})
if (error.code) {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startAbility
startAbility(want: Want, options?: StartOptions): Promise&lt;void&gt;;
......@@ -106,7 +148,7 @@ Starts an ability. This API uses a promise to return the result.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
**Return value**
......@@ -115,23 +157,40 @@ Starts an ability. This API uses a promise to return the result.
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
var options = {
windowMode: 0,
};
try {
this.context.startAbility(want, options)
.then(() => {
console.log('Operation successful.')
}).catch((error) => {
console.log('Operation failed.');
.then((data) => {
// Carry out normal service processing.
console.log('startAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -147,20 +206,42 @@ Starts an ability. This API uses an asynchronous callback to return the result w
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want |[Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback&lt;[AbilityResult](js-apis-featureAbility.md#abilityresult)&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
this.context.startAbilityForResult(
{deviceId: "", 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)
```ts
var want = {
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
try {
this.context.startAbilityForResult(want, (error, result) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log("startAbilityForResult succeed, result.resultCode = " +
result.resultCode)
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
);
```
## AbilityContext.startAbilityForResult
......@@ -175,24 +256,46 @@ Starts an ability with start options specified. This API uses an asynchronous ca
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want |[Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want |[Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;[AbilityResult](js-apis-featureAbility.md#abilityresult)&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var options = {
windowMode: 0,
};
this.context.startAbilityForResult(
{deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options,
(error, result) => {
console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code)
console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode)
try {
this.context.startAbilityForResult(want, options, (error, result) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log("startAbilityForResult succeed, result.resultCode = " +
result.resultCode)
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
);
```
......@@ -208,7 +311,7 @@ Starts an ability. This API uses a promise to return the result when the ability
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
......@@ -218,17 +321,40 @@ Starts an ability. This API uses a promise to return the result when the ability
| -------- | -------- |
| Promise&lt;[AbilityResult](js-apis-featureAbility.md#abilityresult)&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
var options = {
windowMode: 0,
};
this.context.startAbilityForResult({deviceId: "", bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options).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)
try {
this.context.startAbilityForResult(want, options)
.then((result) => {
// Carry out normal service processing.
console.log("startAbilityForResult succeed, result.resultCode = " + result.resultCode);
})
.catch((error) => {
// Process service logic errors.
console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startAbilityForResultWithAccount
......@@ -247,23 +373,44 @@ Starts an ability. This API uses an asynchronous callback to return the result w
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<AbilityResult\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startAbilityWithAccount(want, accountId, (err, data) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
console.log('---------- startAbilityWithAccount success, data: -----------', data);
try {
this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
result.resultCode)
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -283,26 +430,48 @@ Starts an ability with start options specified. This API uses an asynchronous ca
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
this.context.startAbilityForResultWithAccount(want, accountId, options, (err) => {
console.log('---------- startAbilityForResultWithAccount fail, err: -----------', err);
try {
this.context.startAbilityForResultWithAccount(want, accountId, options, (error, result) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
result.resultCode)
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -322,7 +491,7 @@ Starts an ability with start options specified. This API uses a promise to retur
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
......@@ -332,25 +501,43 @@ Starts an ability with start options specified. This API uses a promise to retur
| -------- | -------- |
| Promise&lt;AbilityResult&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
try {
this.context.startAbilityForResultWithAccount(want, accountId, options)
.then((data) => {
console.log('---------- startAbilityForResultWithAccount success, data: -----------', data);
})
.catch((err) => {
console.log('---------- startAbilityForResultWithAccount fail, err: -----------', err);
.then((result) => {
// Carry out normal service processing.
console.log("startAbilityForResultWithAccount succeed, result.resultCode = " +
result.resultCode)
})
.catch((error) => {
// Process service logic errors.
console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startServiceExtensionAbility
......@@ -366,20 +553,41 @@ Starts a new Service Extension ability. This API uses an asynchronous callback t
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
this.context.startServiceExtensionAbility(want, (err) => {
console.log('---------- startServiceExtensionAbility fail, err: -----------', err);
try {
this.context.startServiceExtensionAbility(want, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startServiceExtensionAbility
......@@ -396,24 +604,42 @@ Starts a new Service Extension ability. This API uses a promise to return the re
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
try {
this.context.startServiceExtensionAbility(want)
.then(() => {
console.log('---------- startServiceExtensionAbility success -----------');
})
.catch((err) => {
console.log('---------- startServiceExtensionAbility fail, err: -----------', err);
.then((data) => {
// Carry out normal service processing.
console.log('startServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startServiceExtensionAbilityWithAccount
startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void;
......@@ -430,22 +656,43 @@ Starts a new Service Extension ability with the account ID specified. This API u
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startServiceExtensionAbilityWithAccount(want,accountId, (err) => {
console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startServiceExtensionAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startServiceExtensionAbilityWithAccount
......@@ -464,25 +711,42 @@ Starts a new Service Extension ability with the account ID specified. This API u
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startServiceExtensionAbilityWithAccount(want,accountId)
.then(() => {
console.log('---------- startServiceExtensionAbilityWithAccount success -----------');
})
.catch((err) => {
console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.startServiceExtensionAbilityWithAccount(want, accountId)
.then((data) => {
// Carry out normal service processing.
console.log('startServiceExtensionAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.stopServiceExtensionAbility
......@@ -498,20 +762,41 @@ Stops a Service Extension ability in the same application. This API uses an asyn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
this.context.stopServiceExtensionAbility(want, (err) => {
console.log('---------- stopServiceExtensionAbility fail, err: -----------', err);
try {
this.context.stopServiceExtensionAbility(want, (error) => {
if (error.code) {
// Process service logic errors.
console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('stopServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.stopServiceExtensionAbility
......@@ -528,23 +813,40 @@ Stops a Service Extension ability in the same application. This API uses a promi
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
try {
this.context.stopServiceExtensionAbility(want)
.then(() => {
console.log('---------- stopServiceExtensionAbility success -----------');
})
.catch((err) => {
console.log('---------- stopServiceExtensionAbility fail, err: -----------', err);
.then((data) => {
// Carry out normal service processing.
console.log('stopServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.stopServiceExtensionAbilityWithAccount
......@@ -563,22 +865,43 @@ Stops a Service Extension ability in the same application with the account ID sp
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.stopServiceExtensionAbilityWithAccount(want,accountId, (err) => {
console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('stopServiceExtensionAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.stopServiceExtensionAbilityWithAccount
......@@ -597,25 +920,42 @@ Stops a Service Extension ability in the same application with the account ID sp
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.stopServiceExtensionAbilityWithAccount(want,accountId)
.then(() => {
console.log('---------- stopServiceExtensionAbilityWithAccount success -----------');
})
.catch((err) => {
console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
.then((data) => {
// Carry out normal service processing.
console.log('stopServiceExtensionAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.terminateSelf
......@@ -632,11 +972,25 @@ Terminates this ability. This API uses an asynchronous callback to return the re
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
this.context.terminateSelf((err) => {
console.log('terminateSelf result:' + JSON.stringify(err));
```ts
this.context.terminateSelf((error) => {
if (error.code) {
// Process service logic errors.
console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('terminateSelf succeed');
});
```
......@@ -655,13 +1009,23 @@ Terminates this ability. This API uses a promise to return the result.
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
this.context.terminateSelf().then(() => {
console.log('success');
```ts
this.context.terminateSelf().then((data) => {
// Carry out normal service processing.
console.log('terminateSelf succeed');
}).catch((error) => {
console.log('failed:' + JSON.stringify(error));
// Process service logic errors.
console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
```
......@@ -681,12 +1045,19 @@ Terminates this ability. This API uses an asynchronous callback to return the ab
| parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | Yes| Information returned to the caller.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"bundleName": "com.extreme.myapplication",
"abilityName": "SecondAbility"
bundleName: "com.extreme.myapplication",
abilityName: "SecondAbility"
}
var resultCode = 100;
// AbilityResult information returned to the caller.
......@@ -694,10 +1065,23 @@ Terminates this ability. This API uses an asynchronous callback to return the ab
want,
resultCode
}
try {
this.context.terminateSelfWithResult(abilityResult, (error) => {
console.log("terminateSelfWithResult is called = " + error.code)
if (error.code) {
// Process service logic errors.
console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('terminateSelfWithResult succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
);
```
......@@ -721,12 +1105,20 @@ Terminates this ability. This API uses a promise to return the ability result in
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"bundleName": "com.extreme.myapplication",
"abilityName": "SecondAbility"
bundleName: "com.extreme.myapplication",
abilityName: "SecondAbility"
}
var resultCode = 100;
// AbilityResult information returned to the caller.
......@@ -734,28 +1126,39 @@ Terminates this ability. This API uses a promise to return the ability result in
want,
resultCode
}
this.context.terminateSelfWithResult(abilityResult).then((result) => {
console.log("terminateSelfWithResult")
try {
this.context.terminateSelfWithResult(abilityResult)
.then((data) => {
// Carry out normal service processing.
console.log('terminateSelfWithResult succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
)
```
## AbilityContext.connectAbility
## AbilityContext.connectServiceExtensionAbility
connectAbility(want: Want, options: ConnectOptions): number;
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to another ability.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | No| Parameters for the connection.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Parameters for the connection.|
**Return value**
......@@ -763,27 +1166,41 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template to connect this ability to
| -------- | -------- |
| number | Result code of the ability connection.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var options = {
onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
onFailed(code) { console.log('----------- onFailed -----------') }
}
const result = this.context.connectAbility(want, options);
console.log('----------- connectAbilityResult: ------------', result);
var connection = null;
try {
connection = this.context.connectServiceExtensionAbility(want, options);
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.connectAbilityWithAccount
## AbilityContext.connectServiceExtensionAbilityWithAccount
connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability with the account ID specified.
......@@ -797,9 +1214,9 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | No| Parameters for the connection.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Parameters for the connection.|
**Return value**
......@@ -807,13 +1224,20 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
| -------- | -------- |
| number | Result code of the ability connection.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
......@@ -821,20 +1245,25 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
onFailed(code) { console.log('----------- onFailed -----------') }
}
const result = this.context.connectAbilityWithAccount(want, accountId, options);
console.log('----------- connectAbilityResult: ------------', result);
var connection = null;
try {
connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.disconnectAbility
## AbilityContext.disconnectServiceExtensionAbility
disconnectAbility(connection: number): Promise\<void>;
disconnectServiceExtensionAbility(connection: number): Promise\<void>;
Disconnects a connection. This API uses a promise to return the result.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
......@@ -847,27 +1276,45 @@ Disconnects a connection. This API uses a promise to return the result.
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
var connectionNumber = 0;
this.context.disconnectAbility(connectionNumber).then(() => {
console.log('disconnectAbility success');
}).catch((err) => {
console.log('disconnectAbility fail, err: ', err);
```ts
// connection is the return value of connectAbility.
var connection = 1;
try {
this.context.disconnectServiceExtensionAbility(connection)
.then((data) => {
// Carry out normal service processing.
console.log('disconnectServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.disconnectAbility
## AbilityContext.disconnectServiceExtensionAbility
disconnectAbility(connection: number, callback:AsyncCallback\<void>): void;
disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void;
Disconnects a connection. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
......@@ -875,13 +1322,35 @@ Disconnects a connection. This API uses an asynchronous callback to return the r
| connection | number | Yes| Result code of the ability connection.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
var connectionNumber = 0;
this.context.disconnectAbility(connectionNumber, (err) => {
console.log('---------- disconnectAbility fail, err: -----------', err);
```ts
// connection is the return value of connectServiceExtensionAbility.
var connection = 1;
try {
this.context.disconnectServiceExtensionAbility(connection, (error) => {
if (error.code) {
// Process service logic errors.
console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('disconnectServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startAbilityByCall
......@@ -892,6 +1361,8 @@ Starts an ability in the foreground or background and obtains the caller object
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
**System API**: This is a system API and cannot be called by third-party applications.
**Parameters**
| Name| Type| Mandatory| Description|
......@@ -906,8 +1377,10 @@ Starts an ability in the foreground or background and obtains the caller object
**Example**
```js
let caller = undefined;
Start an ability in the background.
```ts
var caller = undefined;
// Start an ability in the background by not passing parameters.
var wantBackground = {
......@@ -916,13 +1389,29 @@ Starts an ability in the foreground or background and obtains the caller object
abilityName: "MainAbility",
deviceId: ""
};
try {
this.context.startAbilityByCall(wantBackground)
.then((obj) => {
// Carry out normal service processing.
caller = obj;
console.log('GetCaller success');
console.log('startAbilityByCall succeed');
}).catch((error) => {
console.log(`GetCaller failed with ${error}`);
// Process service logic errors.
console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
Start an ability in the foreground.
```ts
var caller = undefined;
// Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
var wantForeground = {
......@@ -934,13 +1423,23 @@ Starts an ability in the foreground or background and obtains the caller object
"ohos.aafwk.param.callAbilityToForeground": true
}
};
try {
this.context.startAbilityByCall(wantForeground)
.then((obj) => {
// Carry out normal service processing.
caller = obj;
console.log('GetCaller success');
console.log('startAbilityByCall succeed');
}).catch((error) => {
console.log(`GetCaller failed with ${error}`);
// Process service logic errors.
console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.startAbilityWithAccount
......@@ -959,22 +1458,43 @@ Starts an ability with the account ID specified. This API uses an asynchronous c
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startAbilityWithAccount(want, accountId, (err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
try {
this.context.startAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -994,26 +1514,47 @@ Starts an ability with the account ID and start options specified. This API uses
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
this.context.startAbilityWithAccount(want, accountId, options, (err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
try {
this.context.startAbilityWithAccount(want, accountId, options, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -1033,29 +1574,46 @@ Starts an ability with the account ID specified. This API uses a promise to retu
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
try {
this.context.startAbilityWithAccount(want, accountId, options)
.then(() => {
console.log('---------- startAbilityWithAccount success -----------');
})
.catch((err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
.then((data) => {
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## AbilityContext.requestPermissionsFromUser
......@@ -1075,7 +1633,7 @@ Requests permissions from the user by displaying a dialog box. This API uses an
**Example**
```js
```ts
var permissions=['com.example.permission']
this.context.requestPermissionsFromUser(permissions,(result) => {
console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
......@@ -1106,7 +1664,7 @@ Requests permissions from the user by displaying a dialog box. This API uses a p
**Example**
```js
```ts
var permissions=['com.example.permission']
this.context.requestPermissionsFromUser(permissions).then((data) => {
console.log('success:' + JSON.stringify(data));
......@@ -1134,7 +1692,7 @@ Sets a label for this ability in the mission. This API uses an asynchronous call
**Example**
```js
```ts
this.context.setMissionLabel("test",(result) => {
console.log('requestPermissionsFromUserresult:' + JSON.stringify(result));
});
......@@ -1163,7 +1721,7 @@ Sets a label for this ability in the mission. This API uses a promise to return
**Example**
```js
```ts
this.context.setMissionLabel("test").then(() => {
console.log('success');
}).catch((error) => {
......@@ -1189,7 +1747,7 @@ Sets an icon for this ability in the mission. This API uses an asynchronous call
**Example**
```js
```ts
import image from '@ohos.multimedia.image';
var imagePixelMap;
var color = new ArrayBuffer(0);
......@@ -1236,7 +1794,7 @@ Sets an icon for this ability in the mission. This API uses a promise to return
**Example**
```js
```ts
import image from '@ohos.multimedia.image';
var imagePixelMap;
var color = new ArrayBuffer(0);
......@@ -1273,11 +1831,11 @@ Restores the window stage data for this ability.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| localStorage | image.LocalStorage | Yes| Storage used to store the restored window stage.|
| localStorage | LocalStorage | Yes| Storage used to store the restored window stage.|
**Example**
```js
```ts
var storage = new LocalStorage();
this.context.restoreWindowStage(storage);
```
......@@ -1298,7 +1856,7 @@ Checks whether this ability is in the terminating state.
**Example**
```js
```ts
var isTerminating = this.context.isTerminating();
console.log('ability state :' + isTerminating);
```
......@@ -46,7 +46,7 @@ Called to initialize the service logic when an ability is created.
**Example**
```js
```ts
class myAbility extends Ability {
onCreate(want, param) {
console.log('onCreate, want:' + want.abilityName);
......@@ -71,7 +71,7 @@ Called when a **WindowStage** is created for this ability.
**Example**
```js
```ts
class myAbility extends Ability {
onWindowStageCreate(windowStage) {
console.log('onWindowStageCreate');
......@@ -90,7 +90,7 @@ Called when the **WindowStage** is destroyed for this ability.
**Example**
```js
```ts
class myAbility extends Ability {
onWindowStageDestroy() {
console.log('onWindowStageDestroy');
......@@ -115,7 +115,7 @@ Called when the **WindowStage** is restored during the migration of this ability
**Example**
```js
```ts
class myAbility extends Ability {
onWindowStageRestore(windowStage) {
console.log('onWindowStageRestore');
......@@ -134,7 +134,7 @@ Called when this ability is destroyed to clear resources.
**Example**
```js
```ts
class myAbility extends Ability {
onDestroy() {
console.log('onDestroy');
......@@ -153,7 +153,7 @@ Called when this ability is switched from the background to the foreground.
**Example**
```js
```ts
class myAbility extends Ability {
onForeground() {
console.log('onForeground');
......@@ -172,7 +172,7 @@ Called when this ability is switched from the foreground to the background.
**Example**
```js
```ts
class myAbility extends Ability {
onBackground() {
console.log('onBackground');
......@@ -203,7 +203,7 @@ Called to save data during the ability migration preparation process.
**Example**
```js
```ts
import AbilityConstant from "@ohos.application.AbilityConstant"
class myAbility extends Ability {
onContinue(wantParams) {
......@@ -232,7 +232,7 @@ Called when the ability startup mode is set to singleton.
**Example**
```js
```ts
class myAbility extends Ability {
onNewWant(want) {
console.log('onNewWant, want:' + want.abilityName);
......@@ -257,7 +257,7 @@ Called when the configuration of the environment where the ability is running is
**Example**
```js
```ts
class myAbility extends Ability {
onConfigurationUpdated(config) {
console.log('onConfigurationUpdated, config:' + JSON.stringify(config));
......@@ -281,7 +281,7 @@ Dumps client information.
**Example**
```js
```ts
class myAbility extends Ability {
dump(params) {
console.log('dump, params:' + JSON.stringify(params));
......@@ -306,7 +306,7 @@ Called when the system has decided to adjust the memory level. For example, this
**Example**
```js
```ts
class myAbility extends Ability {
onMemoryLevel(level) {
console.log('onMemoryLevel, level:' + JSON.stringify(level));
......@@ -315,12 +315,47 @@ Called when the system has decided to adjust the memory level. For example, this
```
## Ability.onSaveState
onSaveState(reason: AbilityConstant.StateType, wantParam : {[key: string]: any}): AbilityConstant.OnSaveResult;
Called when the framework saves the ability state in the case of an application fault if automatic saving is enabled. This API is used together with [appRecovery](js-apis-app-ability-appRecovery.md).
**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| reason | [AbilityConstant.StateType](js-apis-application-abilityConstant.md#abilityconstantstatetype) | Yes| Reason for triggering the callback to save the ability state.|
| wantParam | {[key:&nbsp;string]:&nbsp;any} | Yes| **want** parameter.|
**Return value**
| Type| Description|
| -------- | -------- |
| AbilityConstant.OnSaveResult | Whether the ability state is saved.|
**Example**
```js
import AbilityConstant from '@ohos.application.AbilityConstant'
class myAbility extends Ability {
onSaveState(reason, wantParam) {
console.log('onSaveState');
wantParam["myData"] = "my1234567";
return AbilityConstant.OnSaveResult.RECOVERY_AGREE;
}
}
```
## Caller
Implements sending of sequenceable data to the target ability when an ability (caller ability) invokes the target ability (callee ability).
## Caller.call
call(method: string, data: rpc.Sequenceable): Promise&lt;void&gt;;
......@@ -342,10 +377,20 @@ Sends sequenceable data to the target ability.
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return a response.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 16200001 | Caller released. The caller has been released. |
| 16200002 | Callee invalid. The callee does not exist. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
class MyMessageAble{ // Custom sequenceable data structure
name:""
str:""
......@@ -377,19 +422,20 @@ Sends sequenceable data to the target ability.
deviceId: ""
}).then((obj) => {
caller = obj;
let msg = new MyMessageAble(1, "world"); // See the definition of Sequenceable.
let msg = new MyMessageAble("msg", "world"); // See the definition of Sequenceable.
caller.call(method, msg)
.then(() => {
console.log('Caller call() called');
}).catch((e) => {
console.log('Caller call() catch error ' + e);
})
.catch((callErr) => {
console.log('Caller.call catch error, error.code: ' + JSON.stringify(callErr.code) +
' error.message: ' + JSON.stringify(callErr.message));
});
console.log('Caller GetCaller Get ' + caller);
}).catch((e) => {
console.log('Caller GetCaller error ' + e);
}).catch((err) => {
console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
' error.message: ' + JSON.stringify(err.message));
});
}
}
```
......@@ -415,10 +461,20 @@ Sends sequenceable data to the target ability and obtains the sequenceable data
| -------- | -------- |
| Promise&lt;rpc.MessageParcel&gt; | Promise used to return the sequenceable data from the target ability.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 201 | The application does not have permission to call the interface. |
| 401 | Invalid input parameter. |
| 16200001 | Caller released. The caller has been released. |
| 16200002 | Callee invalid. The callee does not exist. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
class MyMessageAble{
name:""
str:""
......@@ -456,12 +512,14 @@ Sends sequenceable data to the target ability and obtains the sequenceable data
console.log('Caller callWithResult() called');
let retmsg = new MyMessageAble(0, "");
data.readSequenceable(retmsg);
}).catch((e) => {
console.log('Caller callWithResult() catch error ' + e);
})
.catch((callErr) => {
console.log('Caller.callWithResult catch error, error.code: ' + JSON.stringify(callErr.code) +
' error.message: ' + JSON.stringify(callErr.message));
});
console.log('Caller GetCaller Get ' + caller);
}).catch((e) => {
console.log('Caller GetCaller error ' + e);
}).catch((err) => {
console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
' error.message: ' + JSON.stringify(err.message));
});
}
}
......@@ -476,10 +534,19 @@ Releases the caller interface of the target ability.
**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200001 | Caller released. The caller has been released. |
| 16200002 | Callee invalid. The callee does not exist. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
var caller;
export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
......@@ -491,21 +558,22 @@ Releases the caller interface of the target ability.
caller = obj;
try {
caller.release();
} catch (e) {
console.log('Caller Release error ' + e);
} catch (releaseErr) {
console.log('Caller.release catch error, error.code: ' + JSON.stringify(releaseErr.code) +
' error.message: ' + JSON.stringify(releaseErr.message));
}
console.log('Caller GetCaller Get ' + caller);
}).catch((e) => {
console.log('Caller GetCaller error ' + e);
}).catch((err) => {
console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
' error.message: ' + JSON.stringify(err.message));
});
}
}
```
## Caller.onRelease
## Caller.on
onRelease(callback: OnReleaseCallBack): void;
on(type: "release", callback: OnReleaseCallback): void;
Registers a callback that is invoked when the stub on the target ability is disconnected.
......@@ -515,12 +583,21 @@ Registers a callback that is invoked when the stub on the target ability is disc
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Event type. The value is fixed at **release**.|
| callback | OnReleaseCallBack | Yes| Callback used for the **onRelease** API.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200001 | Caller released. The caller has been released. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
var caller;
export default class MainAbility extends Ability {
onWindowStageCreate(windowStage) {
......@@ -531,15 +608,16 @@ Registers a callback that is invoked when the stub on the target ability is disc
}).then((obj) => {
caller = obj;
try {
caller.onRelease((str) => {
caller.on("release", (str) => {
console.log(' Caller OnRelease CallBack is called ' + str);
});
} catch (e) {
console.log('Caller Release error ' + e);
} catch (error) {
console.log('Caller.on catch error, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
}
console.log('Caller GetCaller Get ' + caller);
}).catch((e) => {
console.log('Caller GetCaller error ' + e);
}).catch((err) => {
console.log('Caller GetCaller error, error.code: ' + JSON.stringify(err.code) +
' error.message: ' + JSON.stringify(err.message));
});
}
}
......@@ -550,7 +628,6 @@ Registers a callback that is invoked when the stub on the target ability is disc
Implements callbacks for caller notification registration and deregistration.
## Callee.on
on(method: string, callback: CalleeCallBack): void;
......@@ -566,10 +643,18 @@ Registers a caller notification callback, which is invoked when the target abili
| method | string | Yes| Notification message string negotiated between the two abilities.|
| callback | CalleeCallBack | Yes| JS notification synchronization callback of the **rpc.MessageParcel** type. The callback must return at least one empty **rpc.Sequenceable** object. Otherwise, the function execution fails.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200004 | Method registered. The method has registered. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
class MyMessageAble{
name:""
str:""
......@@ -601,12 +686,16 @@ Registers a caller notification callback, which is invoked when the target abili
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
console.log('Callee onCreate is called');
try {
this.callee.on(method, funcCallBack);
} catch (error) {
console.log('Callee.on catch error, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
}
}
}
```
## Callee.off
off(method: string): void;
......@@ -621,20 +710,34 @@ Deregisters a caller notification callback, which is invoked when the target abi
| -------- | -------- | -------- | -------- |
| method | string | Yes| Registered notification message string.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| 16200005 | Method not registered. The method has not registered. |
| 16000050 | Internal Error. |
**Example**
```js
import Ability from '@ohos.application.Ability';
```ts
import Ability from '@ohos.app.ability.UIAbility';
var method = 'call_Function';
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
console.log('Callee onCreate is called');
try {
this.callee.off(method);
} catch (error) {
console.log('Callee.off catch error, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
}
}
}
```
## OnReleaseCallBack
## OnReleaseCallback
(msg: string): void;
......@@ -644,7 +747,7 @@ Deregisters a caller notification callback, which is invoked when the target abi
| -------- | -------- | -------- | -------- | -------- |
| (msg: string) | function | Yes| No| Prototype of the listener function registered by the caller.|
## CalleeCallBack
## CalleeCallback
(indata: rpc.MessageParcel): rpc.Sequenceable;
......@@ -653,3 +756,5 @@ Deregisters a caller notification callback, which is invoked when the target abi
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| (indata: rpc.MessageParcel) | rpc.Sequenceable | Yes| No| Prototype of the listener function registered by the callee.|
<!--no_check-->
\ No newline at end of file
......@@ -17,7 +17,7 @@ This module provides APIs for accessing resources of a specific Extension abilit
| -------- | -------- | -------- | -------- | -------- |
| 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.)|
| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundleManager-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**.
......@@ -31,7 +31,7 @@ To adapt to devices with different performance, an application provides three mo
Define a **ServiceExtension** with the same name for the three modules.
``` js
import ServiceExtension from '@ohos.application.ServiceExtensionAbility'
import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'
import Want from '@ohos.application.Want'
export default class TheServiceExtension extends ServiceExtension {
onCreate(want:Want) {
......@@ -61,7 +61,7 @@ export default class TheServiceExtension extends ServiceExtension {
Start **ServiceExtension** within the **onCreate** callback of the main ability of the entry.
``` js
import Ability from '@ohos.application.Ability'
import Ability from '@ohos.app.ability.Ability'
export default class MainAbility extends Ability {
onCreate(want, launchParam) {
console.log("[Demo] MainAbility onCreate");
......
......@@ -13,16 +13,18 @@ You can use the APIs of this module to start, terminate, connect, and disconnect
Before using the **ServiceExtensionContext** module, you must define a child class that inherits from **ServiceExtensionAbility**.
```js
import ServiceExtensionAbility from '@ohos.application.ServiceExtensionAbility';
```ts
import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
let context = undefined;
class MainAbility extends ServiceExtensionAbility {
onCreate() {
let context = this.context;
context = this.context;
}
}
```
## startAbility
## ServiceExtensionContext.startAbility
startAbility(want: Want, callback: AsyncCallback&lt;void&gt;): void;
......@@ -36,21 +38,43 @@ Starts an ability. This API uses an asynchronous callback to return the result.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability, such as the ability name and bundle name.|
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let want = {
"bundleName": "com.example.myapp",
"abilityName": "MyAbility"};
this.context.startAbility(want, (err) => {
console.log('startAbility result:' + JSON.stringify(err));
```ts
var want = {
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
try {
this.context.startAbility(want, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## startAbility
## ServiceExtensionContext.startAbility
startAbility(want: Want, options?: StartOptions): Promise\<void>;
......@@ -64,8 +88,8 @@ Starts an ability. This API uses a promise to return the result.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability, such as the ability name and bundle name.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
**Return value**
......@@ -73,26 +97,47 @@ Starts an ability. This API uses a promise to return the result.
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let want = {
"bundleName": "com.example.myapp",
"abilityName": "MyAbility"
```ts
var want = {
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
var options = {
windowMode: 0,
};
this.context.startAbility(want).then((data) => {
console.log('success:' + JSON.stringify(data));
}).catch((error) => {
console.log('failed:' + JSON.stringify(error));
});
try {
this.context.startAbility(want, options)
.then((data) => {
// Carry out normal service processing.
console.log('startAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## startAbility
## ServiceExtensionContext.startAbility
startAbility(want: Want, options: StartOptions, callback: AsyncCallback&lt;void&gt;): void
Starts an ability. 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
......@@ -102,24 +147,45 @@ Starts an ability. This API uses an asynchronous callback to return the result.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var options = {
windowMode: 0,
windowMode: 0
};
try {
this.context.startAbility(want, options, (error) => {
console.log("error.code = " + error.code)
})
if (error.code) {
// Process service logic errors.
console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startAbilityWithAccount
......@@ -136,30 +202,50 @@ Starts an ability with the account ID specified. This API uses an asynchronous c
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startAbilityWithAccount(want, accountId, (err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
try {
this.context.startAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startAbilityWithAccount
startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void;
Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result.
Starts an ability with the account ID and start options specified. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
......@@ -169,26 +255,47 @@ Starts an ability with the account ID specified. This API uses an asynchronous c
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
| options | [StartOptions](js-apis-application-StartOptions.md) | Yes| Parameters used for starting the ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
this.context.startAbilityWithAccount(want, accountId, options, (err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
try {
this.context.startAbilityWithAccount(want, accountId, options, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
......@@ -206,7 +313,7 @@ Starts an ability with the account ID specified. This API uses a promise to retu
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | [StartOptions](js-apis-application-StartOptions.md) | No| Parameters used for starting the ability.|
......@@ -216,25 +323,42 @@ Starts an ability with the account ID specified. This API uses a promise to retu
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
windowMode: 0,
windowMode: 0
};
try {
this.context.startAbilityWithAccount(want, accountId, options)
.then((data) => {
console.log('---------- startAbilityWithAccount success, data: -----------', data);
})
.catch((err) => {
console.log('---------- startAbilityWithAccount fail, err: -----------', err);
// Carry out normal service processing.
console.log('startAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startServiceExtensionAbility
......@@ -251,20 +375,41 @@ Starts a new Service Extension ability. This API uses an asynchronous callback t
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
this.context.startServiceExtensionAbility(want, (err) => {
console.log('---------- startServiceExtensionAbility fail, err: -----------', err);
try {
this.context.startServiceExtensionAbility(want, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startServiceExtensionAbility
......@@ -281,7 +426,7 @@ Starts a new Service Extension ability. This API uses a promise to return the re
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
**Return value**
......@@ -289,21 +434,38 @@ Starts a new Service Extension ability. This API uses a promise to return the re
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
try {
this.context.startServiceExtensionAbility(want)
.then((data) => {
console.log('---------- startServiceExtensionAbility success, data: -----------', data);
})
.catch((err) => {
console.log('---------- startServiceExtensionAbility fail, err: -----------', err);
// Carry out normal service processing.
console.log('startServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
......@@ -322,22 +484,44 @@ Starts a new Service Extension ability with the account ID specified. This API u
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startServiceExtensionAbilityWithAccount(want,accountId, (err) => {
console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('startServiceExtensionAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startServiceExtensionAbilityWithAccount
......@@ -356,7 +540,7 @@ Starts a new Service Extension ability with the account ID specified. This API u
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
**Return value**
......@@ -365,22 +549,39 @@ Starts a new Service Extension ability with the account ID specified. This API u
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.startServiceExtensionAbilityWithAccount(want,accountId)
try {
this.context.startServiceExtensionAbilityWithAccount(want, accountId)
.then((data) => {
console.log('---------- startServiceExtensionAbilityWithAccount success, data: -----------', data);
})
.catch((err) => {
console.log('---------- startServiceExtensionAbilityWithAccount fail, err: -----------', err);
// Carry out normal service processing.
console.log('startServiceExtensionAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.stopServiceExtensionAbility
......@@ -397,20 +598,41 @@ Stops a Service Extension ability in the same application. This API uses an asyn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
this.context.stopServiceExtensionAbility(want, (err) => {
console.log('---------- stopServiceExtensionAbility fail, err: -----------', err);
try {
this.context.stopServiceExtensionAbility(want, (error) => {
if (error.code) {
// Process service logic errors.
console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('stopServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.stopServiceExtensionAbility
......@@ -427,7 +649,7 @@ Stops a Service Extension ability in the same application. This API uses a promi
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
**Return value**
......@@ -435,21 +657,38 @@ Stops a Service Extension ability in the same application. This API uses a promi
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
try {
this.context.stopServiceExtensionAbility(want)
.then((data) => {
console.log('---------- stopServiceExtensionAbility success, data: -----------', data);
})
.catch((err) => {
console.log('---------- stopServiceExtensionAbility fail, err: -----------', err);
// Carry out normal service processing.
console.log('stopServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
......@@ -468,22 +707,43 @@ Stops a Service Extension ability in the same application with the account ID sp
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.stopServiceExtensionAbilityWithAccount(want,accountId, (err) => {
console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err);
try {
this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => {
if (error.code) {
// Process service logic errors.
console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('stopServiceExtensionAbilityWithAccount succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.stopServiceExtensionAbilityWithAccount
......@@ -502,7 +762,7 @@ Stops a Service Extension ability in the same application with the account ID sp
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
**Return value**
......@@ -511,22 +771,39 @@ Stops a Service Extension ability in the same application with the account ID sp
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
this.context.stopServiceExtensionAbilityWithAccount(want,accountId)
try {
this.context.stopServiceExtensionAbilityWithAccount(want, accountId)
.then((data) => {
console.log('---------- stopServiceExtensionAbilityWithAccount success, data: -----------', data);
})
.catch((err) => {
console.log('---------- stopServiceExtensionAbilityWithAccount fail, err: -----------', err);
// Carry out normal service processing.
console.log('stopServiceExtensionAbilityWithAccount succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.terminateSelf
......@@ -543,13 +820,27 @@ Terminates this ability. This API uses an asynchronous callback to return the re
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
this.context.terminateSelf((err) => {
console.log('terminateSelf result:' + JSON.stringify(err));
```ts
this.context.terminateSelf((error) => {
if (error.code) {
// Process service logic errors.
console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('terminateSelf succeed');
});
```
......@@ -569,19 +860,29 @@ Terminates this ability. This API uses a promise to return the result.
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
this.context.terminateSelf().then((data) => {
console.log('success:' + JSON.stringify(data));
// Carry out normal service processing.
console.log('terminateSelf succeed');
}).catch((error) => {
console.log('failed:' + JSON.stringify(error));
// Process service logic errors.
console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
```
## ServiceExtensionContext.connectAbility
## ServiceExtensionContext.connectServiceExtensionAbility
connectAbility(want: Want, options: ConnectOptions): number;
connectServiceExtensionAbility(want: Want, options: ConnectOptions): number;
Connects this ability to a Service ability.
......@@ -593,7 +894,7 @@ Connects this ability to a Service ability.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability, such as the ability name and bundle name.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability, such as the ability name and bundle name.|
| options | [ConnectOptions](js-apis-featureAbility.md#connectoptions) | Yes| Callback used to return the information indicating that the connection is successful, interrupted, or failed.|
**Return value**
......@@ -602,24 +903,39 @@ Connects this ability to a Service ability.
| -------- | -------- |
| number | A number, based on which the connection will be interrupted.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let want = {
"bundleName": "com.example.myapp",
"abilityName": "MyAbility"
```ts
var want = {
bundleName: "com.example.myapp",
abilityName: "MyAbility"
};
let options = {
var options = {
onConnect(elementName, remote) { console.log('----------- onConnect -----------') },
onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
onFailed(code) { console.log('----------- onFailed -----------') }
}
let connection = this.context.connectAbility(want,options);
var connection = null;
try {
connection = this.context.connectServiceExtensionAbility(want, options);
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.connectAbilityWithAccount
## ServiceExtensionContext.connectServiceExtensionAbilityWithAccount
connectAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number;
Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect this ability to another ability.
......@@ -631,9 +947,9 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| want | [Want](js-apis-application-Want.md) | Yes| Information about the target ability.|
| want | [Want](js-apis-application-Want.md) | Yes| Want information about the target ability.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| options | ConnectOptions | No| Remote object instance.|
| options | ConnectOptions | Yes| Remote object instance.|
**Return value**
......@@ -641,13 +957,20 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
| -------- | -------- |
| number | Result code of the ability connection.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
```ts
var want = {
"deviceId": "",
"bundleName": "com.extreme.test",
"abilityName": "MainAbility"
deviceId: "",
bundleName: "com.extreme.test",
abilityName: "MainAbility"
};
var accountId = 100;
var options = {
......@@ -655,13 +978,20 @@ Uses the **AbilityInfo.AbilityType.SERVICE** template and account ID to connect
onDisconnect(elementName) { console.log('----------- onDisconnect -----------') },
onFailed(code) { console.log('----------- onFailed -----------') }
}
const result = this.context.connectAbilityWithAccount(want, accountId, options);
console.log('----------- connectAbilityResult: ------------', result);
var connection = null;
try {
connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options);
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.disconnectAbility
## ServiceExtensionContext.disconnectServiceExtensionAbility
disconnectAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback&lt;void&gt;): void;
Disconnects this ability from the Service ability. This API uses an asynchronous callback to return the result.
......@@ -674,21 +1004,42 @@ Disconnects this ability from the Service ability. This API uses an asynchronous
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| connection | number | Yes| Number returned after **connectAbility** is called.|
| callback | AsyncCallback&lt;void&gt; | No| Callback used to return the result.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let connection=1
this.context.disconnectAbility(connection, (err) => {
// connection is the return value of connectAbility.
console.log('terminateSelf result:' + JSON.stringify(err));
```ts
// connection is the return value of connectServiceExtensionAbility.
var connection = 1;
try {
this.context.disconnectServiceExtensionAbility(connection, (error) => {
if (error.code) {
// Process service logic errors.
console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
return;
}
// Carry out normal service processing.
console.log('disconnectServiceExtensionAbility succeed');
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.disconnectAbility
## ServiceExtensionContext.disconnectServiceExtensionAbility
disconnectAbility(connection: number): Promise&lt;void&gt;;
disconnectServiceExtensionAbility(connection: number): Promise&lt;void&gt;;
Disconnects this ability from the Service ability. This API uses a promise to return the result.
......@@ -708,16 +1059,35 @@ Disconnects this ability from the Service ability. This API uses a promise to re
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let connection=1
this.context.disconnectAbility(connection).then((data) => {
```ts
// connection is the return value of connectAbility.
console.log('success:' + JSON.stringify(data));
}).catch((error) => {
console.log('failed:' + JSON.stringify(error));
var connection = 1;
try {
this.context.disconnectServiceExtensionAbility(connection)
.then((data) => {
// Carry out normal service processing.
console.log('disconnectServiceExtensionAbility succeed');
})
.catch((error) => {
// Process service logic errors.
console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
## ServiceExtensionContext.startAbilityByCall
......@@ -742,10 +1112,19 @@ Starts an ability in the foreground or background and obtains the caller object
| -------- | -------- |
| Promise&lt;Caller&gt; | Promise used to return the caller object to communicate with.|
**Error codes**
| ID| Error Message|
| ------- | -------------------------------- |
| 401 | Invalid input parameter. |
| Other IDs| See [Ability Error Codes](../errorcodes/errorcode-ability.md).|
**Example**
```js
let caller = undefined;
Start an ability in the background.
```ts
var caller = undefined;
// Start an ability in the background by not passing parameters.
var wantBackground = {
......@@ -754,13 +1133,29 @@ Starts an ability in the foreground or background and obtains the caller object
abilityName: "MainAbility",
deviceId: ""
};
try {
this.context.startAbilityByCall(wantBackground)
.then((obj) => {
// Carry out normal service processing.
caller = obj;
console.log('GetCaller Success');
console.log('startAbilityByCall succeed');
}).catch((error) => {
console.log(`GetCaller failed with ${error}`);
// Process service logic errors.
console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
Start an ability in the foreground.
```ts
var caller = undefined;
// Start an ability in the foreground with ohos.aafwk.param.callAbilityToForeground in parameters set to true.
var wantForeground = {
......@@ -772,12 +1167,22 @@ Starts an ability in the foreground or background and obtains the caller object
"ohos.aafwk.param.callAbilityToForeground": true
}
};
try {
this.context.startAbilityByCall(wantForeground)
.then((obj) => {
// Carry out normal service processing.
caller = obj;
console.log('GetCaller success');
console.log('startAbilityByCall succeed');
}).catch((error) => {
console.log(`GetCaller failed with ${error}`);
// Process service logic errors.
console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) +
' error.message: ' + JSON.stringify(error.message));
});
} catch (paramError) {
// Process input parameter errors.
console.log('error.code: ' + JSON.stringify(paramError.code) +
' error.message: ' + JSON.stringify(paramError.message));
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册