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