js-apis-particleAbility.md 16.8 KB
Newer Older
W
wusongqing 已提交
1
# ParticleAbility
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4 5 6
> **NOTE**
> 
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 
> The APIs of this module can be used only in the FA model.
W
wusongqing 已提交
7

W
wusongqing 已提交
8 9 10 11 12 13 14 15 16 17
## 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 已提交
18
## particleAbility.startAbility
W
wusongqing 已提交
19

W
wusongqing 已提交
20
startAbility(parameter: StartAbilityParameter, callback: AsyncCallback\<void>): void
W
wusongqing 已提交
21

W
wusongqing 已提交
22
Starts a Particle ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
23

W
wusongqing 已提交
24
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
25

W
wusongqing 已提交
26
**Parameters**
W
wusongqing 已提交
27 28


W
wusongqing 已提交
29
| Name     | Type                                           | Mandatory| Description             |
W
wusongqing 已提交
30
| --------- | ----------------------------------------------- | ---- | ----------------- |
31
| parameter | [StartAbilityParameter](js-apis-featureAbility.md#startabilityparameter) | Yes  | Ability to start.|
W
wusongqing 已提交
32
| callback  | AsyncCallback\<void>                            | Yes  | Callback used to return the result. |
W
wusongqing 已提交
33

W
wusongqing 已提交
34
**Example**
W
wusongqing 已提交
35 36 37

```js
import particleAbility from '@ohos.ability.particleAbility'
W
wusongqing 已提交
38
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
39 40 41 42 43 44 45
particleAbility.startAbility(
	{
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
46
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
            deviceId: "",
            bundleName: "com.example.Data",
            abilityName: "com.example.Data.MainAbility",
            uri:""
        },
    },
    (error, result) => {
		console.log('particleAbility startAbility errCode:' + error + 'result:' + result)
    },
)
```



W
wusongqing 已提交
61
## particleAbility.startAbility
W
wusongqing 已提交
62

63
startAbility(parameter: StartAbilityParameter): Promise\<void>;
W
wusongqing 已提交
64

W
wusongqing 已提交
65
Starts a Particle ability. This API uses a promise to return the result.
W
wusongqing 已提交
66

W
wusongqing 已提交
67
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
68

W
wusongqing 已提交
69
**Parameters**
W
wusongqing 已提交
70 71


W
wusongqing 已提交
72
| Name     | Type                                           | Mandatory| Description             |
W
wusongqing 已提交
73
| --------- | ----------------------------------------------- | ---- | ----------------- |
74
| parameter | [StartAbilityParameter](js-apis-featureAbility.md#startabilityparameter) | Yes  | Ability to start.|
W
wusongqing 已提交
75

W
wusongqing 已提交
76
**Return value**
W
wusongqing 已提交
77

W
wusongqing 已提交
78
| Type          | Description                     |
W
wusongqing 已提交
79
| -------------- | ------------------------- |
W
wusongqing 已提交
80
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
81

W
wusongqing 已提交
82
**Example**
W
wusongqing 已提交
83 84 85

```js
import particleAbility from '@ohos.ability.particleAbility'
W
wusongqing 已提交
86
import wantConstant from '@ohos.ability.wantConstant'
W
wusongqing 已提交
87 88 89 90 91 92 93
particleAbility.startAbility(
	{
        want:
        {
            action: "action.system.home",
            entities: ["entity.system.home"],
            type: "MIMETYPE",
W
wusongqing 已提交
94
            flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION,
W
wusongqing 已提交
95 96
            deviceId: "",
            bundleName: "com.example.Data",
W
wusongqing 已提交
97
            abilityName: "com.example. Data.MainAbility",
W
wusongqing 已提交
98 99 100
            uri:""
        },
    },
W
wusongqing 已提交
101
).then((data) => {
W
wusongqing 已提交
102 103 104 105 106 107
    console.info("particleAbility startAbility");
});
```



W
wusongqing 已提交
108
## particleAbility.terminateSelf
W
wusongqing 已提交
109

W
wusongqing 已提交
110
terminateSelf(callback: AsyncCallback\<void>): void
W
wusongqing 已提交
111

W
wusongqing 已提交
112
Terminates this Particle ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
113

W
wusongqing 已提交
114
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
115

W
wusongqing 已提交
116
**Parameters**
W
wusongqing 已提交
117

W
wusongqing 已提交
118
| Name    | Type                | Mandatory| Description                |
W
wusongqing 已提交
119
| -------- | -------------------- | ---- | -------------------- |
W
wusongqing 已提交
120
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|
W
wusongqing 已提交
121

W
wusongqing 已提交
122
**Example**
W
wusongqing 已提交
123 124 125 126 127 128 129 130 131 132 133 134

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



W
wusongqing 已提交
135
## particleAbility.terminateSelf
W
wusongqing 已提交
136

W
wusongqing 已提交
137
terminateSelf(): Promise\<void>
W
wusongqing 已提交
138

W
wusongqing 已提交
139
Terminates this Particle ability. This API uses a promise to return the result.
W
wusongqing 已提交
140

W
wusongqing 已提交
141
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
142

W
wusongqing 已提交
143
**Return value**
W
wusongqing 已提交
144

W
wusongqing 已提交
145
| Type          | Description                     |
W
wusongqing 已提交
146
| -------------- | ------------------------- |
W
wusongqing 已提交
147
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
148

W
wusongqing 已提交
149
**Example**
W
wusongqing 已提交
150 151 152

```js
import particleAbility from '@ohos.ability.particleAbility'
W
wusongqing 已提交
153
particleAbility.terminateSelf().then((data) => {
W
wusongqing 已提交
154 155 156 157 158 159
	console.info("particleAbility terminateSelf");
});
```



W
wusongqing 已提交
160
## particleAbility.acquireDataAbilityHelper
W
wusongqing 已提交
161

W
wusongqing 已提交
162
acquireDataAbilityHelper(uri: string): DataAbilityHelper
W
wusongqing 已提交
163

W
wusongqing 已提交
164
Obtains a **dataAbilityHelper** object.
W
wusongqing 已提交
165

W
wusongqing 已提交
166
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel
W
wusongqing 已提交
167

W
wusongqing 已提交
168
**Parameters**
W
wusongqing 已提交
169

W
wusongqing 已提交
170
| Name| Type  | Mandatory| Description                    |
W
wusongqing 已提交
171
| :--- | ------ | ---- | ------------------------ |
W
wusongqing 已提交
172
| uri  | string | Yes  | URI of the file to open.|
W
wusongqing 已提交
173

W
wusongqing 已提交
174
**Return value**
W
wusongqing 已提交
175

W
wusongqing 已提交
176
| Type             | Description                                        |
W
wusongqing 已提交
177
| ----------------- | -------------------------------------------- |
W
wusongqing 已提交
178 179
| DataAbilityHelper | A utility class used to help other abilities access a Data ability.|

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

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

W
wusongqing 已提交
188 189 190 191 192

## particleAbility.startBackgroundRunning

startBackgroundRunning(id: number, request: NotificationRequest, callback: AsyncCallback&lt;void&gt;): void;

W
wusongqing 已提交
193 194 195
Requests a continuous task from the system. This API uses an asynchronous callback to return the result. You are advised to use the new API [backgroundTaskManager.startBackgroundRunning](js-apis-backgroundTaskManager.md#backgroundtaskmanagerstartbackgroundrunning8).

**Required permissions**: ohos.permission.KEEP_BACKGROUND_RUNNING
W
wusongqing 已提交
196

W
wusongqing 已提交
197
**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask
W
wusongqing 已提交
198

W
wusongqing 已提交
199
**Parameters**
W
wusongqing 已提交
200

W
wusongqing 已提交
201 202 203 204
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | id | number | Yes| Notification ID of a continuous task.|
  | request | NotificationRequest | Yes| Notification parameter, which is used to display information in the notification bar.|
W
wusongqing 已提交
205 206 207
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

 **Example**
W
wusongqing 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

```js
import notification from '@ohos.notification';
import particleAbility from '@ohos.ability.particleAbility';
import wantAgent from '@ohos.wantAgent';

function callback(err, data) {
    if (err) {
        console.error("Operation failed Cause: " + err);
    } else {
        console.info("Operation succeeded");
    }
}

let wantAgentInfo = {
    wants: [
        {
            bundleName: "com.example.myapplication",
            abilityName: "com.example.myapplication.MainAbility"
        }
    ],
    operationType: wantAgent.OperationType.START_ABILITY,
    requestCode: 0,
W
wusongqing 已提交
231
    wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
W
wusongqing 已提交
232 233 234 235 236 237 238 239
};

wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
    let basicContent = {
        title: "title",
        text: "text"
    };
    let notificationContent = {
W
wusongqing 已提交
240
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
W
wusongqing 已提交
241 242 243
        normal: basicContent
    };
    let request = {
W
wusongqing 已提交
244
        content: notificationContent,
W
wusongqing 已提交
245 246 247 248 249 250 251 252 253 254 255 256
        wantAgent: wantAgentObj
    };
    let id = 1;
    particleAbility.startBackgroundRunning(id, request, callback);
});

```

## particleAbility.startBackgroundRunning

startBackgroundRunning(id: number, request: NotificationRequest): Promise&lt;void&gt;

W
wusongqing 已提交
257 258
**Required permissions**: ohos.permission.KEEP_BACKGROUND_RUNNING

W
wusongqing 已提交
259
**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask
W
wusongqing 已提交
260

W
wusongqing 已提交
261
Requests a continuous task from the system. This API uses a promise to return the result. You are advised to use the new API [backgroundTaskManager.startBackgroundRunning](js-apis-backgroundTaskManager.md#backgroundtaskmanagerstartbackgroundrunning8-1).
W
wusongqing 已提交
262 263

**Parameters**
W
wusongqing 已提交
264

W
wusongqing 已提交
265
| Name| Type| Mandatory| Description|
W
wusongqing 已提交
266
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
267 268
| id | number | Yes| Notification ID of a continuous task.|
| request | NotificationRequest | Yes| Notification parameter, which is used to display information in the notification bar.|
W
wusongqing 已提交
269 270

**Return value**
W
wusongqing 已提交
271

W
wusongqing 已提交
272
| Type          | Description                     |
W
wusongqing 已提交
273
| -------------- | ------------------------- |
W
wusongqing 已提交
274
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
275

W
wusongqing 已提交
276 277
**Example**

W
wusongqing 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291
```js
import notification from '@ohos.notification';
import particleAbility from '@ohos.ability.particleAbility';
import wantAgent from '@ohos.wantAgent';

let wantAgentInfo = {
    wants: [
        {
            bundleName: "com.example.myapplication",
            abilityName: "com.example.myapplication.MainAbility"
        }
    ],
    operationType: wantAgent.OperationType.START_ABILITY,
    requestCode: 0,
W
wusongqing 已提交
292
    wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
W
wusongqing 已提交
293 294 295 296 297 298 299 300
};

wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
    let basicContent = {
        title: "title",
        text: "text"
    };
    let notificationContent = {
W
wusongqing 已提交
301
        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
W
wusongqing 已提交
302 303 304
        normal: basicContent
    };
    let request = {
W
wusongqing 已提交
305
        content: notificationContent,
W
wusongqing 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        wantAgent: wantAgentObj
    };
    let id = 1;
    particleAbility.startBackgroundRunning(id, request).then(() => {
        console.info("Operation succeeded");
    }).catch((err) => {
        console.error("Operation failed Cause: " + err);
    });
});

```

## particleAbility.cancelBackgroundRunning

cancelBackgroundRunning(callback: AsyncCallback&lt;void&gt;): void;

W
wusongqing 已提交
322
Requests to cancel a continuous task from the system. This API uses an asynchronous callback to return the result. You are advised to use the new API [backgroundTaskManager.stopBackgroundRunning](js-apis-backgroundTaskManager.md#backgroundtaskmanagerstopbackgroundrunning8).
W
wusongqing 已提交
323

W
wusongqing 已提交
324
**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask
W
wusongqing 已提交
325

W
wusongqing 已提交
326
 **Parameters**
W
wusongqing 已提交
327

W
wusongqing 已提交
328
  | Name| Type| Mandatory| Description|
W
wusongqing 已提交
329
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
330
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
331 332

 **Example**
W
wusongqing 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

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

function callback(err, data) {
    if (err) {
        console.error("Operation failed Cause: " + err);
    } else {
        console.info("Operation succeeded");
    }
}

particleAbility.cancelBackgroundRunning(callback);

```

## particleAbility.cancelBackgroundRunning

cancelBackgroundRunning(): Promise&lt;void&gt;;

W
wusongqing 已提交
353
Requests a continuous task from the system. This API uses a promise to return the result. You are advised to use the new API [backgroundTaskManager.stopBackgroundRunning](js-apis-backgroundTaskManager.md#backgroundtaskmanagerstopbackgroundrunning8-1).
W
wusongqing 已提交
354

W
wusongqing 已提交
355
**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask
W
wusongqing 已提交
356

W
wusongqing 已提交
357
**Return value**
W
wusongqing 已提交
358

W
wusongqing 已提交
359
| Type          | Description                     |
W
wusongqing 已提交
360
| -------------- | ------------------------- |
W
wusongqing 已提交
361
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
362

W
wusongqing 已提交
363 364
 **Example**

W
wusongqing 已提交
365 366 367 368 369 370 371 372 373 374
```js
import particleAbility from '@ohos.ability.particleAbility';

particleAbility.cancelBackgroundRunning().then(() => {
    console.info("Operation succeeded");
}).catch((err) => {
    console.error("Operation failed Cause: " + err);
});

```
W
wusongqing 已提交
375 376 377 378 379 380 381 382 383 384 385 386


## particleAbility.connectAbility

connectAbility(request: Want, options:ConnectOptions): number

Connects this ability to a specific Service ability. This API uses a callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

W
wusongqing 已提交
387
| Name   | Type          | Mandatory| Description                        |
W
wusongqing 已提交
388
| ------- | -------------- | ---- | ---------------------------- |
W
wusongqing 已提交
389 390
| request | [Want](js-apis-application-Want.md)           | Yes  | Service ability to connect.|
| options | ConnectOptions | Yes  | Callback used to return the result.          |
W
wusongqing 已提交
391

W
wusongqing 已提交
392

W
wusongqing 已提交
393
ConnectOptions
W
wusongqing 已提交
394 395 396

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

W
wusongqing 已提交
397
| Name          | Readable/Writable| Type      | Mandatory  | Description                       |
W
wusongqing 已提交
398
| ------------ | ---- | -------- | ---- | ------------------------- |
W
wusongqing 已提交
399 400 401
| onConnect    | Read only  | function | Yes   | Callback invoked when the connection is successful.              |
| onDisconnect | Read only  | function | Yes   | Callback invoked when the connection fails.              |
| onFailed     | Read only  | function | Yes   | Callback invoked when **connectAbility** fails to be called.|
W
wusongqing 已提交
402

W
wusongqing 已提交
403 404 405
**Example**

```js
W
wusongqing 已提交
406
        import rpc from '@ohos.rpc'
W
wusongqing 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        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: "ServiceAbilityA",
            },
            {
                onConnect: onConnectCallback,
                onDisconnect: onDisconnectCallback,
                onFailed: onFailedCallback,
            },
        );

W
wusongqing 已提交
428 429 430 431
        particleAbility.disconnectAbility(connId).then((data)=>{
            console.log( " data: " + data);
        }).catch((error)=>{
            console.log('particleAbilityTest result errCode : ' + error.code )
W
wusongqing 已提交
432 433 434 435 436 437 438 439
        });
    

```


## particleAbility.disconnectAbility

W
wusongqing 已提交
440
disconnectAbility(connection: number, callback:AsyncCallback\<void>): void;
W
wusongqing 已提交
441 442 443 444 445 446 447

Disconnects this ability from the Service ability. This API uses a callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Parameters**

W
wusongqing 已提交
448
  | Name| Type| Mandatory| Description|
W
wusongqing 已提交
449
  | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
450
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
W
wusongqing 已提交
451 452 453 454

**Example**

```js
W
wusongqing 已提交
455
import rpc from '@ohos.rpc'
W
wusongqing 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
 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: "ServiceAbilityA",
            },
            {
                onConnect: onConnectCallback,
                onDisconnect: onDisconnectCallback,
                onFailed: onFailedCallback,
            },
        );
W
wusongqing 已提交
476 477 478 479 480
        var result =  particleAbility.disconnectAbility(connId).then((data)=>{
            console.log( " data: " + data);
        }).catch((error)=>{
            console.log('particleAbilityTest result errCode : ' + error.code )
        });
W
wusongqing 已提交
481 482 483 484 485 486

```


## particleAbility.disconnectAbility

W
wusongqing 已提交
487
disconnectAbility(connection: number): Promise\<void>;
W
wusongqing 已提交
488 489 490 491 492 493 494

Disconnects this ability from the Service ability. This API uses a promise to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

**Return value**

W
wusongqing 已提交
495
| Type          | Description                     |
W
wusongqing 已提交
496
| -------------- | ------------------------- |
W
wusongqing 已提交
497
| Promise\<void> | Promise used to return the result.|
W
wusongqing 已提交
498 499 500 501

**Example**

```js
W
wusongqing 已提交
502
import rpc from '@ohos.rpc'
W
wusongqing 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
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: "ServiceAbilityA",
            },
            {
                onConnect: onConnectCallback,
                onDisconnect: onDisconnectCallback,
                onFailed: onFailedCallback,
            },
        );

W
wusongqing 已提交
524 525 526 527
         particleAbility.disconnectAbility(connId).then((data)=>{
            console.log( " data: " + data);
        }).catch((error)=>{
            console.log('particleAbilityTest result errCode : ' + error.code )
W
wusongqing 已提交
528 529 530 531 532 533 534 535
        });

```

## ErrorCode

Enumerates error codes.

W
wusongqing 已提交
536 537
**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel

W
wusongqing 已提交
538 539
| Name                         | Value  | Description                                                        |
| ----------------------------- | ---- | ------------------------------------------------------------ |
W
wusongqing 已提交
540
| INVALID_PARAMETER         | -1    | Invalid parameter.|