harmony 鸿蒙@ohos.file.BackupExtensionContext (Backup and Restore Extension Capability)

  • 2025-06-12
  • 浏览 (3)

@ohos.file.BackupExtensionContext (Backup and Restore Extension Capability)

BackupExtensionContext is the context of BackupExtension and inherits from ExtensionContext.

The BackupExtensionContext module provides the capability for accessing a specific BackupExtension. For a BackupExtension, you can directly use BackupExtensionContext as the context or define a context that inherits from BackupExtensionContext.

NOTE

  • The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs of this module can be used only in the stage model.

Modules to Import

import  { BackupExtensionContext } from '@kit.CoreFileKit';

Properties

System capability: SystemCapability.FileManagement.StorageService.Backup

Name Type Read-Only Optional Description
backupDir12+ string Yes No Temporary directory used for backup or restore. This directory cannot be used for other purposes. Currently, only el1/ and el2/ are supported.

When to Use

BackupExtensionContext is used to obtain a temporary directory for backup or restore.

Example

import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
import { contextConstant } from '@kit.AbilityKit';

export default class MyBackupExtAbility extends BackupExtensionAbility {
    async onBackup() {
        console.log("onBackup begin");
        // You can modify this.context.area to change the sandbox path corresponding to el1/ or el2/.
        this.context.area = contextConstant.AreaMode.EL1;
        // Use this.context.backupDir to obtain the sandbox path.
        let dir = this.context.backupDir;
        console.log(`onBackup el1 dir: ${dir}`);
        this.context.area = contextConstant.AreaMode.EL2;
        dir = this.context.backupDir;
        console.log(`onBackup el2 dir: ${dir}`);
        console.log("onBackup end");
    }

    async onRestore() {
        console.log("onRestore begin");
        this.context.area = contextConstant.AreaMode.EL1;
        let dir = this.context.backupDir;
        console.log(`onRestore el1 dir: ${dir}`);
        this.context.area = contextConstant.AreaMode.EL2;
        dir = this.context.backupDir;
        console.log(`onRestore el2 dir: ${dir}`);
        console.log("onRestore end");
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

0  赞