js-apis-application-appManager.md 20.5 KB
Newer Older
1
# @ohos.application.appManager (appManager)
2 3 4 5 6 7 8 9 10 11

The **appManager** module implements application management. You can use the APIs of this module to query whether the application is undergoing a stability test, whether the application is running on a RAM constrained device, the memory size of the application, and information about the running process.

> **NOTE**
> 
> The APIs of this module are supported since API version 7 and deprecated since API version 9. You are advised to use [@ohos.app.ability.appManager](js-apis-app-ability-appManager.md) instead. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```ts
12
import appManager from '@ohos.application.appManager';
13 14 15 16 17 18 19 20 21 22 23 24
```

## appManager.isRunningInStabilityTest<sup>8+</sup>

static isRunningInStabilityTest(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether this application is undergoing a stability test. This API uses an asynchronous callback to return the result.

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

**Parameters**

25 26 27
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the result. If the application is undergoing a stability test, **true** will be returned; otherwise, **false** will be returned.| 
28 29 30 31

**Example**
    
  ```ts
32 33 34 35 36 37
  appManager.isRunningInStabilityTest((error, flag) => {
    if (error && error.code !== 0) {
        console.error('isRunningInStabilityTest fail, error: ${JSON.stringify(error)}');
    } else {
        console.log('isRunningInStabilityTest success, the result is: ${JSON.stringify(flag)}');
    }
38
  });
39 40 41 42 43 44 45 46 47 48 49 50 51
  ```


## appManager.isRunningInStabilityTest<sup>8+</sup>

static isRunningInStabilityTest(): Promise&lt;boolean&gt;

Checks whether this application is undergoing a stability test. This API uses a promise to return the result.

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

**Return value**

52 53 54
  | Type| Description| 
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise used to return the result. If the application is undergoing a stability test, **true** will be returned; otherwise, **false** will be returned.| 
55 56 57 58

**Example**
    
  ```ts
59
  appManager.isRunningInStabilityTest().then((flag) => {
60
      console.log('The result of isRunningInStabilityTest is: ${JSON.stringify(flag)}');
61
  }).catch((error) => {
62
      console.error('error: ${JSON.stringify(error)}');
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  });
  ```


## appManager.isRamConstrainedDevice

isRamConstrainedDevice(): Promise\<boolean>;

Checks whether this application is running on a RAM constrained device. This API uses a promise to return the result.

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

**Return value**

77 78 79
  | Type| Description| 
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise used to return whether the application is running on a RAM constrained device. If the application is running on a RAM constrained device, **true** will be returned; otherwise, **false** will be returned.| 
80 81 82 83

**Example**
    
  ```ts
84
  appManager.isRamConstrainedDevice().then((data) => {
85
      console.log('The result of isRamConstrainedDevice is: ${JSON.stringify(data)}');
86
  }).catch((error) => {
87
      console.error('error: ${JSON.stringify(error)}');
88 89 90 91 92 93 94 95 96 97 98 99 100
  });
  ```

## appManager.isRamConstrainedDevice

isRamConstrainedDevice(callback: AsyncCallback\<boolean>): void;

Checks whether this application is running on a RAM constrained device. This API uses an asynchronous callback to return the result.

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

**Parameters**

101 102 103
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the application is running on a RAM constrained device. If the application is running on a RAM constrained device, **true** will be returned; otherwise, **false** will be returned.| 
104 105 106 107

**Example**
    
  ```ts
108 109 110 111 112 113
  appManager.isRamConstrainedDevice((error, data) => {
      if (error && error.code !== 0) {
          console.error('isRamConstrainedDevice fail, error: ${JSON.stringify(error)}');
      } else {
          console.log('The result of isRamConstrainedDevice is: ${JSON.stringify(data)}');
      }
114
  });
115 116 117 118 119 120 121 122 123 124 125 126
  ```

## appManager.getAppMemorySize

getAppMemorySize(): Promise\<number>;

Obtains the memory size of this application. This API uses a promise to return the result.

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

**Return value**

127 128 129
  | Type| Description| 
  | -------- | -------- |
  | Promise&lt;number&gt; | Promise used to return the memory size, in MB.| 
130 131 132 133

**Example**
    
  ```ts
134
  appManager.getAppMemorySize().then((data) => {
135
      console.log('The size of app memory is: ${JSON.stringify(data)}');
136
  }).catch((error) => {
137
      console.error('error: ${JSON.stringify(error)}');
138 139 140 141 142 143 144 145 146 147 148 149 150
  });
  ```

## appManager.getAppMemorySize

getAppMemorySize(callback: AsyncCallback\<number>): void;

Obtains the memory size of this application. This API uses an asynchronous callback to return the result.

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

**Parameters**

151 152 153
  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;number&gt; | Yes| Callback used to return the memory size, in MB.| 
154 155 156 157

**Example**
    
  ```ts
158 159 160 161 162 163
  appManager.getAppMemorySize((error, data) => {
      if (error && error.code !== 0) {
          console.error('getAppMemorySize fail, error: ${JSON.stringify(error)}');
      } else {
          console.log('The size of app memory is: ${JSON.stringify(data)}');
      }
164
  });
165 166 167 168 169 170 171
  ```
## appManager.getProcessRunningInfos<sup>(deprecated)</sup>

getProcessRunningInfos(): Promise\<Array\<ProcessRunningInfo>>;

Obtains information about the running processes. This API uses a promise to return the result.

172
> This API is deprecated since API version 9. You are advised to use [appManager.getRunningProcessInformation<sup>9+</sup>](js-apis-app-ability-appManager.md#appmanagergetrunningprocessinformation) instead.
173 174 175 176 177 178 179 180 181

**Required permissions**: ohos.permission.GET_RUNNING_INFO

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

**Return value**

| Type| Description|
| -------- | -------- |
182
| Promise\<Array\<[ProcessRunningInfo](js-apis-inner-application-processRunningInfo.md)>> | Promise used to return the process information.|
183 184 185 186

**Example**
    
  ```ts
187
  appManager.getProcessRunningInfos().then((data) => {
188
      console.log('The process running infos is: ${JSON.stringify(data)}');
189
  }).catch((error) => {
190
      console.error('error: ${JSON.stringify(error)}');
191 192 193 194 195 196 197 198 199
  });
  ```

## appManager.getProcessRunningInfos<sup>(deprecated)</sup>

getProcessRunningInfos(callback: AsyncCallback\<Array\<ProcessRunningInfo>>): void;

Obtains information about the running processes. This API uses an asynchronous callback to return the result.

200
> This API is deprecated since API version 9. You are advised to use [appManager.getRunningProcessInformation<sup>9+</sup>](js-apis-app-ability-appManager.md#appmanagergetrunningprocessinformation9) instead.
201 202 203 204 205 206 207 208 209

**Required permissions**: ohos.permission.GET_RUNNING_INFO

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
210
| callback | AsyncCallback\<Array\<[ProcessRunningInfo](js-apis-inner-application-processRunningInfo.md)>> | Yes| Obtains information about the running processes. This API uses a promise to return the result.|
211 212 213 214

**Example**
    
  ```ts
215 216 217 218 219 220
  appManager.getProcessRunningInfos((error, data) => {
      if (error && error.code !== 0) {
          console.error('getProcessRunningInfos fail, error: ${JSON.stringify(error)}');
      } else {
          console.log('getProcessRunningInfos success, data: ${JSON.stringify(data)}');
      }
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  });
  ```

## appManager.registerApplicationStateObserver<sup>8+</sup>

registerApplicationStateObserver(observer: ApplicationStateObserver): number;

Registers an observer to listen for the state changes of all applications.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| observer | [ApplicationStateObserver](js-apis-inner-application-applicationStateObserver.md) | Yes| Numeric code of the observer.|

**Example**
    
  ```ts
245
  let applicationStateObserver = {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    onForegroundApplicationChanged(appStateData) {
        console.log('------------ onForegroundApplicationChanged -----------', appStateData);
    },
    onAbilityStateChanged(abilityStateData) {
        console.log('------------ onAbilityStateChanged -----------', abilityStateData);
    },
    onProcessCreated(processData) {
        console.log('------------ onProcessCreated -----------', processData);
    },
    onProcessDied(processData) {
        console.log('------------ onProcessDied -----------', processData);
    },
    onProcessStateChanged(processData) {
        console.log('------------ onProcessStateChanged -----------', processData);
    }
261
  };
262
  const observerCode = appManager.registerApplicationStateObserver(applicationStateObserver);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
  console.log('-------- observerCode: ---------', observerCode);
  ```

## appManager.unregisterApplicationStateObserver<sup>8+</sup>

unregisterApplicationStateObserver(observerId: number,  callback: AsyncCallback\<void>): void;

Deregisters the application state observer. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**
279

280 281 282 283 284 285 286 287
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| observerId | number | Yes| Numeric code of the observer.|
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**
    
  ```ts
288
  let observerId = 100;
289 290 291

  function unregisterApplicationStateObserverCallback(err) {
    if (err) {
292
        console.error('------------ unregisterApplicationStateObserverCallback ------------', err);
293 294
    }
  }
295
  appManager.unregisterApplicationStateObserver(observerId, unregisterApplicationStateObserverCallback);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  ```

## appManager.unregisterApplicationStateObserver<sup>8+</sup>

unregisterApplicationStateObserver(observerId: number): Promise\<void>;

Deregisters the application state observer. This API uses a promise to return the result.

**Required permissions**: ohos.permission.RUNNING_STATE_OBSERVER

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| observerId | number | Yes| Numeric code of the observer.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

**Example**
    
  ```ts
325
  let observerId = 100;
326

327
  appManager.unregisterApplicationStateObserver(observerId)
328 329 330 331
  .then((data) => {
      console.log('----------- unregisterApplicationStateObserver success ----------', data);
  })
  .catch((err) => {
332
      console.error('----------- unregisterApplicationStateObserver fail ----------', err);
333
  });
334 335 336 337 338 339
  ```

## appManager.getForegroundApplications<sup>8+</sup>

getForegroundApplications(callback: AsyncCallback\<Array\<AppStateData>>): void;

340
Obtains information about the applications that are running in the foreground. This API uses an asynchronous callback to return the result. The application information is defined by [AppStateData](js-apis-inner-application-appStateData.md).
G
Gloria 已提交
341

342 343 344 345 346 347 348 349 350 351
**Required permissions**: ohos.permission.GET_RUNNING_INFO

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
352
| callback | AsyncCallback\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Yes| Callback used to return the application information.|
353 354 355 356 357 358

**Example**
    
  ```ts
  function getForegroundApplicationsCallback(err, data) {
    if (err) {
359
        console.error('--------- getForegroundApplicationsCallback fail ---------', err);
360
    } else {
361
        console.log('--------- getForegroundApplicationsCallback success ---------', data);
362 363
    }
  }
364
  appManager.getForegroundApplications(getForegroundApplicationsCallback);
365 366 367 368 369 370
  ```

## appManager.getForegroundApplications<sup>8+</sup>

getForegroundApplications(): Promise\<Array\<AppStateData>>;

371
Obtains information about the applications that are running in the foreground. This API uses a promise to return the result. The application information is defined by [AppStateData](js-apis-inner-application-appStateData.md).
372 373 374 375 376 377 378 379 380 381 382

**Required permissions**: ohos.permission.GET_RUNNING_INFO

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

**System API**: This is a system API and cannot be called by third-party applications.

**Return value**

| Type| Description|
| -------- | -------- |
383
| Promise\<Array\<[AppStateData](js-apis-inner-application-appStateData.md)>> | Promise used to return the application information.|
384 385 386 387

**Example**
    
  ```ts
388
  appManager.getForegroundApplications()
389 390 391 392
  .then((data) => {
      console.log('--------- getForegroundApplications success -------', data);
  })
  .catch((err) => {
393
      console.error('--------- getForegroundApplications fail -------', err);
394
  });
395 396 397 398 399 400 401 402
  ```

## appManager.killProcessWithAccount<sup>8+</sup>

killProcessWithAccount(bundleName: string, accountId: number): Promise\<void\>

Kills a process by bundle name and account ID. This API uses a promise to return the result.

403
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user) and ohos.permission.CLEAN_BACKGROUND_PROCESSES
404 405 406 407 408 409 410

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

411 412 413 414
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| bundleName | string | Yes| Bundle name.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
415 416 417 418

**Example**

```ts
419 420
let bundleName = 'bundleName';
let accountId = 0;
421
appManager.killProcessWithAccount(bundleName, accountId)
422 423 424 425
   .then((data) => {
       console.log('------------ killProcessWithAccount success ------------', data);
   })
   .catch((err) => {
426
       console.error('------------ killProcessWithAccount fail ------------', err);
427
   });
428 429 430 431 432 433 434 435 436 437 438 439 440
```


## appManager.killProcessWithAccount<sup>8+</sup>

killProcessWithAccount(bundleName: string, accountId: number, callback: AsyncCallback\<void\>): void

Kills a process by bundle name and account ID. This API uses an asynchronous callback to return the result.

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

**System API**: This is a system API and cannot be called by third-party applications.

441
**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS (required only when the account ID is not the current user) and ohos.permission.CLEAN_BACKGROUND_PROCESSES
442 443 444

**Parameters**

445 446 447 448 449
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| bundleName | string | Yes| Bundle name.|
| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).|
| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.|
450 451 452 453

**Example**

```ts
454 455
let bundleName = 'bundleName';
let accountId = 0;
456 457
function killProcessWithAccountCallback(err, data) {
   if (err) {
458
       console.error('------------- killProcessWithAccountCallback fail, err: --------------', err);
459 460 461 462
   } else {
       console.log('------------- killProcessWithAccountCallback success, data: --------------', data);
   }
}
463
appManager.killProcessWithAccount(bundleName, accountId, killProcessWithAccountCallback);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
```

## appManager.killProcessesByBundleName<sup>8+</sup>

killProcessesByBundleName(bundleName: string, callback: AsyncCallback\<void>);

Kills a process by bundle name. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
482
| bundleName | string | Yes| Bundle name.|
483 484 485 486 487
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**
    
  ```ts
488
  let bundleName = 'bundleName';
489 490
  function killProcessesByBundleNameCallback(err, data) {
    if (err) {
491
        console.error('------------- killProcessesByBundleNameCallback fail, err: --------------', err);
492 493 494 495
    } else {
        console.log('------------- killProcessesByBundleNameCallback success, data: --------------', data);
    }
  }
496
  appManager.killProcessesByBundleName(bundleName, killProcessesByBundleNameCallback);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  ```

## appManager.killProcessesByBundleName<sup>8+</sup>

killProcessesByBundleName(bundleName: string): Promise\<void>;

Kills a process by bundle name. This API uses a promise to return the result.

**Required permissions**: ohos.permission.CLEAN_BACKGROUND_PROCESSES

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
515
| bundleName | string | Yes| Bundle name.|
516 517 518 519 520 521 522 523

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

**Example**
524

525
  ```ts
526
  let bundleName = 'com.example.myapplication';
527
  appManager.killProcessesByBundleName(bundleName)
528 529 530 531
    .then((data) => {
        console.log('------------ killProcessesByBundleName success ------------', data);
    })
    .catch((err) => {
532
        console.error('------------ killProcessesByBundleName fail ------------', err);
533
    });
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  ```

## appManager.clearUpApplicationData<sup>8+</sup>

clearUpApplicationData(bundleName: string, callback: AsyncCallback\<void>);

Clears application data by bundle name. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
552
| bundleName | string | Yes| Bundle name.|
553 554 555 556 557
| callback | AsyncCallback\<void> | Yes| Callback used to return the result.|

**Example**
    
  ```ts
558
  let bundleName = 'bundleName';
559 560
  function clearUpApplicationDataCallback(err, data) {
    if (err) {
561
        console.error('------------- clearUpApplicationDataCallback fail, err: --------------', err);
562 563 564 565
    } else {
        console.log('------------- clearUpApplicationDataCallback success, data: --------------', data);
    }
  }
566
  appManager.clearUpApplicationData(bundleName, clearUpApplicationDataCallback);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
  ```

## appManager.clearUpApplicationData<sup>8+</sup>

clearUpApplicationData(bundleName: string): Promise\<void>;

Clears application data by bundle name. This API uses a promise to return the result.

**Required permissions**: ohos.permission.CLEAN_APPLICATION_DATA

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

**System API**: This is a system API and cannot be called by third-party applications.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
585
| bundleName | string | Yes| Bundle name.|
586 587 588 589 590 591 592 593 594 595

**Return value**

| Type| Description|
| -------- | -------- |
| Promise\<void> | Promise used to return the result.|

**Example**
    
  ```ts
596
  let bundleName = 'bundleName';
597
  appManager.clearUpApplicationData(bundleName)
598 599 600 601
    .then((data) => {
        console.log('------------ clearUpApplicationData success ------------', data);
    })
    .catch((err) => {
602
        console.error('------------ clearUpApplicationData fail ------------', err);
603
    });
604
  ```