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

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

加油哦,周末愉快~

结尾

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

相关推荐
一只大侠的侠5 分钟前
Flutter开源鸿蒙跨平台训练营 Day17Calendar 日历组件开发全解
flutter·开源·harmonyos
前端世界27 分钟前
从一个 entry 写到十几个模块:鸿蒙模块化开发的真实落地方案(含可运行 Demo)
华为·harmonyos
一只大侠的侠36 分钟前
Flutter开源鸿蒙跨平台训练营 Day14React Native表单开发
flutter·开源·harmonyos
听麟39 分钟前
HarmonyOS 6.0+ APP AR文旅导览系统开发实战:空间定位与文物交互落地
人工智能·深度学习·华为·ar·wpf·harmonyos
空白诗1 小时前
高级进阶React Native 鸿蒙跨平台开发:slider 滑块组件 - 音量调节器完整实现
react native·react.js·harmonyos
●VON1 小时前
HarmonyOS应用开发实战(基础篇)Day01-《ArkTS基本知识》
学习·华为·harmonyos·鸿蒙·von
BlackWolfSky1 小时前
鸿蒙高级课程笔记2—应用性能优化
笔记·华为·harmonyos
ujainu2 小时前
护眼又美观:Flutter + OpenHarmony 鸿蒙记事本一键切换夜间模式(四)
android·flutter·harmonyos
一只大侠的侠2 小时前
Flutter开源鸿蒙跨平台训练营 Day 13从零开发注册页面
flutter·华为·harmonyos
森之鸟2 小时前
鸿蒙审核常见问题避坑指南_Scroll嵌套List_Grid滑动优化
华为·harmonyos