目录

后台系统资源合理使用

无长时任务的应用退至后台后,应释放对应资源,避免阻止系统休眠。

约束

接口runningLock.create的type参数BACKGROUND类型已废弃,不建议使用。如果确实需要使用,后台运行时必须主动释放锁。

示例

[h2]应用直接持锁

import { runningLock } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// Return to the background to release the lock
runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND_USER_IDLE)
  .then((lock: runningLock.RunningLock) => {
    try {
      lock.unhold();
    } catch (error) {
      let err = error as BusinessError;
      hilog.warn(0x0000, 'testTag', `setColorMode failed, code=${err.code}, message=${err.message}`);
    }
  })
  .catch((err: Error) => {
    console.error('create running lock failed, err: ' + err);
  });

有关RunningLock开发相关接口的使用,详情可以参考RunningLock锁。

[h2]系统帮助应用持锁

使用音频资源时,系统会为应用持锁。如果不释放音频资源,会导致系统持锁不释放。因此,应用在后台应主动释放音频资源。

可参考合理使用音频资源。

import { UIAbility } from '@kit.AbilityKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
// ...
let audioRenderer: audio.AudioRenderer;
// ...
export default class EntryAbility extends UIAbility {
  // ...

  onForeground(): void {
    //Apply for the resources required by the system, or reapply for the resources released in onBackground ()
    audio.createAudioRenderer(audioRendererOptions,(err: BusinessError) => {});
  }

  onBackground(): void {
    //Release resources when the UI is invisible
    audioRenderer.stop((err: BusinessError) => {});
  }
}