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

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

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

相关推荐
二二孚日18 分钟前
自用华为ICT云赛道Big Data第四章知识点-Flink流批一体分布式实时处理引擎
大数据·华为
zhanshuo32 分钟前
开发者必看!如何在HarmonyOS中快速调用摄像头功能
harmonyos
HMSCore37 分钟前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
zhanshuo38 分钟前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
HarmonyOS小助手1 小时前
闯入鸿蒙:浪漫、理想与「草台班子」
harmonyos·鸿蒙·harmonyos next·鸿蒙生态
xq95271 小时前
flutter 鸿蒙化插件开发横空出世
harmonyos
HarmonyOS_SDK2 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
别说我什么都不会2 小时前
【OpenHarmony】鸿蒙开发之Jmustache
harmonyos
塞尔维亚大汉2 小时前
鸿蒙内核源码分析(文件系统篇) | 用图书管理说文件系统
源码·harmonyos
塞尔维亚大汉6 小时前
鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗
源码·harmonyos