目录

Process Management

Since API version 26.0.0, you can query and set process management policies, and query, add, and delete process management information.

Use Cases

Provides process management capabilities for apps, including:

API Description

For details about these APIs, please refer to the API Reference.

API Description
getManagedProcessPolicy(): Promise<ManagedProcessPolicy | null> Obtains the process management policies on the current device using a promise.
setManagedProcessPolicy(policy: ManagedProcessPolicy): Promise<void> Sets the process management policies on the current device using a promise.
getManagedProcessList(): Promise<ManagedProcessInfo[]> Obtains the list of managed processes on the current device using a promise.
addManagedProcess(processInfo: ManagedProcessInfo): Promise<void> Adds a process to be managed using a promise.
removeManagedProcess(processInfo: ManagedProcessInfo): Promise<void> Deletes a managed process using a promise.

Development Procedure

  1. Import required modules.

    import { process } from '@kit.ArkTS';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { fileGuard } from '@kit.EnterpriseDataGuardKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Initialize the guard object of FileGuard and call the setManagedProcessPolicy API to set the process management policies for the current device.

    const TAG: string = 'FileGuard_ManagedProcess';
    const DOMAIN: number = 0x0000;
    
    // ...
    /**
     * Set the process management policies for the current device and return the result asynchronously using a promise.
     */
    function testSetManagedProcessPolicy() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      let policy: fileGuard.ManagedProcessPolicy = {
        status: fileGuard.ManagedProcessStatus.TIME_BASED,
        time: 3000
      };
      guard.setManagedProcessPolicy(policy).then(() => {
        hilog.info(DOMAIN, TAG, `Succeeded in setting the managed process policy.`);
      }).catch((err: BusinessError) => {
        hilog.error(DOMAIN, TAG, `Failed to set the managed process policy. Code: ${err.code}, message: ${err.message}.`);
      });
    }
  3. Initialize the guard object of FileGuard and call the getManagedProcessPolicy API to query the management policies for managed processes on the current device.

    const TAG: string = 'FileGuard_ManagedProcess';
    const DOMAIN: number = 0x0000;
    
    /**
     * Obtain the management policies for managed processes on the current device and return the result asynchronously using a promise.
     */
    function testGetManagedProcessPolicy() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      guard.getManagedProcessPolicy().then((policy: fileGuard.ManagedProcessPolicy | null) => {
        if (policy === null) {
          hilog.info(DOMAIN, TAG, `The managed process policy is null.`);
          return;
        }
        hilog.info(DOMAIN, TAG,
          `Succeeded in getting the managed process policy. status: ${policy.status}, time: ${policy.time}`);
      }).catch((err: BusinessError) => {
        hilog.error(DOMAIN, TAG, `Failed to get the managed process policy. Code: ${err.code}, message: ${err.message}.`);
      });
    }
  4. Initialize the guard object of FileGuard and call the addManagedProcess API to add a process to be managed.

    const TAG: string = 'FileGuard_ManagedProcess';
    const DOMAIN: number = 0x0000;
    
    // ...
    /**
     * Add a process to be managed and return the result asynchronously using a promise.
     */
    function testAddManagedProcess() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      let processInfo: fileGuard.ManagedProcessInfo = {
        pid: process.pid
      };
      guard.addManagedProcess(processInfo).then(() => {
        hilog.info(DOMAIN, TAG, `Succeeded in adding the managed process.`);
      }).catch((err: BusinessError) => {
        hilog.error(DOMAIN, TAG, `Failed to add the managed process. Code: ${err.code}, message: ${err.message}.`);
      });
    }
  5. Initialize the guard object of FileGuard and call the getManagedProcessList API to query the management policies for managed processes on the current device.

    const TAG: string = 'FileGuard_ManagedProcess';
    const DOMAIN: number = 0x0000;
    
    // ...
    /**
     * Obtain the list of managed processes on the current device and return the result asynchronously using a promise.
     */
    function testGetManagedProcessList() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      guard.getManagedProcessList().then((infos: fileGuard.ManagedProcessInfo[]) => {
        hilog.info(DOMAIN, TAG, `Succeeded in getting the managed process list.`);
        infos.forEach((info: fileGuard.ManagedProcessInfo) => {
          hilog.info(DOMAIN, TAG, `ManagedProcessInfo pid: ${info.pid}, policy: ${info.policy}`);
        });
      }).catch((err: BusinessError) => {
        hilog.error(DOMAIN, TAG, `Failed to get the managed process list. Code: ${err.code}, message: ${err.message}.`);
      });
    }
  6. Initialize the guard object of FileGuard and call the removeManagedProcess API to delete a managed process.

    const TAG: string = 'FileGuard_ManagedProcess';
    const DOMAIN: number = 0x0000;
    
    // ...
    /**
     * Delete a managed process and return the result asynchronously using a promise.
     */
    function testRemoveManagedProcess() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      let processInfo: fileGuard.ManagedProcessInfo = {
        pid: process.pid,
        policy: 'Tag1'
      };
      guard.removeManagedProcess(processInfo).then(() => {
        hilog.info(DOMAIN, TAG, `Succeeded in removing the managed process.`);
      }).catch((err: BusinessError) => {
        hilog.error(DOMAIN, TAG, `Failed to remove the managed process. Code: ${err.code}, message: ${err.message}.`);
      });
    }