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

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

加油哦,周末愉快~

结尾

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

相关推荐
冯志浩6 小时前
Harmony NEXT:如何给数据库添加自定义分词
harmonyos·掘金·金石计划
爱桥代码的程序媛8 小时前
鸿蒙OpenHarmony【轻量系统芯片移植案例】标准系统方案之瑞芯微RK3568移植案例
嵌入式硬件·harmonyos·鸿蒙·鸿蒙系统·移植·openharmony·鸿蒙开发
AORO_BEIDOU9 小时前
防爆手机+鸿蒙系统,遨游通讯筑牢工业安全基石
5g·安全·智能手机·信息与通信·harmonyos
小强在此1 天前
【基于开源鸿蒙(OpenHarmony)的智慧农业综合应用系统】
华为·开源·团队开发·智慧农业·harmonyos·开源鸿蒙
PlumCarefree1 天前
基于鸿蒙API10的RTSP播放器(四:沉浸式播放窗口)
华为·harmonyos
中关村科金1 天前
中关村科金推出得助音视频鸿蒙SDK,助力金融业务系统鸿蒙化提速
华为·音视频·harmonyos
小强在此1 天前
基于OpenHarmony(开源鸿蒙)的智慧医疗综合应用系统
华为·开源·团队开发·健康医疗·harmonyos·开源鸿蒙
奔跑的露西ly2 天前
【鸿蒙 HarmonyOS NEXT】popup弹窗
华为·harmonyos
OH五星上将2 天前
OpenHarmony(鸿蒙南向开发)——轻量和小型系统三方库移植指南(一)
嵌入式硬件·移动开发·harmonyos·openharmony·鸿蒙开发·鸿蒙移植