# Multi-device Collaboration (for System Applications Only)
## When to Use
...
...
@@ -319,10 +319,10 @@ The following describes how to implement multi-device collaboration through cros
| startAbilityByCall(want: Want): Promise<Caller>; | Starts a UIAbility in the foreground or background and obtains the caller object for communicating with the UIAbility.|
| 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.|
| call(method: string, data: rpc.Parcelable): Promise<void> | Sends agreed parcelable data to the callee ability.|
| callWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence>| Sends agreed parcelable data to the callee ability and obtains the agreed parcelable data returned by the callee ability.|
| release(): void | Releases the caller object.|
| on(type: "release", callback: OnReleaseCallback): void | Callback invoked when the caller object is released.|
| on(type: "release", callback: OnReleaseCallback): void | Callback invoked when the caller object is released.|
### How to Develop
...
...
@@ -348,16 +348,15 @@ The following describes how to implement multi-device collaboration through cros
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 launch type of the UIAbility.
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**.|
| JSON Field| Description|
| -------- | -------- |
| "launchType"| Ability launch type. Set this parameter to **singleton**.|
An example of the UIAbility configuration is as follows:
An example of the UIAbility configuration is as follows:
```json
"abilities":[{
"name": ".CalleeAbility",
...
...
@@ -369,19 +368,18 @@ The following describes how to implement multi-device collaboration through cros
"visible": true
}]
```
2. Import the **UIAbility** module.
```ts
importAbilityfrom'@ohos.app.ability.UIAbility';
```
3. Define the agreed sequenceable data.
The data formats sent and received by the caller and callee abilities must be consistent. In the following example, the data formats are number and string.
3. Define the agreed parcelable data.
The data formats sent and received by the caller and callee abilities must be consistent. In the following example, the data formats are number and string.
```ts
exportdefaultclassMySequenceable{
export default class MyParcelable {
num: number = 0;
str: string = "";
...
...
@@ -390,71 +388,69 @@ The following describes how to implement multi-device collaboration through cros
this.str = string;
}
marshalling(messageParcel){
messageParcel.writeInt(this.num);
messageParcel.writeString(this.str);
marshalling(messageSequence) {
messageSequence.writeInt(this.num);
messageSequence.writeString(this.str);
return true;
}
unmarshalling(messageParcel){
this.num=messageParcel.readInt();
this.str=messageParcel.readString();
unmarshalling(messageSequence) {
this.num = messageSequence.readInt();
this.str = messageSequence.readString();
return true;
}
}
```
4. Implement **Callee.on** and **Callee.off**.
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.
```ts
constTAG:string='[CalleeAbility]';
constMSG_SEND_METHOD:string='CallSendMsg';
functionsendMsgCallback(data){
console.info('CalleeSortFunc called');
// Obtain the sequenceable data sent by the caller ability.
console.info(`${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`);
}
}
In the following example, the **MSG_SEND_METHOD** listener is registered in **onCreate()** of the ability and deregistered in **onDestroy()**. After receiving parcelable data, the application processes the data and returns the data result. You need to implement processing based on service requirements.
onDestroy(){
try{
this.callee.off(MSG_SEND_METHOD);
}catch(error){
console.error(TAG,`${MSG_SEND_METHOD} unregister failed with error ${JSON.stringify(error)}`);
}
}
}
```
```ts
const TAG: string = '[CalleeAbility]';
const MSG_SEND_METHOD: string = 'CallSendMsg';
function sendMsgCallback(data) {
console.info('CalleeSortFunc called');
// Obtain the parcelable data sent by the caller ability.
// Return the parcelable data result to the caller ability.
return new MyParcelable(receivedData.num + 1, `send ${receivedData.str} succeed`);
}
export default class CalleeAbility extends Ability {
onCreate(want, launchParam) {
try {
this.callee.on(MSG_SEND_METHOD, sendMsgCallback);
} catch (error) {
console.info(`${MSG_SEND_METHOD} register failed with error ${JSON.stringify(error)}`);
}
}
onDestroy() {
try {
this.callee.off(MSG_SEND_METHOD);
} catch (error) {
console.error(TAG, `${MSG_SEND_METHOD} unregister failed with error ${JSON.stringify(error)}`);
}
}
}
```
4. Obtain the caller object and access the callee ability.
1. Import the **UIAbility** module.
```ts
importAbilityfrom'@ohos.app.ability.UIAbility';
```
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.
```ts
async onButtonGetRemoteCaller() {
...
...
@@ -483,14 +479,14 @@ The following describes how to implement multi-device collaboration through cros
For details about how to implement **getRemoteDeviceId()**, see [Starting UIAbility and ServiceExtensionAbility Across Devices (No Data Returned)](#starting-uiability-and-serviceextensionability-across-devices-no-data-returned).
5. Sends agreed sequenceable data to the callee ability.
1. The sequenceable data can be sent to the callee ability with or without a return value. The method and sequenceable data must be consistent with those of the callee ability. The following example describes how to send data to the callee ability.
5. Sends agreed parcelable data to the callee ability.
1. The parcelable data can be sent to the callee ability with or without a return value. The method and parcelable data must be consistent with those of the callee ability. The following example describes how to send data to the callee ability.
```ts
constMSG_SEND_METHOD:string='CallSendMsg';
asynconButtonCall(){
try{
letmsg=newMySequenceable(1,'origin_Msg');
letmsg=newMyParcelable(1,'origin_Msg');
awaitthis.caller.call(MSG_SEND_METHOD,msg);
}catch(error){
console.info(`caller call failed with ${error}`);
...
...
@@ -505,12 +501,12 @@ The following describes how to implement multi-device collaboration through cros
@@ -488,7 +488,7 @@ Ability call is usually used in the following scenarios:
- Starting the callee ability in the background
**Table 1** Terms used in the ability call
**Table 1** Terms used in the ability call
| **Term**| Description|
| -------- | -------- |
...
...
@@ -519,15 +519,15 @@ The following figure shows the ability call process.
The following table describes the main APIs used for the ability call. For details, see [AbilityContext](../reference/apis/js-apis-app-ability-uiAbility.md#caller).
**Table 2** Ability call APIs
**Table 2** Ability call APIs
| API| Description|
| -------- | -------- |
| startAbilityByCall(want: Want): Promise<Caller> | Starts a UIAbility in the foreground (through the **want** configuration) or background (default) and obtains the caller object for communication with the UIAbility. For details, see [AbilityContext](../reference/apis/js-apis-inner-application-uiAbilityContext.md#abilitycontextstartabilitybycall) or [ServiceExtensionContext](../reference/apis/js-apis-inner-application-serviceExtensionContext.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.|
| call(method: string, data: rpc.Parcelable): Promise<void> | Sends agreed parcelable data to the callee ability.|
| callWithResult(method: string, data: rpc.Parcelable): Promise<rpc.MessageSequence> | Sends agreed parcelable data to the callee ability and obtains the agreed parcelable data returned by the callee ability.|
| release(): void | Releases the caller object.|
| on(type: "release", callback: OnReleaseCallback): void | Callback invoked when the caller object is released.|
...
...
@@ -571,13 +571,13 @@ For the callee ability, implement the callback to receive data and the methods t
importAbilityfrom'@ohos.app.ability.UIAbility';
```
3. Define the agreed sequenceable data.
3. Define the agreed parcelable data.
The data formats sent and received by the caller and callee abilities must be consistent. In the following example, the data formats are number and string.
```ts
exportdefaultclassMySequenceable{
exportdefaultclassMyParcelable{
num:number=0
str:string=""
...
...
@@ -586,15 +586,15 @@ For the callee ability, implement the callback to receive data and the methods t
this.str=string
}
marshalling(messageParcel){
messageParcel.writeInt(this.num)
messageParcel.writeString(this.str)
marshalling(messageSequence){
messageSequence.writeInt(this.num)
messageSequence.writeString(this.str)
returntrue
}
unmarshalling(messageParcel){
this.num=messageParcel.readInt()
this.str=messageParcel.readString()
unmarshalling(messageSequence){
this.num=messageSequence.readInt()
this.str=messageSequence.readString()
returntrue
}
}
...
...
@@ -602,7 +602,7 @@ For the callee ability, implement the callback to receive data and the methods t
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 sample code 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 parcelable data, the application processes the data and returns the data result. You need to implement processing based on service requirements. The sample code is as follows:
```ts
constTAG:string='[CalleeAbility]';
constMSG_SEND_METHOD:string='CallSendMsg';
...
...
@@ -610,14 +610,14 @@ For the callee ability, implement the callback to receive data and the methods t
functionsendMsgCallback(data){
console.info('CalleeSortFunc called');
// Obtain the sequenceable data sent by the caller ability.
letreceivedData=newMySequenceable(0,'');
data.readSequenceable(receivedData);
// Obtain the parcelable data sent by the caller ability.
@@ -319,7 +319,7 @@ Sends sequenceable data to the target ability.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| method | string | Yes| Notification message string negotiated between the two abilities. The message is used to instruct the callee to register a function to receive the sequenceable data.|
| data | [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Yes| Sequenceable data. You need to customize the data.|
| data | [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Yes| Parcelable data. You need to customize the data.|
**Return value**
...
...
@@ -338,7 +338,7 @@ For details about the error codes, see [Ability Error Codes](../errorcodes/error
**Example**
```ts
classMyMessageAble{// Custom sequenceable data structure.
classMyMessageAble{// Custom parcelable data structure.
name:''
str:''
num:1
...
...
@@ -346,15 +346,15 @@ For details about the error codes, see [Ability Error Codes](../errorcodes/error
Sends sequenceable data to the target ability and obtains the sequenceable data returned by the target ability.
...
...
@@ -398,13 +398,13 @@ Sends sequenceable data to the target ability and obtains the sequenceable data
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| method | string | Yes| Notification message string negotiated between the two abilities. The message is used to instruct the callee to register a function to receive the sequenceable data.|
| data | [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Yes| Sequenceable data. You need to customize the data.|
| data | [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Yes| Parcelable data. You need to customize the data.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<[rpc.MessageParcel](js-apis-rpc.md#sequenceabledeprecated)> | Promise used to return the sequenceable data from the target ability.|
| Promise<[rpc.MessageSequence](js-apis-rpc.md#messagesequence9)> | Promise used to return the sequenceable data from the target ability.|
**Error codes**
...
...
@@ -425,15 +425,15 @@ For details about the error codes, see [Ability Error Codes](../errorcodes/error
@@ -712,7 +712,7 @@ Registers a caller notification callback, which is invoked when the target abili
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| method | string | Yes| Notification message string negotiated between the two abilities.|
| callback | [CalleeCallback](#calleecallback) | Yes| JS notification synchronization callback of the [rpc.MessageParcel](js-apis-rpc.md#messageparceldeprecated) type. The callback must return at least one empty [rpc.Sequenceable](js-apis-rpc.md#sequenceabledeprecated) object. Otherwise, the function execution fails.|
| callback | [CalleeCallback](#calleecallback) | Yes| JS notification synchronization callback of the [rpc.MessageSequence](js-apis-rpc.md#messagesequence9) type. The callback must return at least one empty [rpc.Parcelable](js-apis-rpc.md#parcelable9) object. Otherwise, the function execution fails.|
**Error codes**
...
...
@@ -733,15 +733,15 @@ For details about the error codes, see [Ability Error Codes](../errorcodes/error
| (indata: [rpc.MessageParcel](js-apis-rpc.md#sequenceabledeprecated)) | Yes| No| [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Prototype of the listener function registered by the callee.|
| (indata: [rpc.MessageSequence](js-apis-rpc.md#messagesequence9)) | Yes| No| [rpc.Parcelable](js-apis-rpc.md#parcelable9) | Prototype of the listener function registered by the callee.|
@@ -106,3 +106,163 @@ let context: common.UIAbilityContext = globalThis.abilityContext;
letappContext=context.getApplicationContext();
appContext.getRunningProcessInformation()
```
## cl.ability.4 WantConstant.Flags API Change
**WantConstant.Flags** has multiple invalid flag definitions. These invalid flags are deleted.
**Change Impact**
JS APIs in API version 9 are affected. Your application needs to adapt these APIs so that it can properly implement features in the SDK environment of the new version.
**Key API/Component Changes**
| Module | Class | Method/Attribute/Enum/Constant | Change Type|
**WantConstant.Action** has multiple invalid action definitions. These invalid actions are deleted.
**Change Impact**
JS APIs in API version 9 are affected. Your application needs to adapt these APIs so that it can properly implement features in the SDK environment of the new version.
**Key API/Component Changes**
| Module | Class | Method/Attribute/Enum/Constant | Change Type|
Caller APIs use the **Parcelable** and **MessageSequence** objects provided by RPC in API version 9 to replace the deprecated **Sequenceable** and **MessageParcel** object.
**Change Impact**
JS APIs in API version 9 are affected. Your application needs to adapt these APIs so that it can properly implement features in the SDK environment of the new version.
**Key API/Component Changes**
| Module | Class | Method/Attribute/Enum/Constant | Change Type|