无长时任务的应用退至后台后,应释放对应资源,避免阻止系统休眠。
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锁。
使用音频资源时,系统会为应用持锁。如果不释放音频资源,会导致系统持锁不释放。因此,应用在后台应主动释放音频资源。
可参考合理使用音频资源。
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) => {});
}
}