HarmonyOS/OpenHarmony (Stage model) card development application context Context usage scenario 1

1. Obtain the application file path.
The base class Context provides the ability to obtain the application file path. ApplicationContext, AbilityStageContext, UIAbilityContext and ExtensionContext all inherit this ability. The application file path belongs to the application sandbox path. The application file paths obtained by the above types of Context are different.

Obtain the application-level application file path through ApplicationContext. This path is the recommended storage path for application global information. These files will be deleted when the application is uninstalled.
 

#2023Blind Box+Code#HarmonyOS/OpenHarmony (Stage model) Card Development Application Context Context Usage Scenario 1 - Open Source Basic Software Community


Obtain the HAP-level application file path through AbilityStageContext, UIAbilityContext, and ExtensionContext. This path is the recommended storage path for HAP-related information. These files will be deleted following the uninstallation of HAP, but will not affect the files in the application-level path unless all HAPs of the application have been uninstalled.
 

#2023Blind Box+Code#HarmonyOS/OpenHarmony (Stage model) Card Development Application Context Context Usage Scenario 1 - Open Source Basic Software Community


Sample code is as follows.

import UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
    onCreate(want, launchParam) {
        let cacheDir = this.context.cacheDir;
        let tempDir = this.context.tempDir;
        let filesDir = this.context.filesDir;
        let databaseDir = this.context.databaseDir;
        let bundleCodeDir = this.context.bundleCodeDir;
        let distributedFilesDir = this.context.distributedFilesDir;
        let preferencesDir = this.context.preferencesDir;
        // ...
    }
}

2. Obtain and modify the encrypted partition
. In the previous scenario, the concept of encryption level was introduced. The current encrypted partition is obtained and set by reading and writing the area attribute of Context. The following two encryption levels are supported: AreaMode.EL1: Device
level Encrypted area, a data area accessible after the device is powered on.

AreaMode.EL2: User-level encryption area, a data area that can only be accessed after the device is turned on and the password is entered for the first time.

import UIAbility from '@ohos.app.ability.UIAbility';

export default class EntryAbility extends UIAbility {
    onCreate(want, launchParam) {
        // 存储普通信息前,切换到EL1设备级加密
        if (this.context.area === 1) { // 获取area
            this.context.area = 0;     // 修改area
        }
        // 存储普通信息

        // 存储敏感信息前,切换到EL2用户级加密
        if (this.context.area === 0) { // 获取area
            this.context.area = 1;     // 修改area
        }
        // 存储敏感信息
    }
}

Guess you like

Origin blog.csdn.net/weixin_69135651/article/details/132496594