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 发布应用市场的详细步骤与流程

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

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

相关推荐
Robot2512 小时前
「华为」人形机器人赛道投资首秀!
大数据·人工智能·科技·microsoft·华为·机器人
鸿蒙布道师2 小时前
鸿蒙NEXT开发动画案例5
android·ios·华为·harmonyos·鸿蒙系统·arkui·huawei
小诸葛的博客9 小时前
华为ensp实现跨vlan通信
网络·华为·智能路由器
康康这名还挺多11 小时前
鸿蒙HarmonyOS list优化一: list 结合 lazyforeach用法
数据结构·list·harmonyos·lazyforeach
晚秋大魔王15 小时前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——nettle库
linux·开源·harmonyos
python算法(魔法师版)18 小时前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
bestadc20 小时前
鸿蒙 UIAbility组件与UI的数据同步和窗口关闭
harmonyos
枫叶丹421 小时前
【HarmonyOS Next之旅】DevEco Studio使用指南(二十二)
华为·harmonyos·deveco studio·harmonyos next
ax一号街阿楠1 天前
华为FAT AP配置 真机
网络·华为·智能路由器
吗喽对你问好1 天前
华为5.7机考第一题充电桩问题Java代码实现
java·华为·排序