We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

System Resources

After an application without a continuous task is switched to the background, it should release the corresponding resources to facilitate system hibernation.

Constraints

The BACKGROUND type of the runningLock.create API has been deprecated. If this type is required, the lock must be released when the application runs in the background.

Example

Direct App Lock

Collapse
Word wrap
Dark theme
Copy code
  1. import { runningLock } from '@kit.BasicServicesKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. // Return to the background to release the lock
  5. runningLock.create('running_lock_test', runningLock.RunningLockType.BACKGROUND)
  6. .then((lock: runningLock.RunningLock) => {
  7. try {
  8. lock.unhold();
  9. } catch (error) {
  10. let err = error as BusinessError;
  11. hilog.warn(0x000, 'testTag', `setColorMode failed, code=${err.code}, message=${err.message}`);
  12. }
  13. })
  14. .catch((err: Error) => {
  15. console.error('create running lock failed, err: ' + err);
  16. });

For details about how to use the APIs related to RunningLock development, see RunningLock.

System-Assisted App Lock

When audio resources are used, the system locks the audio resources for the application. If the audio resources are not released, the lock stays held. Therefore, the application should release the audio resources proactively in the background.

For details, see Proper Use of Audio Resources.

Collapse
Word wrap
Dark theme
Copy code
  1. import { UIAbility } from '@kit.AbilityKit';
  2. import { audio } from '@kit.AudioKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. // ...
  5. export default class EntryAbility extends UIAbility {
  6. // ...
  7. onForeground(): void {
  8. //Apply for the resources required by the system, or reapply for the resources released in onBackground ()
  9. audio.createAudioRenderer(audioRendererOptions,(err: BusinessError) => {});
  10. }
  11. onBackground(): void {
  12. //Release resources when the UI is invisible
  13. audioRenderer.stop((err: BusinessError) => {});
  14. }
  15. }
Search in Best Practices
Enter a keyword.