js-apis-particleAbility.md 14.2 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# ParticleAbility Module

## Constraints

The ParticleAbility module is used to perform operations on abilities of the Data and Service types.

## Modules to Import

```js
import particleAbility from '@ohos.ability.particleAbility'
```

W
wusongqing 已提交
13
## particleAbility.startAbility
W
wusongqing 已提交
14

W
wusongqing 已提交
15
startAbility(parameter: StartAbilityParameter, callback: AsyncCallback\<void>: void
W
wusongqing 已提交
16

W
wusongqing 已提交
17
Starts a Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
18

W
wusongqing 已提交
19
**Parameters**
W
wusongqing 已提交
20 21


W
wusongqing 已提交
22 23 24 25
| Name| Type| Mandatory| Description|
| --------- | ----------------------------------------------- | ---- | ----------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
| callback  | AsyncCallback\<void>                            | Yes| Callback used to return the result.|
W
wusongqing 已提交
26

W
wusongqing 已提交
27
**Example**
W
wusongqing 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

```js
import particleAbility from '@ohos.ability.particleAbility'
particleAbility.startAbility(
	{
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
            flags: FLAG_AUTH_READ_URI_PERMISSION;
            deviceId: "",
            bundleName: "com.example.Data",
            abilityName: "com.example.Data.MainAbility",
            uri:""
        },
    },
    (error, result) => {
		console.log('particleAbility startAbility errCode:' + error + 'result:' + result)
    },
)
```



W
wusongqing 已提交
53
## particleAbility.startAbility
W
wusongqing 已提交
54

W
wusongqing 已提交
55
startAbility(parameter: StartAbilityParameter): Promise\<number>
W
wusongqing 已提交
56 57
Starts a Service ability. This method uses a promise to return the result.

W
wusongqing 已提交
58
**Parameters**
W
wusongqing 已提交
59 60


W
wusongqing 已提交
61 62 63 64 65 66 67
| Name| Type| Mandatory| Description|
| --------- | ----------------------------------------------- | ---- | ----------------- |
| parameter | [StartAbilityParameter](#startabilityparameter) | Yes| Ability to start.|
**Return value**
| Type| Description|
| -------------- | ------------------------- |
| Promise\<void> | Promise returned with the result.|
W
wusongqing 已提交
68

W
wusongqing 已提交
69
**Example**
W
wusongqing 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

```js
import particleAbility from '@ohos.ability.particleAbility'
particleAbility.startAbility(
	{
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
            flags: FLAG_AUTH_READ_URI_PERMISSION;
            deviceId: "",
            bundleName: "com.example.Data",
            abilityName: "com.example.Data.MainAbility",
            uri:""
        },
    },
).then((void) => {
    console.info("particleAbility startAbility");
});
```



W
wusongqing 已提交
94
## particleAbility.terminateSelf
W
wusongqing 已提交
95

W
wusongqing 已提交
96
terminateSelf(callback: AsyncCallback\<void>): void
W
wusongqing 已提交
97

W
wusongqing 已提交
98
Terminates this Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
99

W
wusongqing 已提交
100
**Parameters**
W
wusongqing 已提交
101

W
wusongqing 已提交
102 103 104
| Name| Type| Mandatory| Description|
| -------- | -------------------- | ---- | -------------------- |
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
105

W
wusongqing 已提交
106
**Example**
W
wusongqing 已提交
107 108 109 110 111 112 113 114 115 116 117 118

```js
import particleAbility from '@ohos.ability.particleAbility'
particleAbility.terminateSelf(
    (error, result) => {
		console.log('particleAbility terminateSelf errCode:' + error + 'result:' + result)
    }
)
```



W
wusongqing 已提交
119
## particleAbility.terminateSelf
W
wusongqing 已提交
120

W
wusongqing 已提交
121
terminateSelf(): Promise\<void>
W
wusongqing 已提交
122

W
wusongqing 已提交
123
Terminates this Service ability. This method uses a promise to return the result.
W
wusongqing 已提交
124

W
wusongqing 已提交
125 126 127 128
**Return value**
| Type| Description|
| -------------- | ------------------------- |
| Promise\<void> | Promise returned with the result.|
W
wusongqing 已提交
129

W
wusongqing 已提交
130
**Example**
W
wusongqing 已提交
131 132 133 134 135 136 137 138 139 140

```js
import particleAbility from '@ohos.ability.particleAbility'
particleAbility.terminateSelf().then((void) => {
	console.info("particleAbility terminateSelf");
});
```



W
wusongqing 已提交
141
## particleAbility.acquireDataAbilityHelper
W
wusongqing 已提交
142

W
wusongqing 已提交
143
acquireDataAbilityHelper(uri: string): DataAbilityHelper
W
wusongqing 已提交
144

W
wusongqing 已提交
145
Obtains a **dataAbilityHelper** object.
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**Parameters**
W
wusongqing 已提交
148

W
wusongqing 已提交
149 150 151
| Name| Type| Mandatory| Description|
| :--- | ------ | ---- | ------------------------ |
| uri  | string | Yes| URI of the file to open.|
W
wusongqing 已提交
152

W
wusongqing 已提交
153
**Return value**
W
wusongqing 已提交
154 155

| Type| Description|
W
wusongqing 已提交
156
| ----------------- | -------------------------------------------- |
W
wusongqing 已提交
157 158
| DataAbilityHelper | A utility class used to help other abilities access a Data ability.|

W
wusongqing 已提交
159
**Example**
W
wusongqing 已提交
160 161 162 163 164 165 166

```js
import particleAbility from '@ohos.ability.particleAbility'     
var uri = "";
particleAbility.acquireDataAbilityHelper(uri)
```

W
wusongqing 已提交
167
## particleAbility.connectAbility
W
wusongqing 已提交
168

W
wusongqing 已提交
169
connectAbility(request: Want, options:ConnectOptions): number
W
wusongqing 已提交
170

W
wusongqing 已提交
171
Connects this ability to a specific Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
172

W
wusongqing 已提交
173
**Parameters**
W
wusongqing 已提交
174

W
wusongqing 已提交
175 176 177 178
| Name| Type| Mandatory| Description|
| ------- | -------------- | ---- | -------------------------- |
| request | [Want](#want)           | Yes| Service ability to connect.|
| options | ConnectOptions | Yes| Callback used to return the result.|
W
wusongqing 已提交
179

W
wusongqing 已提交
180
ConnectOptions
W
wusongqing 已提交
181 182 183 184 185 186 187

| Name| Readable/Writable| Type| Mandatory| Description|
| ------------ | -------- | -------- | ---- | ---------------------------------- |
| onConnect    | Read-only| function | Yes| Callback invoked when the connection is set up.|
| onDisconnect | Read-only| function | Yes| Callback invoked when the connection is interrupted.|
| onFailed     | Read-only| function | Yes| Callback invoked when the connection fails.|

W
wusongqing 已提交
188 189 190 191
**Return value**
| Type| Description|
| ------ | -------------------------- |
| number | Unique identifier of the connection between the client and the service side.|
W
wusongqing 已提交
192

W
wusongqing 已提交
193
**Example**
W
wusongqing 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

```javascript
import particleAbility from '@ohos.ability.particleAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('particleAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = particleAbility.connectAbility(
    {
        bundleName: "com.ix.ServiceAbility",
        abilityName: "com.ix.ServiceAbility.ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
```

W
wusongqing 已提交
219
## particleAbility.disconnectAbility
W
wusongqing 已提交
220

W
wusongqing 已提交
221
disconnectAbility(connection: number, callback:AsyncCallback\<void>): void
W
wusongqing 已提交
222

W
wusongqing 已提交
223
Disconnects this ability from a specific Service ability. This method uses a callback to return the result.
W
wusongqing 已提交
224

W
wusongqing 已提交
225
**Parameters**
W
wusongqing 已提交
226

W
wusongqing 已提交
227 228 229 230
| Name| Type| Mandatory| Description|
| ---------- | ------------- | ---- | ------------------------------ |
| connection | number        | Yes| ID of the Service ability to disconnect.|
| callback   | AsyncCallback\<void> | Yes| Callback used to return the result.|
W
wusongqing 已提交
231

W
wusongqing 已提交
232
**Example**
W
wusongqing 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

```javascript
import particleAbility from '@ohos.ability.particleAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('particleAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = particleAbility.connectAbility(
    {
        bundleName: "com.ix.ServiceAbility",
        abilityName: "com.ix.ServiceAbility.ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
var result = particleAbility.disconnectAbility(connId,
    (error,data) => {
        console.log('particleAbilityTest DisConnectAbility result errCode : ' + error.code + " data: " + data)
    },
);
```

W
wusongqing 已提交
263
## particleAbility.disconnectAbility
W
wusongqing 已提交
264

W
wusongqing 已提交
265
disconnectAbility(connection: number): Promise\<void>
W
wusongqing 已提交
266

W
wusongqing 已提交
267
Disconnects this ability from a specific Service ability. This method uses a promise to return the result.
W
wusongqing 已提交
268

W
wusongqing 已提交
269
**Parameters**
W
wusongqing 已提交
270

W
wusongqing 已提交
271 272 273
| Name| Type| Mandatory| Description|
| ---------- | ------ | ---- | ------------------------------ |
| connection | number | Yes| ID of the Service ability to disconnect.|
W
wusongqing 已提交
274

W
wusongqing 已提交
275 276 277 278
**Return value**
| Type| Description|
| -------------- | ------------------------- |
| Promise\<void> | Promise returned with the result.|
W
wusongqing 已提交
279

W
wusongqing 已提交
280
**Example**
W
wusongqing 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

```javascript
import particleAbility from '@ohos.ability.particleAbility'
function onConnectCallback(element, remote){
    console.log('ConnectAbility onConnect remote is proxy:' + (remote instanceof rpc.RemoteProxy));
}
function onDisconnectCallback(element){
    console.log('ConnectAbility onDisconnect element.deviceId : ' + element.deviceId)
}
function onFailedCallback(code){
    console.log('particleAbilityTest ConnectAbility onFailed errCode : ' + code)
}
var connId = particleAbility.connectAbility(
    {
        bundleName: "com.ix.ServiceAbility",
        abilityName: "com.ix.ServiceAbility.ServiceAbilityA",
    },
    {
        onConnect: onConnectCallback,
        onDisconnect: onDisconnectCallback,
        onFailed: onFailedCallback,
    },
);
var result = particleAbility.disconnectAbility(connId).then((void) => {
	console.info("particleAbilityTest disconnectAbility");
});
```

## StartAbilityParameter

| Name| Readable/Writable| Type| Mandatory| Description|
| ------------------- | -------- | --------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
313
| want                | Read-only| [Want](#want) | Yes| Information about the ability to start.|
W
wusongqing 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
| abilityStartSetting | Read-only| {[key: string]: any}  | No| Special attribute of the ability to start. This attribute can be passed in the method call.|

## Want

| Name| Readable/Writable| Type| Mandatory| Description|
| ----------- | -------- | -------------------- | ---- | ------------------------------------------------------------ |
| deviceId    | Read-only| string               | No| ID of the device running the ability to start.|
| bundleName  | Read-only| string               | No| Bundle name of the ability to start. If both **bundleName** and **abilityName** are specified in a **Want** object, the **Want** object can directly match the specified ability.|
| abilityName | Read-only| string               | No| Name of the ability to start. If both **bundleName** and **abilityName** are specified in a **Want** object, the **Want** object can directly match the specified ability.|
| uri         | Read-only| string               | No| URI information to match. If **uri** is specified in a **Want** object, the **Want** object will match the specified URI information, including **scheme**, **schemeSpecificPart**, **authority**, and **path**.|
| type        | Read-only| string               | No| MIME type, for example, **text/plain** or **image/***.|
| flags       | Read-only| number               | No| How the **Want** object will be handled. By default, numbers are passed in. For details, see [flags](#flags).|
| action      | Read-only| string               | No| Action option.|
| parameters  | Read-only| {[key: string]: any} | No| List of parameters in the **Want** object.|
| entities    | Read-only| Array\<string>       | No| List of entities.|

## flags

| Name| Value| Description|
| ------------------------------------ | ---------- | ------------------------------------------------------------ |
| FLAG_AUTH_READ_URI_PERMISSION        | 0x00000001 | Indicates the permission to read the URI.|
| FLAG_AUTH_WRITE_URI_PERMISSION       | 0x00000002 | Indicates the permission to write the URI.|
| FLAG_ABILITY_FORWARD_RESULT          | 0x00000004 | Returns the result to the ability.|
| FLAG_ABILITY_CONTINUATION            | 0x00000008 | Indicates whether the ability on the local device can be migrated to a remote device.|
| FLAG_NOT_OHOS_COMPONENT              | 0x00000010 | Indicates that a component does not belong to OHOS.|
| FLAG_ABILITY_FORM_ENABLED            | 0x00000020 | Indicates whether to enable an ability.|
| FLAG_AUTH_PERSISTABLE_URI_PERMISSION | 0x00000040 | Indicates the permission to make the URI persistent.|
| FLAG_AUTH_PREFIX_URI_PERMISSION      | 0x00000080 | Indicates the permission to verify URIs by prefix matching.|
| FLAG_ABILITYSLICE_MULTI_DEVICE       | 0x00000100 | Supports cross-device startup in a distributed scheduler.|
| FLAG_START_FOREGROUND_ABILITY        | 0x00000200 | Indicates that the Service ability is started regardless of whether the host application has been started.|
| FLAG_ABILITY_CONTINUATION_REVERSIBLE | 0x00000400 | Indicates that the migration is reversible.|
| FLAG_INSTALL_ON_DEMAND               | 0x00000800 | Indicates that the specific ability will be installed if it has not been installed.|
| FLAG_INSTALL_WITH_BACKGROUND_MODE    | 0x80000000 | Indicates that the specific ability will be installed in the background if it has not been installed.|
| FLAG_ABILITY_CLEAR_MISSION           | 0x00008000 | Clears other operation missions. This flag can be set for the **Want** object passed to **ohos.app.Context#startAbility** and must be used together with **flag_ABILITY_NEW_MISSION**.|
| FLAG_ABILITY_NEW_MISSION             | 0x10000000 | Creates a mission on the historical mission stack.|
| FLAG_ABILITY_MISSION_TOP             | 0x20000000 | Starts the mission on the top of the existing mission stack; creates an ability instance if no mission exists.|

## AbilityStartSetting

The **AbilityStartSetting** attribute is an object defined as [key: string]: any. The key is a type of **AbilityStartSetting**, and the value is a type of **AbilityWindowConfiguration**.

W
wusongqing 已提交
355 356
Defines special attributes of an ability, for example, **featureAbility.AbilityStartSetting.BOUNDS_KEY**.

W
wusongqing 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
| Name| Value| Description|
| --------------- | --------------- | -------------------------- |
| BOUNDS_KEY      | "abilityBounds" | Ability window size.|
| WINDOW_MODE_KEY | "windowMode"    | Ability window display mode.|
| DISPLAY_ID_KEY  | "displayId"     | Display device ID.|

## AbilityWindowConfiguration

Defines the window display modes of a Page ability, for example, **featureAbility.AbilityWindowConfiguration.WINDOW_MODE_UNDEFINED**.

| Name| Value| Description|
| --------------------------- | ---- | ---------- |
| WINDOW_MODE_UNDEFINED       | 0    | The Page ability is in an undefined window display mode.|
| WINDOW_MODE_FULLSCREEN      | 1    | The Page ability is in full screen mode.|
| WINDOW_MODE_SPLIT_PRIMARY   | 100  | The Page ability is displayed in the primary window when it is in split-screen mode.|
| WINDOW_MODE_SPLIT_SECONDARY | 101  | The Page ability is displayed in the secondary window when it is in split-screen mode.|
| WINDOW_MODE_FLOATING        | 102  | The Page ability is displayed in floating window mode.|