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

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

加油哦,周末愉快~

结尾

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

相关推荐
whysqwhw5 小时前
鸿蒙分布式投屏
harmonyos
whysqwhw6 小时前
鸿蒙AVSession Kit
harmonyos
whysqwhw8 小时前
鸿蒙各种生命周期
harmonyos
whysqwhw9 小时前
鸿蒙音频编码
harmonyos
whysqwhw9 小时前
鸿蒙音频解码
harmonyos
whysqwhw9 小时前
鸿蒙视频解码
harmonyos
whysqwhw10 小时前
鸿蒙视频编码
harmonyos
ajassi200010 小时前
开源 Arkts 鸿蒙应用 开发(十八)通讯--Ble低功耗蓝牙服务器
华为·开源·harmonyos
前端世界11 小时前
在鸿蒙应用中快速接入地图功能:从配置到实战案例全解析
华为·harmonyos
江拥羡橙12 小时前
【基础-判断】HarmonyOS提供了基础的应用加固安全能力,包括混淆、加密和代码签名能力
安全·华为·typescript·harmonyos