HarmonyOS开发实例:【app帐号管理】

2024-04-14 04:28

本文主要是介绍HarmonyOS开发实例:【app帐号管理】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

应用帐号管理

介绍

本示例选择应用进行注册/登录,并设置帐号相关信息,简要说明应用帐号管理相关功能。效果图如下:

效果预览

image.png

使用说明参考鸿蒙文档:qr23.cn/AKFP8k点击或者转到。

搜狗高速浏览器截图20240326151344.png

1.首页面选择想要进入的应用,首次进入该应用需要进行注册,如已注册帐号则直接登录。

2.注册页面可设置帐号名、邮箱、个性签名、密码(带*号为必填信息),注册完成后返回登录页面使用注册的帐号进行登录。

3.登录后进入帐号详情界面,点击修改信息按钮可跳转至帐号信息修改页面重新设置帐号信息。

4.点击切换应用按钮则退出该帐号并返回首页面。重新选择想要进入的应用。

5.点击删除帐号按钮则会删除该帐号所有相关信息。

代码解读

entry/src/main/ets/
|---common
|   |---AccountInfo.ets                    // 切换应用组件
|   |---BundleInfo.ets                     // 首页列表组件
|   |---LoginInfo.ets                      // 登录组件
|   |---ModifyInfo.ets                     // 修改信息组件
|   |---NavigationBar.ets                  // 路由跳转组件
|   |---RegisterInfo.ets                   // 注册组件
|---entryAbility
|   |---EntryAbility.ts             
|---model
|   |---AccountData.ts                     // 数据存储
|   |---AccountModel.ts                    // 数据管理
|   |---Logger.ts                          // 日志工具
|---pages
|   |---Index.ets                          // 首页
|   |---Account.ets                        // 切换应用页面
|   |---Login.ets                          // 登录页面
|   |---Modify.ets                         // 修改信息页面
|   |---Register.ets                       // 注册信息页面

具体实现

  • 本示例分为音乐,视频,地图三个模块

    • 音乐模块

      • 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面, createAppAccountManager方法创建应用帐号管理器对象
      • 源码链接:[AccountData.ts]
/** Copyright (c) 2022 Huawei Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import Logger from '../model/Logger'import common from '@ohos.app.ability.common'import preferences from '@ohos.data.preferences'const TAG: string = '[AccountData]'export class AccountData {static instance: AccountData = nullprivate storage: preferences.Preferences = nullpublic static getInstance() {if (this.instance === null) {this.instance = new AccountData()}return this.instance}async getFromStorage(context: common.Context, url: string) {let name = urlLogger.info(TAG, `Name is ${name}`)try {this.storage = await preferences.getPreferences(context, `${name}`)} catch (err) {Logger.error(`getStorage failed, code is ${err.code}, message is ${err.message}`)}if (this.storage === null) {Logger.info(TAG, `Create stroage is fail.`)}}async getStorage(context: common.Context, url: string) {this.storage = nullawait this.getFromStorage(context, url)return this.storage}async putStorageValue(context: common.Context, key: string, value: string, url: string) {this.storage = await this.getStorage(context, url)try {await this.storage.put(key, value)await this.storage.flush()Logger.info(TAG, `put key && value success`)} catch (err) {Logger.info(TAG, `aaaaaa put failed`)}return}async hasStorageValue(context: common.Context, key: string, url: string) {this.storage = await this.getStorage(context, url)let resulttry {result = await this.storage.has(key)} catch (err) {Logger.error(`hasStorageValue failed, code is ${err.code}, message is ${err.message}`)}Logger.info(TAG, `hasStorageValue success result is ${result}`)return result}async getStorageValue(context: common.Context, key: string, url: string) {this.storage = await this.getStorage(context, url)let getValuetry {getValue = await this.storage.get(key, 'null')} catch (err) {Logger.error(`getStorageValue failed, code is ${err.code}, message is ${err.message}`)}Logger.info(TAG, `getStorageValue success`)return getValue}async deleteStorageValue(context: common.Context, key: string, url: string) {this.storage = await this.getStorage(context, url)try {await this.storage.delete(key)await this.storage.flush()} catch (err) {Logger.error(`deleteStorageValue failed, code is ${err.code}, message is ${err.message}`)}Logger.info(TAG, `delete success`)return}}

[AccountModel.ts]

/** Copyright (c) 2022 Huawei Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/import Logger from '../model/Logger'import appAccount from '@ohos.account.appAccount'const TAG: string = '[AccountModel]'const app: appAccount.AppAccountManager = appAccount.createAppAccountManager()export class AccountModel {async addAccount(username: string) {await app.addAccount(username)Logger.info(TAG, `addAccount success`)return}async deleteAccount(username: string) {await app.deleteAccount(username)Logger.info(TAG, `deleteAccount success`)return}async setAccountCredential(username: string, credentialType: string, credential: string) {await app.setAccountCredential(username, credentialType, credential)Logger.info(TAG, `setAccountCredential success`)return}async setAccountExtraInfo(name: string, extraInfo: string) {await app.setAccountExtraInfo(name, extraInfo)Logger.info(TAG, `setAccountExtraInfo success`)return}async setAssociatedData(name: string, key: string, value: string) {await app.setAssociatedData(name, key, value)Logger.info(TAG, `setAssociatedData success`)return}async getAccountCredential(name: string, credentialType: string) {let result = await app.getAccountCredential(name, credentialType)Logger.info(TAG, `getAccountCredential success`)return result}async getAccountExtraInfo(name: string) {let result = await app.getAccountExtraInfo(name)Logger.info(TAG, `getAccountExtraInfo success`)return result}async getAssociatedData(name: string, key: string) {let result = await app.getAssociatedData(name, key)Logger.info(TAG, `getAssociatedData success`)return result}}
  • 接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]

    • 视频模块

      • 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面,createAppAccountManager方法创建应用帐号管理器对象
      • 源码链接:[AccountData.ts],[AccountModel.ts]
      • 接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]
    • 地图模块

      • 使用Navigation,Button,Text,TextInput组件开发注册,登录,修改信息和切换应用页面,createAppAccountManager方法创建应用帐号管理器对象
      • 源码链接:[AccountData.ts],[AccountModel.ts]
      • 接口参考:[@ohos.account.appAccount],[@ohos.data.preferences],[@ohos.router]

鸿蒙开发岗位需要掌握那些核心要领?

目前还有很多小伙伴不知道要学习哪些鸿蒙技术?不知道重点掌握哪些?为了避免学习时频繁踩坑,最终浪费大量时间的。

自己学习时必须要有一份实用的鸿蒙(Harmony NEXT)资料非常有必要。 这里我推荐,根据鸿蒙开发官网梳理与华为内部人员的分享总结出的开发文档。内容包含了:【ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技术知识点。

废话就不多说了,接下来好好看下这份资料。

如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。鸿蒙OpenHarmony知识←前往。下面是鸿蒙开发的学习路线图。

针对鸿蒙成长路线打造的鸿蒙学习文档。鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,帮助大家在技术的道路上更进一步。

其中内容包含:

《鸿蒙开发基础》鸿蒙OpenHarmony知识←前往

  1. ArkTS语言
  2. 安装DevEco Studio
  3. 运用你的第一个ArkTS应用
  4. ArkUI声明式UI开发
  5. .……

《鸿蒙开发进阶》鸿蒙OpenHarmony知识←前往

  1. Stage模型入门
  2. 网络管理
  3. 数据管理
  4. 电话服务
  5. 分布式应用开发
  6. 通知与窗口管理
  7. 多媒体技术
  8. 安全技能
  9. 任务管理
  10. WebGL
  11. 国际化开发
  12. 应用测试
  13. DFX面向未来设计
  14. 鸿蒙系统移植和裁剪定制
  15. ……

《鸿蒙开发实战》鸿蒙OpenHarmony知识←前往

  1. ArkTS实践
  2. UIAbility应用
  3. 网络案例
  4. ……

最后

鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行!

这篇关于HarmonyOS开发实例:【app帐号管理】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/902068

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java