HarmonyOS应用开发-基础建设

HarmonyOS应用开发是目前是最火、最有前景的方向,现在入门鸿蒙开发还来得及。鸿蒙开发官网:developer.huawei.com/consumer/cn

大家好,我是德莱问,最近在尝试开发鸿蒙app,写文章主要是为了记录下自己的本周所学,对本周所了解到的知识有所回顾和整理归纳。

之前我是一个前端开发工程师,在现在鸿蒙大火的趋势下,现在转战鸿蒙,从零开始,小白做起,如果你也对鸿蒙感兴趣,欢迎点赞关注。正文从这里开始~

本周学习到的几个常用命令

  • 连接手机后,可能需要往手机上传文件,当upload提示权限不够的时候。

    可以执行以下命令:

    hdc shell mount -o remount,rw /

  • 连接手机后,获取设备的版本号和sn:

    • 版本号:hdc shell param get const.product.software.version
    • sn: hdc shell param get ohos.boot.sn

运行期间的缓存

运行期间的缓存,使用的是一个简单的对象进行存储,其实类似于js的内存对象。但是,因为合作的几个伙伴是安卓开发,习惯于class模式、单例模式,所以实现起来并不是那么前端。实现如下:

typescript 复制代码
// 定义&实现
export class MemoryStorage {
  private static instance: MemoryStorage;
  private _objects: Map<string, Object>;

  private constructor() {
    this._objects = new Map<string, Object>();
  }

  public static getStorage(): MemoryStorage {
    if (!MemoryStorage.instance) {
      MemoryStorage.instance = new MemoryStorage();
    }
    return MemoryStorage.instance;
  }

  getObject(key: string): Object | undefined {
    return this._objects.get(key);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }

  deleteObject(key: string): void {
    this._objects.delete(key);
  }
}

// 使用,存数据
MemoryStorage.getStorage().setObject('user', { name: '德莱问' });
// 使用,取数据
MemoryStorage.getStorage().getObject('user');

持久化缓存

鸿蒙官方提供了一个持久化缓存的库,使用起来也比较简单,不过不像前端的LocalStorage属于同步方法,鸿蒙的持久化缓存属于异步方法,调用的时候需要稍微注意一下。实现如下:

kotlin 复制代码
import dataPreferences from '@ohos.data.preferences';
import common from '@ohos.app.ability.common';

const storeName: string = 'private_store_name';

class PersistentStorage {
  private preferences?: dataPreferences.Preferences;
  private context = getContext(this) as common.UIAbilityContext;
  private static instance: PersistentStorage;

  constructor() {
    this.initPreference(storeName);
  }

  private async initPreference(storeName: string): Promise<void> {
    const preferences = await dataPreferences.getPreferences(this.context, storeName);
    this.preferences = preferences;
  }

  public static getInstance(): PersistentStorage {
    if (!PersistentStorage.instance) {
      PersistentStorage.instance = new PersistentStorage();
    }
    return PersistentStorage.instance;
  }

  async setValue<T>(key: string, value: T): Promise<void> {
    if (this.preferences) {
      await this.preferences.put(key, JSON.stringify(value));
      this.saveUserData();
    } else {
      await this.initPreference(storeName);
      this.setValue<T>(key, value);
    }
  }

  async getValue<T>(key: string): Promise<T | null> {
    if (this.preferences) {
      const res = await this.preferences.get(key, '');
      if (!res) {
        return null;
      }
      return safeParse<T>(res as string);
    } else {
      await this.initPreference(storeName);
      return this.getValue<T>(key);
    }
  }

  async hasValue(key: string): Promise<boolean> {
    if (this.preferences) {
      return this.preferences.has(key);
    } else {
      await this.initPreference(storeName);
      return this.hasValue(key);
    }
  }

  async deleteValue(key: string): Promise<void> {
    if (this.preferences) {
      await this.preferences.delete(key);
      this.saveUserData();
    } else {
      await this.initPreference(storeName);
      return this.deleteValue(key);
    }
  }

  private saveUserData() {
    this.preferences?.flush();
  }
}

// 使用,存数据
await PersistentStorage.getInstance().setValue('key', true);

// 使用,取数据
await PersistentStorage.getInstance().getValue<boolean>('key');

习惯了使用asyncawait语法糖的方式来写异步,写起来还是很方便的,不过有大小限制,不可超过8192个字符,还能给客户端的小伙伴们普及普及~~~

关于获取构建后的信息

  • 使用BuildProfile

    构建完成后会自动生成一个BuildProfile文件,可以直接引用此文件获取构建信息。

    文件内容为:

    javascript 复制代码
     export default class BuildProfile { 
      static readonly BUNDLE_NAME = 'com.baidu.demo';
      static readonly BUNDLE_TYPE = 'app';
      static readonly VERSION_CODE = 1234;
      static readonly VERSION_NAME = '1.2.34';
      static readonly TARGET_NAME = 'default';
      static readonly PRODUCT_NAME = 'default';
      static readonly BUILD_MODE_NAME = 'debug';
      static readonly DEBUG = true;
     }

    文件位置为:

  • 使用系统能力获取

    鸿蒙还提供了系统的一些api去获取其构建信息

    typescript 复制代码
    export function getAppInfo() {
      const appInfo = getContext(globalThis).applicationInfo;
      console.log(`bundle_name: ${appInfo.name}`);
      console.log(`bundle_type: ${appInfo.bundleType}`);
      console.log(`debug: ${appInfo.debug}`);
    }

本周就先说这些,不断学习,不断积累。鸿蒙对现在的我们来说,就像是刚刚起步时的苹果,面临着很大的挑战和机遇,希望大家可以抓住机遇,面对挑战。

加油哦,周末愉快~

结尾

欢迎关注公众号【丶德莱问】。

相关推荐
2501_944424126 小时前
Flutter for OpenHarmony游戏集合App实战之贪吃蛇食物生成
android·开发语言·flutter·游戏·harmonyos
不会写代码0006 小时前
Flutter 框架跨平台鸿蒙开发 - 全国景区门票查询应用开发教程
flutter·华为·harmonyos
猛扇赵四那边好嘴.7 小时前
Flutter 框架跨平台鸿蒙开发 - 旅行规划助手应用开发教程
flutter·华为·harmonyos
紫雾凌寒8 小时前
【 HarmonyOS 面试题】2026 最新 ArkTS 语言基础面试题
华为·面试·程序员·华为云·职场发展·harmonyos·arkts
摘星编程9 小时前
React Native鸿蒙:BiometricAuth指纹解锁实现
react native·react.js·harmonyos
2501_9444241210 小时前
Flutter for OpenHarmony游戏集合App实战之俄罗斯方块七种形状
android·开发语言·flutter·游戏·harmonyos
哈哈你是真的厉害11 小时前
小白基础入门 React Native 鸿蒙跨平台开发:实现一个简单的文件路径处理工具
react native·react.js·harmonyos
HMS Core11 小时前
【FAQ】HarmonyOS SDK 闭源开放能力 — Audio Kit
华为·harmonyos
哈哈你是真的厉害11 小时前
小白基础入门 React Native 鸿蒙跨平台开发:实现一个简单的个人所得税计算器
react native·react.js·harmonyos
小白阿龙12 小时前
鸿蒙+flutter 跨平台开发——汇率查询器开发实战
flutter·华为·harmonyos·鸿蒙