data-share-via-want.md 5.5 KB
Newer Older
G
Gloria 已提交
1 2 3 4
# Using Want to Share Data Between Applications

Users often need to share data (such as a text or an image) from one application to another. The following uses PDF file sharing as an example to describe how to use Want to share data between applications.

5
Data sharing requires two UIAbility components (one for the sharing party and the other for the shared party) and one system component (used as the application sharing box). When the sharing party initiates data sharing by calling **startAbility()**, the system implicitly matches and displays all applications that support the type of data to share. After the user selects an application, the system starts the application to complete data sharing.
G
Gloria 已提交
6

7 8 9 10
In this section, data sharing is triggered by touching a button. You can use other ways to trigger data sharing during application development. This section focuses on how to configure Want to implement data sharing.

The following actions are involved for data sharing:

11
- **ohos.want.action.select**: action of starting the application sharing box.
12 13 14 15
- **ohos.want.action.sendData**: action of sending a single data record, that is, transferring data to the shared party.

## Sharing Party

16
The sharing party starts an application sharing box and transfers the data to the shared party. Therefore, Want of the sharing party must be nested at two layers. In the first layer, implicit Want is used together with the **ohos.want.action.select** action to display the application sharing box. In the second layer, the data to share is declared
17

18
in the custom field **parameters**, and then the Want that includes the **ohos.want.action.sendData** action and the **parameters** field is transferred to the application sharing box. The shared party obtains the shared data from **parameters**.
19 20 21 22 23 24 25 26 27 28 29 30

```ts
import common from '@ohos.app.ability.common';

let fileType = 'application/pdf';
let fileName = 'TestFile.pdf';
let fileFd = -1; // Obtain the file descriptor (FD) of the file to share.
let fileSize; // Obtain the size of the file to share.

function implicitStartAbility() {
  let context = getContext(this) as common.UIAbilityContext;
  let wantInfo = {
31
    / This action is used to implicitly match the application sharing box.
32 33
    action: 'ohos.want.action.select',
    // This is the custom parameter in the first layer of Want,
34
    / which is intended to add information to the application sharing box.
35 36
    parameters: {
      // MIME type of PDF.
37 38 39
      'ability.picker.type': fileType,
      'ability.picker.fileNames': [fileName],
      'ability.picker.fileSizes': [fileSize],
40
      // This is nested Want ,which will be directly sent to the selected application.
41 42 43 44 45
      'ability.want.params.INTENT': {
        'action': 'ohos.want.action.sendData',
        'type': 'application/pdf',
        'parameters': {
          'keyFd': { 'type': 'FD', 'value': fileFd }
46
        }
G
Gloria 已提交
47
      }
48 49 50 51 52 53 54 55 56 57 58 59 60 61
    }
  }
  context.startAbility(wantInfo).then(() => {
    // ...
  }).catch((err) => {
    // ...
  })
}
```

> **NOTE**
>
> Data sharing can be implemented only in FD format. For details about how to obtain the FD and file name, see [File Management](../reference/apis/js-apis-file-fs.md).

62
In the preceding code, under the custom field **parameters**, the following **ability.picker.*** fields are used to pass the information to be displayed on the application sharing box:
63 64 65 66 67 68

- **ability.picker.type**: file type icon.
- **ability.picker.fileNames**: file name.
- **ability.picker.fileSizes**: file size, in bytes.
- **ability.picker.fileNames** and **ability.picker.fileSizes** are arrays and have a one-to-one mapping.

69 70
The following figure shows an example. 

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
![](figures/ability-startup-with-implicit-want2.png)

## Shared Party

To enable the shared party to identify the shared content, configure **skills** in the [module.json5 file](../quick-start/module-configuration-file.md) of the UIAbility of the shared party. The **actions** and **type** fields in **uris** match the **action** and **type** fields in **ability.want.params.INTENT** of the sharing party, respectively.

```json
{
  "module": {
    // ...
    "abilities": [
      {
        // ...
        "skills": [
          {
G
Gloria 已提交
86
            // ...
87 88
            "actions": [
              "action.system.home",
G
Gloria 已提交
89 90
              "ohos.want.action.sendData"
              // ...
91 92 93 94 95 96 97 98
            ],
            "uris": [
              {
                "type": "application/pdf"
              },
            ]
          }
        ]
G
Gloria 已提交
99
      }
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    ]
  }
}
```

After the user selects an application, the Want nested in the **ability.want.params.INTENT** field is passed to that application. The UIAbility of the shared party, after being started, can call [onCreate()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityoncreate) or [onNewWant()](../reference/apis/js-apis-app-ability-uiAbility.md#uiabilityonnewwant) to obtain the passed Want.

The following is an example of the Want obtained. You can use the FD of the shared file to perform required operations.

```json
{
    "deviceId": "",
    "bundleName": "com.example.myapplication",
    "abilityName": "EntryAbility",
    "moduleName": "entry",
    "uri": "",
    "type": "application/pdf",
    "flags": 0,
    "action": "ohos.want.action.sendData",
    "parameters": {
        "component.startup.newRules": true,
        "keyFd": {
            "type": "FD",
            "value": 36
        },
        "mime-type": "application/pdf",
        "moduleName": "entry",
        "ohos.aafwk.param.callerPid": 3488,
        "ohos.aafwk.param.callerToken": 537379209,
        "ohos.aafwk.param.callerUid": 20010014
    },
    "entities": []
}
```