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}`);
    }

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

加油哦,周末愉快~

结尾

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

相关推荐
加农炮手Jinx12 小时前
Flutter 组件 ubuntu_service 适配鸿蒙 HarmonyOS 实战:底层系统服务治理,构建鸿蒙 Linux 子系统与守护进程交互架构
flutter·harmonyos·鸿蒙·openharmony·ubuntu_service
里欧跑得慢12 小时前
Flutter 三方库 mobx_codegen — 自动化驱动的高性能响应式状态管理(适配鸿蒙 HarmonyOS Next ohos)
flutter·自动化·harmonyos
王码码203512 小时前
Flutter 三方库 login_client 的鸿蒙化适配指南 - 打造工业级安全登录、OAuth2 自动化鉴权、鸿蒙级身份守门员
flutter·harmonyos·鸿蒙·openharmony·login_client
星辰徐哥12 小时前
鸿蒙金融理财全栈项目——上线与运维、用户反馈、持续迭代
运维·金融·harmonyos
加农炮手Jinx12 小时前
Flutter 三方库 cloudflare 鸿蒙云边协同分发流适配精讲:直连全球高速存储网关阵列无缝吞吐海量动静态画像资源,构筑大吞吐业务级网络负载安全分流-适配鸿蒙 HarmonyOS ohos
网络·flutter·harmonyos
大雷神16 小时前
HarmonyOS APP<玩转React>开源教程十一:组件化开发概述
前端·react.js·harmonyos
国医中兴19 小时前
Flutter 三方库 dson 的鸿蒙化适配指南 - 极简的序列化魔法、在鸿蒙端实现反射式 JSON 映射实战
flutter·harmonyos·鸿蒙·openharmony
池央21 小时前
在鸿蒙上跑 AI Agent:JiuwenClaw-on-OpenHarmony 完整实战
人工智能·华为·harmonyos
互联网散修21 小时前
零基础鸿蒙应用开发第五节:基础数据类型与对象类型转换
华为·harmonyos·鸿蒙应用开发入门教程
弓.长.21 小时前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-fast-image — 高性能图片加载组件
react native·react.js·harmonyos