1# @ohos.ability.particleAbility (ParticleAbility) 2 3The **particleAbility** module provides APIs for operating a DataAbility and ServiceAbility. You can use the APIs to start and terminate a ParticleAbility, obtain a **dataAbilityHelper** object, and connect to or disconnect from a ServiceAbility. 4 5> **NOTE** 6> 7> 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. 8> 9> The APIs of this module can be used only in the FA model. 10 11## Constraints 12 13The ParticleAbility module is used to perform operations on abilities of the Data and Service types. 14 15## Modules to Import 16 17```ts 18import { particleAbility } from '@kit.AbilityKit'; 19``` 20 21## particleAbility.startAbility 22 23startAbility(parameter: StartAbilityParameter, callback: AsyncCallback\<void>): void 24 25Starts a ParticleAbility. This API uses an asynchronous callback to return the result. 26 27> **NOTE** 28> 29> For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md). 30 31**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 32 33**Parameters** 34 35| Name | Type | Mandatory| Description | 36| --------- | ----------------------------------------------- | ---- | ----------------- | 37| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes | Ability to start.| 38| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the ParticleAbility is started, **err** is **undefined**; otherwise, **err** is an error object. | 39 40**Example** 41 42<!--code_no_check_fa--> 43```ts 44import { particleAbility, wantConstant } from '@kit.AbilityKit'; 45 46particleAbility.startAbility( 47 { 48 want: 49 { 50 action: 'ohos.want.action.home', 51 entities: ['entity.system.home'], 52 type: 'MIMETYPE', 53 flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, 54 deviceId: '', 55 bundleName: 'com.example.Data', 56 abilityName: 'com.example.Data.EntryAbility', 57 uri: '' 58 }, 59 }, 60 (error, data) => { 61 if (error && error.code !== 0) { 62 console.error(`startAbility fail, error: ${JSON.stringify(error)}`); 63 } else { 64 console.log(`startAbility success, data: ${JSON.stringify(data)}`); 65 } 66 }, 67); 68``` 69 70## particleAbility.startAbility 71 72startAbility(parameter: StartAbilityParameter): Promise\<void> 73 74Starts a ParticleAbility. This API uses a promise to return the result. 75 76> **NOTE** 77> 78> For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md). 79 80**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 81 82**Parameters** 83 84| Name | Type | Mandatory| Description | 85| --------- | ----------------------------------------------- | ---- | ----------------- | 86| parameter | [StartAbilityParameter](js-apis-inner-ability-startAbilityParameter.md) | Yes | Ability to start.| 87 88**Return value** 89 90| Type | Description | 91| -------------- | ------------------------- | 92| Promise\<void> | Promise used to return the result. Promise that returns no value.| 93 94**Example** 95 96<!--code_no_check_fa--> 97```ts 98import { particleAbility, wantConstant } from '@kit.AbilityKit'; 99 100particleAbility.startAbility( 101 { 102 want: 103 { 104 action: 'ohos.want.action.home', 105 entities: ['entity.system.home'], 106 type: 'MIMETYPE', 107 flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, 108 deviceId: '', 109 bundleName: 'com.example.Data', 110 abilityName: 'com.example.Data.EntryAbility', 111 uri: '' 112 }, 113 }, 114).then(() => { 115 console.info('particleAbility startAbility'); 116}); 117``` 118 119## particleAbility.terminateSelf 120 121terminateSelf(callback: AsyncCallback\<void>): void 122 123Terminates this ParticleAbility. This API uses an asynchronous callback to return the result. 124 125**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 126 127**Parameters** 128 129| Name | Type | Mandatory| Description | 130| -------- | -------------------- | ---- | -------------------- | 131| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the ParticleAbility is terminated, **err** is **undefined**; otherwise, **err** is an error object.| 132 133**Example** 134 135<!--code_no_check_fa--> 136```ts 137import { particleAbility } from '@kit.AbilityKit'; 138 139particleAbility.terminateSelf( 140 (error) => { 141 if (error && error.code !== 0) { 142 console.error(`terminateSelf fail, error: ${JSON.stringify(error)}`); 143 } 144 } 145); 146``` 147 148## particleAbility.terminateSelf 149 150terminateSelf(): Promise\<void> 151 152Terminates this ParticleAbility. This API uses a promise to return the result. 153 154**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 155 156**Return value** 157 158| Type | Description | 159| -------------- | ------------------------- | 160| Promise\<void> | Promise used to return the result. Promise that returns no value.| 161 162**Example** 163 164<!--code_no_check_fa--> 165```ts 166import { particleAbility } from '@kit.AbilityKit'; 167 168particleAbility.terminateSelf().then(() => { 169 console.info('particleAbility terminateSelf'); 170}); 171``` 172 173 174 175## particleAbility.acquireDataAbilityHelper 176 177acquireDataAbilityHelper(uri: string): DataAbilityHelper 178 179Obtains a **dataAbilityHelper** object. 180 181> **NOTE** 182> 183> For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md). 184> To access a DataAbility of another application, the target application must be configured with associated startup (**AssociateWakeUp** set to **true**). 185 186**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 187 188**Parameters** 189 190| Name| Type | Mandatory| Description | 191| :--- | ------ | ---- | ------------------------ | 192| uri | string | Yes | URI of the file to open.| 193 194**Return value** 195 196| Type | Description | 197| ----------------- | -------------------------------------------- | 198| [DataAbilityHelper](js-apis-inner-ability-dataAbilityHelper.md) | A utility class used to help other abilities access a DataAbility.| 199 200**Example** 201 202<!--code_no_check_fa--> 203```ts 204import { particleAbility } from '@kit.AbilityKit'; 205 206let uri = ''; 207particleAbility.acquireDataAbilityHelper(uri); 208``` 209 210 211## particleAbility.startBackgroundRunning<sup>(deprecated)</sup> 212 213startBackgroundRunning(id: number, request: NotificationRequest, callback: AsyncCallback<void>): void 214 215Requests a continuous task from the system. This API uses an asynchronous callback to return the result. 216 217**Required permissions**: ohos.permission.KEEP_BACKGROUND_RUNNING 218 219**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 220 221> **NOTE** 222> 223> This API is deprecated since API version 9. You are advised to use [backgroundTaskManager.startBackgroundRunning](../apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md#backgroundtaskmanagerstartbackgroundrunning) instead. 224 225**Parameters** 226 227 | Name| Type| Mandatory| Description| 228 | -------- | -------- | -------- | -------- | 229 | id | number | Yes| Notification ID of the continuous task.| 230 | request | [NotificationRequest](../apis-notification-kit/js-apis-notification.md#notificationrequest) | Yes| Notification parameter, which is used to display information in the notification bar.| 231 | callback | AsyncCallback<void> | Yes| Callback used to return the result. If the continuous task is requested, **err** is **undefined**. Otherwise, **err** is an error object.| 232 233 **Example** 234 235<!--code_no_check_fa--> 236```ts 237import { particleAbility, wantAgent } from '@kit.AbilityKit'; 238import { BusinessError } from '@kit.BasicServicesKit'; 239import notification from '@ohos.notification'; 240 241function callback(error: BusinessError, data: void) { 242 if (error && error.code !== 0) { 243 console.error(`Operation failed error: ${JSON.stringify(error)}`); 244 } else { 245 console.info(`Operation succeeded, data: ${data}`); 246 } 247} 248 249let wantAgentInfo: wantAgent.WantAgentInfo = { 250 wants: [ 251 { 252 bundleName: 'com.example.myapplication', 253 abilityName: 'EntryAbility' 254 } 255 ], 256 operationType: wantAgent.OperationType.START_ABILITY, 257 requestCode: 0, 258 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 259}; 260 261wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { 262 let id = 1; 263 particleAbility.startBackgroundRunning(id, { 264 content: 265 { 266 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 267 normal: 268 { 269 title: 'title', 270 text: 'text' 271 } 272 }, 273 wantAgent: wantAgentObj 274 }, callback); 275}); 276``` 277 278## particleAbility.startBackgroundRunning<sup>(deprecated)</sup> 279 280startBackgroundRunning(id: number, request: NotificationRequest): Promise<void> 281 282Requests a continuous task from the system. This API uses a promise to return the result. 283 284**Required permissions**: ohos.permission.KEEP_BACKGROUND_RUNNING 285 286**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 287 288> **NOTE** 289> 290> This API is deprecated since API version 9. You are advised to use [backgroundTaskManager.startBackgroundRunning](../apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md#backgroundtaskmanagerstartbackgroundrunning-1) instead. 291 292**Parameters** 293 294| Name| Type| Mandatory| Description| 295| -------- | -------- | -------- | -------- | 296| id | number | Yes| Notification ID of the continuous task.| 297| request | [NotificationRequest](../apis-notification-kit/js-apis-notification.md#notificationrequest) | Yes| Notification parameter, which is used to display information in the notification bar.| 298 299**Return value** 300 301| Type | Description | 302| -------------- | ------------------------- | 303| Promise\<void> | Promise used to return the result. Promise that returns no value.| 304 305**Example** 306 307<!--code_no_check_fa--> 308```ts 309import { particleAbility, wantAgent } from '@kit.AbilityKit'; 310import { BusinessError } from '@kit.BasicServicesKit'; 311import notification from '@ohos.notification'; 312 313let wantAgentInfo: wantAgent.WantAgentInfo = { 314 wants: [ 315 { 316 bundleName: 'com.example.myapplication', 317 abilityName: 'EntryAbility' 318 } 319 ], 320 operationType: wantAgent.OperationType.START_ABILITY, 321 requestCode: 0, 322 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 323}; 324 325wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { 326 let id = 1; 327 particleAbility.startBackgroundRunning(id, { 328 content: 329 { 330 contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 331 normal: 332 { 333 title: 'title', 334 text: 'text' 335 } 336 }, 337 wantAgent: wantAgentObj 338 }).then(() => { 339 console.info('Operation succeeded'); 340 }).catch((err: BusinessError) => { 341 console.error(`Operation failed cause: ${JSON.stringify(err)}`); 342 }); 343}); 344``` 345 346## particleAbility.cancelBackgroundRunning<sup>(deprecated)</sup> 347 348cancelBackgroundRunning(callback: AsyncCallback<void>): void 349 350Requests to cancel a continuous task from the system. This API uses an asynchronous callback to return the result. 351 352**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 353 354> **NOTE** 355> 356> This API is deprecated since API version 9. You are advised to use [backgroundTaskManager.stopBackgroundRunning](../apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md#backgroundtaskmanagerstopbackgroundrunning) instead. 357 358**Parameters** 359 360| Name| Type| Mandatory| Description| 361| -------- | -------- | -------- | -------- | 362| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the continuous task is canceled, **err** is **undefined**. Otherwise, **err** is an error object.| 363 364**Example** 365 366<!--code_no_check_fa--> 367```ts 368import { particleAbility } from '@kit.AbilityKit'; 369import { BusinessError } from '@kit.BasicServicesKit'; 370 371function callback(error: BusinessError, data: void) { 372 if (error && error.code !== 0) { 373 console.error(`Operation failed error: ${JSON.stringify(error)}`); 374 } else { 375 console.info(`Operation succeeded, data: ${data}`); 376 } 377} 378 379particleAbility.cancelBackgroundRunning(callback); 380``` 381 382## particleAbility.cancelBackgroundRunning<sup>(deprecated)</sup> 383 384cancelBackgroundRunning(): Promise<void> 385 386Requests to cancel a continuous task from the system. This API uses a promise to return the result. 387 388**System capability**: SystemCapability.ResourceSchedule.BackgroundTaskManager.ContinuousTask 389 390> **NOTE** 391> 392> This API is deprecated since API version 9. You are advised to use [backgroundTaskManager.stopBackgroundRunning](../apis-backgroundtasks-kit/js-apis-resourceschedule-backgroundTaskManager.md#backgroundtaskmanagerstopbackgroundrunning-1) instead. 393 394**Return value** 395 396| Type | Description | 397| -------------- | ------------------------- | 398| Promise\<void> | Promise used to return the result. Promise that returns no value.| 399 400 **Example** 401 402<!--code_no_check_fa--> 403```ts 404import { particleAbility } from '@kit.AbilityKit'; 405import { BusinessError } from '@kit.BasicServicesKit'; 406 407particleAbility.cancelBackgroundRunning().then(() => { 408 console.info('Operation succeeded'); 409}).catch((err: BusinessError) => { 410 console.error(`Operation failed cause: ${JSON.stringify(err)}`); 411}); 412``` 413 414## particleAbility.connectAbility 415 416connectAbility(request: Want, options:ConnectOptions): number 417 418Connects this ability to a ServiceAbility. 419 420> **NOTE** 421> 422> For details about the startup rules for the components in the FA model, see [Component Startup Rules (FA Model)](../../application-models/component-startup-rules-fa.md). 423> To connect to a ServiceAbility of another application, the target application must be configured with associated startup (**AssociateWakeUp** set to **true**). 424 425 426**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 427 428**Parameters** 429 430| Name | Type | Mandatory| Description | 431| ------- | -------------- | ---- | ---------------------------- | 432| request | [Want](js-apis-application-want.md) | Yes | ServiceAbility to connect.| 433| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes | Connection options. | 434 435**Return value** 436 437| Type | Description | 438| ------ | -------------------- | 439| number | ID of the connected ServiceAbility. The ID starts from 0 and is incremented by 1 each time a connection is set up.| 440 441**Example** 442 443<!--code_no_check_fa--> 444```ts 445import { particleAbility } from '@kit.AbilityKit'; 446import { rpc } from '@kit.IPCKit'; 447import { BusinessError } from '@kit.BasicServicesKit'; 448 449let connId = particleAbility.connectAbility( 450 { 451 bundleName: 'com.ix.ServiceAbility', 452 abilityName: 'ServiceAbilityA', 453 }, 454 { 455 onConnect: (element, remote) => { 456 console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`); 457 }, 458 onDisconnect: (element) => { 459 console.log(`ConnectAbility onDisconnect element.deviceId: ${element.deviceId}`); 460 }, 461 onFailed: (code) => { 462 console.error(`particleAbilityTest ConnectAbility onFailed errCode: ${code}`); 463 }, 464 }, 465); 466 467particleAbility.disconnectAbility(connId).then((data) => { 468 console.log(`data: ${data}`); 469}).catch((error: BusinessError) => { 470 console.error(`particleAbilityTest result errCode: ${error.code}`); 471}); 472``` 473 474## particleAbility.disconnectAbility 475 476disconnectAbility(connection: number, callback:AsyncCallback\<void>): void 477 478Disconnects this ability from a specific ServiceAbility. This API uses an asynchronous callback to return the result. 479 480**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 481 482**Parameters** 483 484 | Name| Type| Mandatory| Description| 485 | -------- | -------- | -------- | -------- | 486 | connection | number | Yes | ID of the ServiceAbility to disconnect.| 487 | callback | AsyncCallback<void> | Yes| Callback used to return the result. If the disconnection is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 488 489**Example** 490 491<!--code_no_check_fa--> 492```ts 493import { particleAbility } from '@kit.AbilityKit'; 494import { rpc } from '@kit.IPCKit'; 495 496let connId = particleAbility.connectAbility( 497 { 498 bundleName: 'com.ix.ServiceAbility', 499 abilityName: 'ServiceAbilityA', 500 }, 501 { 502 onConnect: (element, remote) => { 503 console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`); 504 }, 505 onDisconnect: (element) => { 506 console.log(`ConnectAbility onDisconnect element.deviceId: ${element.deviceId}`); 507 }, 508 onFailed: (code) => { 509 console.error(`particleAbilityTest ConnectAbility onFailed errCode: ${code}`); 510 }, 511 }, 512); 513 514particleAbility.disconnectAbility(connId, (err) => { 515 console.error(`particleAbilityTest disconnectAbility err: ${JSON.stringify(err)}`); 516}); 517``` 518 519 520## particleAbility.disconnectAbility 521 522disconnectAbility(connection: number): Promise\<void> 523 524Disconnects this ability from a specific ServiceAbility. This API uses a promise to return the result. 525 526**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 527 528**Parameters** 529 530| Name| Type| Mandatory| Description| 531| -------- | -------- | -------- | -------- | 532| connection | number | Yes | ID of the ServiceAbility to disconnect.| 533 534**Return value** 535 536| Type | Description | 537| -------------- | ------------------------- | 538| Promise\<void> | Promise used to return the result. Promise that returns no value.| 539 540**Example** 541 542<!--code_no_check_fa--> 543```ts 544import { particleAbility } from '@kit.AbilityKit'; 545import { rpc } from '@kit.IPCKit'; 546import { BusinessError } from '@kit.BasicServicesKit'; 547 548let connId = particleAbility.connectAbility( 549 { 550 bundleName: 'com.ix.ServiceAbility', 551 abilityName: 'ServiceAbilityA', 552 }, 553 { 554 onConnect: (element, remote) => { 555 console.log(`ConnectAbility onConnect remote is proxy: ${(remote instanceof rpc.RemoteProxy)}`); 556 }, 557 onDisconnect: (element) => { 558 console.log(`ConnectAbility onDisconnect element.deviceId: ${element.deviceId}`); 559 }, 560 onFailed: (code) => { 561 console.error(`particleAbilityTest ConnectAbility onFailed errCode: ${code}`); 562 }, 563 }, 564); 565 566particleAbility.disconnectAbility(connId).then(() => { 567 console.log('disconnectAbility success'); 568}).catch((error: BusinessError) => { 569 console.error(`particleAbilityTest result errCode : ${error.code}`); 570}); 571``` 572## ErrorCode 573 574Enumerates the error codes that may be returned when an ability is started. 575 576**System capability**: SystemCapability.Ability.AbilityRuntime.FAModel 577 578| Name | Value | Description | 579| ------------------------------ | ---- | ---------------------------------------- | 580| INVALID_PARAMETER | -1 | Invalid parameter.| 581