鸿蒙HarmonyOS项目实战开发:分布式账本

打印 上一主题 下一主题

主题 1058|帖子 1058|积分 3174

简介

Demo基于Open Harmony系统使用ETS语言举行编写,本Demo紧张通过装备认证、分布式拉起、分布式数据管理等功能来实现。
应用结果


  • 装备认证,获取同一个局域网内的装备ID,并拉起应用
 


  • 添加数据并在另一台装备显示该数据
 

开发步调

1.新建Openharmony ETS工程

在DevEco Studio中点击File -> New Project ->[Standard]Empty Ability->Next,Language 选择ETS语言,末了点击Finish即创建乐成。
 

2.界面代码编写

1) 首页界面

 

  1. build() {
  2.     Flex({ direction: FlexDirection.Column}) {
  3.       //发现设备
  4.       Button('发现设备', { type: ButtonType.Normal, stateEffect: true })
  5.         .borderRadius(8)
  6.         .backgroundColor(0x317aff).width(90)
  7.         .onClick(() =>{
  8.           this.fun()
  9.         })
  10.       //设备认证
  11.       Button('authDevice', { type: ButtonType.Normal, stateEffect: true })
  12.         .borderRadius(8)
  13.         .backgroundColor(0x317aff).width(90)
  14.         .onClick(() =>{
  15.           this.authDevice()
  16.         })
  17.     // 拉起应用
  18.       Button('拉起应用', { type: ButtonType.Normal, stateEffect: true })
  19.         .borderRadius(8)
  20.         .backgroundColor(0x317aff).width(90)
  21.         .onClick(() =>{
  22.                     this.startAb()
  23.         })
  24.       Stack({
  25.         alignContent:Alignment.TopEnd
  26.       }){
  27.         Text('家庭账本')
  28.           .fontSize(20)
  29.           .fontWeight(FontWeight.Bold)
  30.           .width('100%')
  31.           .margin({left:12})
  32.            .onClick(() =>{
  33.              //          routePage()
  34.              this.fun()
  35.            })
  36.         Image("/picture/add.png")
  37.           .width(40)
  38.           .height(40)
  39.           .align(Alignment.Start)
  40.           .margin({
  41.             right:12
  42.           }).onClick(() =>{
  43.           routePage()
  44.         })
  45.       }
  46.       .width(350)
  47.       .height(60)
  48.       .margin({
  49.         top:10,
  50.         bottom:10
  51.       })
  52.       Flex({
  53.         direction: FlexDirection.Column, alignItems:ItemAlign.Start,
  54.       }){
  55.         Text("2022年12月")
  56.           .fontSize(20)
  57.           .fontColor(Color.White)
  58.         Text("结余")
  59.           .fontSize(20)
  60.           .fontColor(Color.White)
  61.           .margin({
  62.             top:30
  63.           }).align(Alignment.Start)
  64.         Text("总支出0|总收入0")
  65.           .fontSize(16)
  66.           .fontColor(Color.White)
  67.           .margin({
  68.             top:10
  69.           }).align(Alignment.Start)
  70.       }
  71.       .backgroundColor("#665A5A")
  72.       .height(230)
  73.       .layoutWeight(1)
  74.       .padding(10)
  75.       .onClick(() =>{
  76.         routePage()
  77.       })
  78.       Tabs() {
  79.         TabContent() {
  80.           ProjectList()
  81.         }
  82.         .tabBar("项目")
  83.         TabContent() {
  84.           Statistics()
  85.         }
  86.         .tabBar("统计")
  87.       }
  88.     }
  89.     .width('100%')
  90.     .height('100%')
  91.     .padding({
  92.       left:12,right:12
  93.     })
  94.   }
复制代码
底部TabContent 项目模块
  1. @Component
  2. struct ProjectList {
  3.   remoteDataManager = new RemoteDataManager()
  4.   @State ProjectData: Array<any> = []
  5.   TestData:any[] =  []
  6.   TestKvData: Array<any> = []
  7.   kvManager = null
  8.   kvStore = null
  9.   STORE_ID = 'store_accountbook'
  10.   aboutToAppear(){
  11.     try {
  12.       const config = {
  13.         userInfo: {
  14.           userId: '0',
  15.           userType: 0
  16.         },
  17.         bundleName: 'com.example.accountbookets'
  18.       }
  19.       factory.createKVManager(config).then((manager) => {
  20.         this.kvManager = manager
  21.         let options =
  22.           {
  23.             createIfMissing: true,
  24.             encrypt: false,
  25.             backup: false,
  26.             autoSync: true,
  27.             kvStoreType: 1,
  28.             securityLevel: 3
  29.           }
  30.         this.kvManager.getKVStore(this.STORE_ID, options).then((store) => {
  31.           this.kvStore = store
  32.           this.kvStore.get("key2").then((data) => {
  33.             this.ProjectData = JSON.parse(data)
  34.           })
  35.         }).catch((err) => {
  36.         })
  37.       }).catch((err) => {
  38.       })
  39.     } catch (e) {
  40.     }
  41.   }
  42.   @Builder ProjectItem(image, name, des,time,money) {
  43.     Flex({ direction: FlexDirection.Row,alignItems: ItemAlign.Center }){
  44.       Image($r("app.media.icon1"))
  45.         .width(30)
  46.         .height(30)
  47.       Column() {
  48.         Text(name)
  49.           .fontSize(16)
  50.           .fontColor(Color.Black)
  51.         Text('11:20')
  52.           .fontSize(16)
  53.           .fontColor(Color.Gray)
  54.       }
  55.       .alignItems(HorizontalAlign.Start)
  56.       .margin({left:15})
  57.       Text('HUAWEI')
  58.         .fontSize(12)
  59.         .fontColor(Color.Black)
  60.         .margin({left:20})
  61.       Text(des)
  62.         .fontSize(14)
  63.         .fontColor(Color.Gray)
  64.         .margin({left:15})
  65.       Column() {
  66.         Text('-100')
  67.           .fontSize(16)
  68.           .fontColor(Color.Black)
  69.         Text(time)
  70.           .fontSize(16)
  71.           .fontColor(Color.Gray)
  72.       }
  73.       .alignItems(HorizontalAlign.Start)
  74.       .margin({left:20})
  75.     }
  76.     .width(400)
  77.     .height(50)
  78.     .margin({top:10})
  79.   }
  80.   build() {
  81.     List() {
  82.       ForEach(this.ProjectData, (item) => {
  83.         ListItem() {
  84.           this.ProjectItem(item.image, item.name, item.des,item.time,item.money)
  85.         }
  86.         .onClick(() => {
  87.         })
  88.       }, (item) => JSON.stringify(item)) {
  89.       }
  90.     }
  91.   }
  92. }
复制代码
底部TabContent 统计模块
  1. @Component
  2. struct Statistics{
  3.   build(){
  4.     Flex({ direction: FlexDirection.Row}){
  5.       Tabs() {
  6.         TabContent() {
  7.           PayList()
  8.         }
  9.         .tabBar("支出分类")
  10.         TabContent() {
  11.         }
  12.         .tabBar("成员分类")
  13.       }
  14.     }
  15.   }
  16. }
复制代码
统计模块里面的TabContent
  1. @Component
  2. struct PayList {
  3.   private PayData: PayBean[] = initializeOnStartup()
  4.   @Builder PayItem(previewUrl, title, describe) {
  5.     Flex({ direction: FlexDirection.Row,alignItems: ItemAlign.Center }){
  6.       Image(previewUrl)
  7.         .width(30)
  8.         .height(30)
  9.       Text(title)
  10.         .fontSize(16)
  11.         .fontColor(Color.Black)
  12.         .margin({left:8})
  13.       Text('100%')
  14.         .fontSize(12)
  15.         .fontColor(Color.Black)
  16.         .margin({left:8})
  17.       Progress({ value: 20, total: 150, style: ProgressStyle.Linear }).color(Color.Red).value(150).width(200)
  18.       Text('-100')
  19.         .fontSize(14)
  20.         .fontColor(Color.Gray)
  21.         .margin({left:8})
  22.     }
  23.     .width(400)
  24.     .height(50)
  25.     .margin({top:10})
  26.   }
  27.   build() {
  28.     List() {
  29.       ForEach(this.PayData, (item) => {
  30.         ListItem() {
  31.           this.PayItem(item.image, item.name, item.des)
  32.         }
  33.         .onClick(() => {
  34.           console.info("点击我")
  35.           router.push({
  36.             uri: "pages/VideoPlayer",
  37.           })
  38.         })
  39.       }, (item) => JSON.stringify(item)) {
  40.       }
  41.     }
  42.   }
  43. }
复制代码
2) add.ets页面


  1. build() {
  2.     Flex({ direction: FlexDirection.Column,alignItems: ItemAlign.Center}) {
  3.       Flex({ direction: FlexDirection.Row,alignItems: ItemAlign.Center})
  4.       {
  5.         Image("/picture/icon_back.png")
  6.           .width(35)
  7.           .height(35)
  8.          .onClick(() =>{
  9.            router.push({
  10.              uri: "pages/index",
  11.            })
  12.          })
  13.         Text("加一笔")
  14.           .fontSize(20)
  15.           .fontWeight(FontWeight.Bold)
  16.         .margin({left:20})
  17.       }.margin({top:10})
  18.       .padding({left:20})
  19.       .height(100)
  20.       .width(500)
  21.       Stack({
  22.         alignContent: Alignment.TopStart
  23.       }){
  24.         Tabs() {
  25.           TabContent() {
  26.             pay({payTime:$strTime,payRemark:$strRemark,payType:$strType})
  27.           }
  28.           .tabBar("支出")
  29.           TabContent() {
  30.             Income()
  31.           }
  32.           .tabBar("收入")
  33.         }
  34.         .height(450)
  35.       }.width(500)
  36.       .height(500)
  37.       Flex({
  38.         direction: FlexDirection.Row,alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center
  39.       }){
  40.         Text("输入金额")
  41.           .fontColor(Color.Black)
  42.           .fontSize(20)
  43.           .margin({
  44.             right:15
  45.           })
  46.           .width(100)
  47.         TextInput({ placeholder: '100', text:this.strMoney })
  48.           .type(InputType.Normal)
  49.           .placeholderColor(Color.Gray)
  50.           .placeholderFont({ size: 20, weight: 2})
  51.           .enterKeyType(EnterKeyType.Search)
  52.           .caretColor(Color.Green)
  53.           .width(250)
  54.           .height(40)
  55.           .borderRadius('20px')
  56.       }
  57.       .width(400)
  58.       .height(50)
  59.       Text('保存')
  60.         .fontColor(Color.White)
  61.         .fontSize(20)
  62.         .margin({
  63.           top:20
  64.         })
  65.         .textAlign(TextAlign.Center)
  66.       .width(380)
  67.         .height(80)
  68.       .backgroundColor("#FE4F16")
  69.         .onClick(() =>{
  70.           TestData.push({image:"/picture/icon1.png",title:'canyin',des:'ceshi',time:'2021',money:'50'})
  71.           if (AppStorage.Get("key1") == null) {
  72.             AppStorage.SetAndLink("key1", TestData)
  73.             this.remoteDataManager.dataChange("key2", JSON.stringify(TestData))
  74.           }else{
  75.             this.TestStorageData = AppStorage.Get("key1")
  76.             //
  77. //            this.TestStorageData.push({image:"/picture/icon1.png",title:'canyin',des:'beizhu',time:'2021',money:'50'})
  78.             //具体代码
  79.             this.TestStorageData.push({image:"/picture/icon1.png",title:this.strType,des:this.strRemark,time:this.strTime,money:this.strMoney})
  80.             AppStorage.SetAndLink("key1", this.TestStorageData)
  81.             let str = JSON.stringify(this.TestStorageData)
  82.             this.TestKvData = JSON.parse(str)
  83.             this.remoteDataManager.dataChange("key2", JSON.stringify(this.TestKvData))
  84.           }
  85.           router.push({
  86.             uri: "pages/index",
  87.           })
  88.         })
  89.     }
  90.     .width('100%')
  91.     .height('100%')
  92.   }
复制代码
add页面付出模块
  1. @Component
  2. struct pay{
  3.   @Link payTime:string
  4.   @Link payRemark:string
  5.   @Link payType:string
  6.   @State private index:number = 0
  7.   @State strType:string = "canyin"
  8.   @State AccountData: Array<any> = [
  9.     { previewUrl: "/picture/icon1.png", title: "canyin" ,number:0},
  10.     { previewUrl: "/picture/icon2_2.png", title: "gouwu" ,number:1},
  11.     { previewUrl: "/picture/icon3_3.png", title: "jiaotong" ,number:2},
  12.     { previewUrl: "/picture/icon4_4.png", title: "fuwu" ,number:3},
  13.     { previewUrl: "/picture/icon5_5.png", title: "jiaoyu" ,number:4},
  14.     { previewUrl: "/picture/icon6_6.png", title: "yundong" ,number:5},
  15.     { previewUrl: "/picture/icon7_7.png", title: "luxing" ,number:6},
  16.     { previewUrl: "/picture/icon8_8.png", title: "yiliao" ,number:7},
  17. //    { previewUrl: "/picture/icon9_9.png", title: "生活" ,number:9},
  18. //    { previewUrl: "/picture/icon10_10.png", title: "宠物" ,number:10},
  19.   ]
  20.   @Builder ProItem(previewUrl, title,number) {
  21.     Stack() {
  22.       Flex({
  23.         direction: FlexDirection.Column
  24.       }) {
  25.         if (this.index == number) {
  26.           if (number == 0) {
  27.             Image("/picture/icon1.png")
  28.               .width(60)
  29.               .height(60)
  30.           }else if (number == 1) {
  31.             Image("/picture/icon2.png")
  32.               .width(60)
  33.               .height(60)
  34.           }else if (number == 2) {
  35.             Image("/picture/icon3.png")
  36.               .width(60)
  37.               .height(60)
  38.           }else if (number == 3) {
  39.             Image("/picture/icon4.png")
  40.               .width(60)
  41.               .height(60)
  42.           }else if (number == 4) {
  43.             Image("/picture/icon5.png")
  44.               .width(60)
  45.               .height(60)
  46.           }else if (number == 5) {
  47.             Image("/picture/icon6.png")
  48.               .width(60)
  49.               .height(60)
  50.           }else if (number == 6) {
  51.             Image("/picture/icon7.png")
  52.               .width(60)
  53.               .height(60)
  54.           }else if (number == 7) {
  55.             Image("/picture/icon8.png")
  56.               .width(60)
  57.               .height(60)
  58.           }else if (number == 8) {
  59.             Image("/picture/icon9.png")
  60.               .width(60)
  61.               .height(60)
  62.           }else if (number == 9) {
  63.             Image("/picture/icon10.png")
  64.               .width(60)
  65.               .height(60)
  66.           }
  67.         }else{
  68.           if (number == 0) {
  69.             Image("/picture/icon1_1.png")
  70.               .width(60)
  71.               .height(60)
  72.           }else{
  73.             Image(previewUrl)
  74.               .width(60)
  75.               .height(60)
  76.           }
  77.         }
  78.         Column() {
  79.           Text(title)
  80.             .fontSize(16)
  81.             .fontColor(Color.Black)
  82.         }
  83.         .alignItems(HorizontalAlign.Center)
  84.       }
  85.     }
  86.     .height(100)
  87.     .width(100)
  88.     .margin({
  89.       bottom: 16
  90.     })
  91.   }
  92.   build(){
  93.     Flex({direction: FlexDirection.Column}){
  94.       Grid(){
  95.         ForEach(this.AccountData, (item) => {
  96.           GridItem() {
  97.             this.ProItem(item.previewUrl, item.title,item.number)
  98.           }
  99.           .onClick(() => {
  100.             console.info("点击我")
  101.             this.index = item.number
  102.             this.payType = this.AccountData[this.index].title
  103.           })
  104.         }, (item) => JSON.stringify(item)) {
  105.         }
  106.       }
  107.       .rowsTemplate('1fr 1fr')
  108.       .columnsTemplate('1fr 1fr 1fr 1fr')
  109.       .columnsGap(8)
  110.       .rowsGap(8)
  111.       .height(200)
  112. //      Time()
  113. //      Remark()
  114.       // ******************时间**********************
  115.       Flex({
  116.         direction: FlexDirection.Row,alignItems: ItemAlign.Center
  117.       }){
  118.         Text("时间")
  119.           .fontColor(Color.Black)
  120.           .fontSize(20)
  121.           .margin({
  122.             right:15
  123.           })
  124.           .width(70)
  125.         TextInput({ placeholder: '输入收支时间', text: this.payTime })
  126.           .type(InputType.Normal)
  127.           .placeholderColor(Color.Gray)
  128.           .placeholderFont({ size: 20, weight: 2})
  129.           .enterKeyType(EnterKeyType.Search)
  130.           .caretColor(Color.Green)
  131.           .width(300)
  132.           .height(40)
  133.           .borderRadius('20px')
  134.           .backgroundColor(Color.White)
  135.           .onChange((value: string) => {
  136.             this.payTime = value
  137.           })
  138.       }
  139.       .margin({
  140.         top:20,left:15
  141.       })
  142.       .width(200)
  143.       //*******************备注********************
  144.       Flex({
  145.         direction: FlexDirection.Row,alignItems: ItemAlign.Center
  146.       }){
  147.         Text("备注")
  148.           .fontColor(Color.Black)
  149.           .fontSize(20)
  150.           .margin({
  151.             right:15
  152.           })
  153.           .width(70)
  154.         TextInput({ placeholder: '输入说明', text: this.payRemark })
  155.           .type(InputType.Normal)
  156.           .placeholderColor(Color.Gray)
  157.           .placeholderFont({ size: 20, weight: 2})
  158.           .enterKeyType(EnterKeyType.Search)
  159.           .caretColor(Color.Green)
  160.         //        .layoutWeight(8)
  161.           .height(40)
  162.           .width(300)
  163.           .borderRadius('20px')
  164.           .backgroundColor(Color.White)
  165.           .onChange((value: string) => {
  166.             this.payRemark = value
  167.           })
  168.       }
  169.       .margin({
  170.         top:20,left:15
  171.       })
  172.       .width(50)
  173.       .height(50)
  174.     }
  175.     .height('100%')
  176.     .layoutWeight(1)
  177.   }
  178. }
复制代码
收入模块代码
  1. @Component
  2. struct Income{
  3.   build(){
  4.     Flex({direction: FlexDirection.Column}){
  5.       Time()
  6.       Remark()
  7.     }
  8.   }
  9. }
复制代码
时间模块
  1. @Component
  2. struct Time{
  3.   @State inputTime:string = ''
  4.   build(){
  5.     Flex({
  6.       direction: FlexDirection.Row,alignItems: ItemAlign.Center
  7.     }){
  8.       Text("时间")
  9.         .fontColor(Color.Black)
  10.         .fontSize(20)
  11.         .margin({
  12.           right:15
  13.         })
  14.       .width(70)
  15.       TextInput({ placeholder: '2021', text: this.inputTime })
  16.         .type(InputType.Normal)
  17.         .placeholderColor(Color.Gray)
  18.         .placeholderFont({ size: 20, weight: 2})
  19.         .enterKeyType(EnterKeyType.Search)
  20.         .caretColor(Color.Green)
  21.         .width(300)
  22.         .height(40)
  23.         .borderRadius('20px')
  24.         .backgroundColor(Color.White)
  25.     }
  26.     .margin({
  27.       top:20,left:15
  28.     })
  29.     .width(200)
  30.   }
  31. }
复制代码
备注模块
  1. @Component
  2. struct Remark{
  3.   @State inputRemark:string = ''
  4.   build(){
  5.     Flex({
  6.       direction: FlexDirection.Row,alignItems: ItemAlign.Center
  7.     }){
  8.       Text("备注")
  9.         .fontColor(Color.Black)
  10.         .fontSize(20)
  11.         .margin({
  12.           right:15
  13.         })
  14.       .width(70)
  15.       TextInput({ placeholder: 'ceshe', text: this.inputRemark })
  16.         .type(InputType.Normal)
  17.         .placeholderColor(Color.Gray)
  18.         .placeholderFont({ size: 20, weight: 2})
  19.         .enterKeyType(EnterKeyType.Search)
  20.         .caretColor(Color.Green)
  21. //        .layoutWeight(8)
  22.         .height(40)
  23.         .width(300)
  24.         .borderRadius('20px')
  25.         .backgroundColor(Color.White)
  26.     }
  27.     .margin({
  28.       top:20,left:15
  29.     })
  30.     .width(50)
  31.     .height(50)
  32.   }
  33. }
复制代码
3.装备认证

装备认证是依赖DeviceManager组件来实现的,详细代码参考源码RemoteDeviceModel.ets
1.创建DeviceManager实例
  1. registerDeviceListCallback(callback) {
  2.     if (typeof (this.#deviceManager) === 'undefined') {
  3.       deviceManager.createDeviceManager('com.example.tictactoegame', (error, value) => {
  4.         if (error) return
  5.         this.#deviceManager = value;
  6.         this.registerDeviceListCallback_(callback);
  7.       });
  8.     } else {
  9.       this.registerDeviceListCallback_(callback);
  10.     }
  11.   }
复制代码
2.查询可信装备列表
  1. var list = this.#deviceManager.getTrustedDeviceListSync();
  2.     if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
  3.       this.deviceList = list;
  4.     }
复制代码
3.注册装备上下线监听
  1. this.#deviceManager.on('deviceStateChange', (data) => {
  2.       switch (data.action) {
  3.         case 0:
  4.           this.deviceList[this.deviceList.length] = data.device;
  5.           this.callback();
  6.           if (this.authCallback != null) {
  7.             this.authCallback();
  8.             this.authCallback = null;
  9.           }
  10.           break;
  11.         case 2:
  12.           if (this.deviceList.length > 0) {
  13.             for (var i = 0; i < this.deviceList.length; i++) {
  14.               if (this.deviceList[i].deviceId === data.device.deviceId) {
  15.                 this.deviceList[i] = data.device;
  16.                 break;
  17.               }
  18.             }
  19.           }
  20.           this.callback();
  21.           break;
  22.         case 1:
  23.           if (this.deviceList.length > 0) {
  24.             var list = [];
  25.             for (var i = 0; i < this.deviceList.length; i++) {
  26.               if (this.deviceList[i].deviceId != data.device.deviceId) {
  27.                 list[i] = data.device;
  28.               }
  29.             }
  30.             this.deviceList = list;
  31.           }
  32.           this.callback();
  33.           break;
  34.         default:
  35.           break;
  36.       }
  37.     });
复制代码
4.装备发现
  1. this.#deviceManager.on('deviceFound', (data) => {
  2.       for (let i = 0; i < this.discoverList.length; i++) {
  3.         if (that.discoverList[i].deviceId === data.device.deviceId) {
  4.           return;
  5.         }
  6.       }
  7.       this.discoverList[this.discoverList.length] = data.device;
  8.       this.callback();
  9.     });
复制代码
5.装备认证
  1. authDevice(deviceInfo, callback){
  2.     let extraInfo = {
  3.       "targetPkgName": 'com.example.tictactoegame',
  4.       "appName": 'com.example.tictactoegame',
  5.       "appDescription": 'com.example.tictactoegame',
  6.       "business": '0'
  7.     };
  8.     let authParam = {
  9.       "authType": 1,
  10.       "appIcon": '',
  11.       "appThumbnail": '',
  12.       "extraInfo": extraInfo
  13.     };
  14.     this.#deviceManager.authenticateDevice(deviceInfo, authParam, (err, data) => {
  15.       if (err) {
  16.         this.authCallback = null;
  17.       } else {
  18.         this.authCallback = callback;
  19.       }
  20.     });
  21.   }
复制代码
4.数据管理

分布式数据管理依赖@ohos.data.distributedData模块实现,详细参考源码RemoteDataManager.ets
1.导入该模块
  1. import factory from '@ohos.data.distributedData';
复制代码
2.创建KVManager实例,用于管理数据库对象
  1.   registerDataListCallback(callback) {
  2.     let that = this
  3.     if (this.kvManager == null) {
  4.       try {
  5.         const config = {
  6.           userInfo: {
  7.             userId: '0',
  8.             userType: 0
  9.           },
  10.           bundleName: 'com.example.tictactoegame'
  11.         }
  12.         factory.createKVManager(config).then((manager) => {
  13.           that.kvManager = manager
  14.           that.registerDataListCallback_(callback)
  15.         }).catch((err) => {
  16.         })
  17.       } catch (e) {
  18.       }
  19.     } else {
  20.       this.registerDataListCallback_(callback)
  21.     }
  22.   }
复制代码
3.创建并获取KVStore数据库
  1. registerDataListCallback_(callback) {
  2.     let that = this
  3.     if (that.kvManager == null) {
  4.       callback()
  5.       return
  6.     }
  7.     if (that.kvStore == null) {
  8.       try {
  9.         let options =
  10.           {
  11.             createIfMissing: true,
  12.             encrypt: false,
  13.             backup: false,
  14.             autoSync: true,
  15.             kvStoreType: 1,
  16.             securityLevel: 3
  17.           }
  18.         this.kvManager.getKVStore(this.STORE_ID, options).then((store) => {
  19.           that.kvStore = store
  20.           that._registerDataListCallback_(callback)
  21.         }).catch((err) => {
  22.         })
  23.       } catch (e) {
  24.       }
  25.     } else {
  26.       this._registerDataListCallback_(callback)
  27.     }
  28.   }
复制代码
4.订阅指定类型的数据变更通知
  1.   _registerDataListCallback_(callback) {
  2.     let that = this
  3.     if (that.kvManager == null) {
  4.       callback()
  5.       return
  6.     }
  7.     this.kvStore.on('dataChange', 1, function(data) {
  8.       if (data) {
  9.          that.arr = data.updateEntries
  10.         callback()
  11.       }
  12.     })
  13.   }
复制代码
5.添加指定类型键值对到数据库
  1.   dataChange(key, value) {
  2.     let that = this
  3.       try {
  4.         that.kvStore.put(JSON.stringify(key), JSON.stringify(value)).then((data) => {
  5.         }).catch((err) => {
  6.           prompt.showToast({message:'put err:'+JSON.stringify(value)})
  7.         })
  8.       } catch (e) {
  9.       }
  10.   }
复制代码
5.长途拉起装备app

使用FeatureAbility模块的startAbility接口拉起长途装备app
  1.   startAbilityContinuation(deviceId) {
  2.     let wantValue = {
  3.       bundleName: 'com.example.tictactoegame',
  4.       abilityName: 'com.example.tictactoegame.MainAbility',
  5.       deviceId: deviceId,
  6.       parameters: {
  7.         uri: 'pages/Fight'
  8.       }
  9.     };
  10.     featureAbility.startAbility({ want: wantValue }).then(() => {
  11.       router.replace({ uri: 'pages/Fight' })
  12.     });
  13.   }
复制代码
6.添加数据

新建一个账单数据 添加到分布式数据
  1. this.remoteDataManager.dataChange("key2", JSON.stringify(this.TestKvData))
复制代码
在另一台装备监听并获取显示该条数据
  1. private onPageShow() {
  2.     this.remoteDataManager.registerDataListCallback(() => {
  3.       let arr = this.remoteDataManager.arr[0]
  4.       this.strTest = arr.value.value
  5.       this.ProjectData = JSON.parse(this.strTest)
  6.      
  7.   }
复制代码
末了,有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道必要重点把握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(Harmony NEXT)资料用来跟着学习是非常有须要的。 
为了可以或许资助各人快速把握鸿蒙(Harmony NEXT)应用开发技术知识。在此给各人分享一下我结合鸿蒙最新资料整理出来的鸿蒙南北向开发学习路线以及整理的最新版鸿蒙学习文档资料。
这份鸿蒙(Harmony NEXT)资料包含了鸿蒙开发必把握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模子、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点。
希望这一份鸿蒙学习资料可以或许给各人带来资助,有必要的小伙伴自行领取,限时开源,先到先得~无套路领取!!
如果你是一名有经验的资深Android移动开发、Java开发、前端开发、对鸿蒙感爱好以及转行人员,可以直接领取这份资料
 获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
鸿蒙(Harmony NEXT)最新学习路线




  •  HarmonOS基础技能



  • HarmonOS就业必备技能 

  •  HarmonOS多媒体技术



  • 鸿蒙NaPi组件进阶



  • HarmonOS高级技能



  • 初识HarmonOS内核 

  • 实战就业级装备开发

 有了路线图,怎么能没有学习资料呢,小编也准备了一份连合鸿蒙官方发布条记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门讲授视频,内容包含:ArkTS、ArkUI、Web开发、应用模子、资源分类…等知识点。
获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
《鸿蒙 (OpenHarmony)开发入门讲授视频》


《鸿蒙生态应用开发V2.0白皮书》


《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

 《鸿蒙开发基础》



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

 《鸿蒙开发进阶》



  • Stage模子入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来计划
  • 鸿蒙系统移植和裁剪定制
  • ……

《鸿蒙进阶实战》



  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料
总结

总的来说,华为鸿蒙不再兼容安卓,对中年步伐员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个厘革的期间中立于不败之地。 



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

西河刘卡车医

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表