HarmonyOS NEXT 实战之元服务:静态案例效果--- 手机一键加速、手机垃圾清理

背景:

前几篇学习了元服务,后面几期就让我们开发简单的元服务吧,里面丰富的内容大家自己加,本期案例 仅供参考

先上本期效果图 ,里面图片自行替换


效果图1完整代码案例如下:

复制代码
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    Column() {

      Text($r('app.string.EntryAbility_label')).fontWeight(FontWeight.Bold).fontSize(29).fontColor("#222222").margin({ top: 20, bottom: 20 })

      Row() {
        Stack() {
          Progress({
            value: 6800,
            total: 10000,
            type: ProgressType.Ring
          })
            .width(84)
            .color("#1B80FA")
            .style({ strokeWidth: 10 })
            .backgroundColor('#EDF3FD')

          Text('68%').fontSize(14).fontColor("#222222").fontWeight(FontWeight.Bold)

        }.margin({ left: 16 })

        Column({ space: 6 }) {
          Text('请放心使用').fontColor('#383838').fontSize(25)
          Text('本周自动清理 1.13 GB').fontColor('#9E9E9E').fontSize(15)
          Text('已使用 52.91 GB/256 GB >').fontColor('#9E9E9E').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 })

      }.width('100%').height(150).backgroundColor(Color.White)
      .borderRadius(10)

      Text('推荐压缩').fontSize(24).fontColor("#222222").margin({ top: 20, bottom: 20 })
      Row() {
        Text().backgroundColor('#EB6F20').width(40).height(40).borderRadius(20)

        Column({ space: 6 }) {
          Text('113 组文件可压缩').fontColor('#383838').fontSize(20)
          Text('491MB').fontColor('#A5A5A5').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)

        Button("无损压缩").height(40).fontSize(16).margin({ left: 4 })
      }
      .width('100%')
      .height(100)
      .backgroundColor(Color.White)
      .borderRadius(10)
      .padding(16)


      Row() {
        Text().backgroundColor('#F8A52B').width(40).height(40).borderRadius(20)

        Column({ space: 6 }) {
          Text('垃圾文件').fontColor('#383838').fontSize(20)
          Text('2.36 GB').fontColor('#A5A5A5').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)

        Button("立即清理").height(40).fontSize(16).margin({ left: 4 }).backgroundColor('#F8A52B')
      }
      .width('100%')
      .height(80)
      .backgroundColor(Color.White)
      .borderRadius(10)
      .padding(16)
      .margin({ top: 16 })

    }
    .height('100%')
    .width('100%')
    .padding(16)
    .backgroundColor('#F2F3F5')
    .margin({ top: 60 })
    .alignItems(HorizontalAlign.Start)
  }

  aboutToAppear() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.loginWithHuaweiID();
  }

  /**
   * Sample code for using HUAWEI ID to log in to atomic service.
   * According to the Atomic Service Review Guide, when a atomic service has an account system,
   * the option to log in with a HUAWEI ID must be provided.
   * The following presets the atomic service to use the HUAWEI ID silent login function.
   * To enable the atomic service to log in successfully using the HUAWEI ID, please refer
   * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.
   */
  private loginWithHuaweiID() {
    // Create a login request and set parameters
    let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
    loginRequest.forceLogin = false;
    // Execute login request
    let controller = new authentication.AuthenticationController();
    controller.executeRequest(loginRequest).then((data) => {
      let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
      let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;
      // Send authCode to the backend in exchange for unionID, session

    }).catch((error: BusinessError) => {
      hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));
      if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {
        // HUAWEI ID is not logged in, it is recommended to jump to the login guide page

      }
    });
  }
}

效果图2完整代码案例如下:

复制代码
import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@ComponentV2
struct Index {
  @Local c1: number = 535;
  @Local c2: number = 348;

  build() {
    Column({ space: 8 }) {
      Text($r('app.string.EntryAbility_label'))
        .fontWeight(FontWeight.Bold)
        .fontSize(29)
        .fontColor("#222222")
        .margin({ top: 20, bottom: 20 })
      Row() {
        Checkbox()
          .select(true).onChange(value => {
          console.log('xxxxxxxxxxxx' + value)
        })

        Column({ space: 6 }) {
          Text('内存垃圾').fontColor('#383838').fontSize(20)
          Text('已选2项').fontColor('#6A6A6A').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)

        Text('2 项 >').fontColor('#6A6A6A').fontSize(20)
      }
      .width('100%')
      .height(100)
      .backgroundColor(Color.White)
      .borderRadius(10)
      .padding(16)

      // 缓存垃圾
      // 已选 535 KB
      // 系统垃圾
      //
      // 2 项
      // 535 KB2
      // 348 KB
      Row() {
        Checkbox()
          .select(true).onChange(value => {
          this.c1 = value ? 535 : 0
        })

        Column({ space: 6 }) {
          Text('缓存垃圾').fontColor('#383838').fontSize(20)
          Text('已选 535 KB').fontColor('#6A6A6A').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)

        Text('535 KB').fontColor('#6A6A6A').fontSize(20)
      }
      .width('100%')
      .height(100)
      .backgroundColor(Color.White)
      .borderRadius(10)
      .padding(16)

      Row() {
        Checkbox()
          .select(true).onChange(value => {
          this.c2 = value ? 348 : 0
        })

        Column({ space: 6 }) {
          Text('系统垃圾').fontColor('#383838').fontSize(20)
          Text('已选 348 KB').fontColor('#6A6A6A').fontSize(15)
        }.alignItems(HorizontalAlign.Start).margin({ left: 8 }).layoutWeight(1)

        Text('348 KB >').fontColor('#6A6A6A').fontSize(20)
      }
      .width('100%')
      .height(100)
      .backgroundColor(Color.White)
      .borderRadius(10)
      .padding(16)


      Button(`删除(已选 ${this.c1 + this.c2} KB)`)
        .width('70%')
        .margin({ top: 40, left: 50 })
        .backgroundColor('#E7E8EA')
        .fontColor('#E65A5B')

    }
    .height('100%')
    .width('100%')
    .alignItems(HorizontalAlign.Start)
    .padding(16)
    .backgroundColor('#F2F3F5')
    .margin({ top: 50 })
  }

  aboutToAppear() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    this.loginWithHuaweiID();
  }

  /**
   * Sample code for using HUAWEI ID to log in to atomic service.
   * According to the Atomic Service Review Guide, when a atomic service has an account system,
   * the option to log in with a HUAWEI ID must be provided.
   * The following presets the atomic service to use the HUAWEI ID silent login function.
   * To enable the atomic service to log in successfully using the HUAWEI ID, please refer
   * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.
   */
  private loginWithHuaweiID() {
    // Create a login request and set parameters
    let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
    // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
    loginRequest.forceLogin = false;
    // Execute login request
    let controller = new authentication.AuthenticationController();
    controller.executeRequest(loginRequest).then((data) => {
      let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
      let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;
      // Send authCode to the backend in exchange for unionID, session

    }).catch((error: BusinessError) => {
      hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));
      if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {
        // HUAWEI ID is not logged in, it is recommended to jump to the login guide page

      }
    });
  }
}

最近文章>>>>>>>>>>>

HarmonyOS NEXT实战:元服务与应用 APP 发布应用市场的详细步骤与流程

若本文对您稍有帮助,诚望您不吝点赞,多谢。

有兴趣的同学可以点击查看源码

相关推荐
Random_index1 小时前
#HarmonyOS篇:学习UI规范基本语法&&学习UI范式装填管理V1&&学习UI范式装填管理V2&&学习UI范式渲染控制
harmonyos
前端世界1 小时前
鸿蒙分布式权限管理实战指南:架构原理 + 可运行 Demo
分布式·架构·harmonyos
LRX_1989272 小时前
华为设备配置练习(八)策略路由配置
服务器·网络·华为
SmartBrain2 小时前
解读:《华为变革法:打造可持续进步的组织》
大数据·人工智能·华为·语言模型
挨踢攻城2 小时前
华为认证高级工程师HCIP考证难度大吗?如何获得?不同方向考点对比?
华为·hcie·hcia·hcip·hcip考试
前端世界2 小时前
鸿蒙分布式通信安全实践:中间人攻击原理与防御实现
分布式·华为·harmonyos
航Hang*2 小时前
第六章:网络系统建设与运维(中级)——链路聚合
运维·服务器·网络·笔记·华为·ensp
cz追天之路11 小时前
华为机考--- 字符串最后一个单词的长度
javascript·css·华为·less
11 小时前
鸿蒙——通知
华为·harmonyos·
周胡杰14 小时前
鸿蒙preferences单多例使用,本地存储类
缓存·华为·harmonyos·preferences·鸿蒙本地存储