IT评测·应用市场-qidao123.com

标题: HarmonyOS NEXT 实战之元服务:静态案例结果---手机查看电量 [打印本页]

作者: 南飓风    时间: 2024-12-28 07:18
标题: HarmonyOS NEXT 实战之元服务:静态案例结果---手机查看电量
配景:

前几篇学习了元服务,反面几期就让我们开发简单的元服务吧,内里丰富的内容大家自己加,本期案例 仅供参考
先上本期结果图 ,内里图片自行更换


结果图1完整代码案比方下:

  1. import { authentication } from '@kit.AccountKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. @Entry
  5. @Component
  6. struct Index {
  7.   @State message: string = 'Hello World';
  8.   build() {
  9.     Column() {
  10.       Row() {
  11.         Image($r('app.media.dian')).width(55)
  12.         Text($r('app.string.EntryAbility_label'))
  13.           .fontWeight(FontWeight.Bold)
  14.           .layoutWeight(1)
  15.           .fontSize(22)
  16.           .fontColor("#222222")
  17.           .margin({ top: 20, bottom: 20 })
  18.         Button('发消息').fontSize(12).margin({ right: 12 })
  19.       }.width('100%').backgroundColor(Color.White).borderRadius(10)
  20.       Text('68%').fontSize(22).fontWeight(FontWeight.Bold)
  21.         .fontColor("#222222")
  22.         .margin({ top: 8 })
  23.       Text('预计继续使用 54 小时 24 分钟').fontSize(12)
  24.         .fontColor("#5F6062")
  25.         .margin({ top: 8 })
  26.       Text('服务卡')
  27.         .fontSize(20)
  28.         .fontColor("#222222")
  29.         .margin({ top: 20, bottom: 20 })
  30.       Column() {
  31.         Row() {
  32.           Image($r('app.media.dian')).width(35)
  33.           Text('设备电量')
  34.             .fontSize(18)
  35.             .fontColor("#222222")
  36.             .margin({ left: 10 })
  37.         }.width('100%').margin({ left: 8, top: 20 })
  38.         Row() {
  39.           Stack() {
  40.             Progress({
  41.               value: 8800,
  42.               total: 10000,
  43.               type: ProgressType.Ring
  44.             })
  45.               .width(64)
  46.               .color("#4EBB53")
  47.               .style({ strokeWidth: 6 })
  48.               .backgroundColor('#EDF3FD')
  49.             Image($r('app.media.zuoer')).width(20)
  50.             Text('88%').fontColor(Color.Black).fontSize(18).margin({ top: 100 })
  51.           }.margin({ left: 16 })
  52.           Stack() {
  53.             Progress({
  54.               value: 4000,
  55.               total: 10000,
  56.               type: ProgressType.Ring
  57.             })
  58.               .width(64)
  59.               .color("#4EBB53")
  60.               .style({ strokeWidth: 6 })
  61.               .backgroundColor('#EDF3FD')
  62.             Image($r('app.media.youer')).width(20)
  63.             Text('40%').fontColor(Color.Black).fontSize(18).margin({ top: 100 })
  64.           }.margin({ left: 16 })
  65.           Stack() {
  66.             Progress({
  67.               value: 6700,
  68.               total: 10000,
  69.               type: ProgressType.Ring
  70.             })
  71.               .width(64)
  72.               .color("#4EBB53")
  73.               .style({ strokeWidth: 6 })
  74.               .backgroundColor('#EDF3FD')
  75.             Image($r('app.media.shojji')).width(25)
  76.             Text('67%').fontColor(Color.Black).fontSize(18).margin({ top: 100 })
  77.           }.margin({ left: 16 })
  78.           Stack() {
  79.             Progress({
  80.               value: 6800,
  81.               total: 10000,
  82.               type: ProgressType.Ring
  83.             })
  84.               .width(64)
  85.               .color("#F1F1F1")
  86.               .style({ strokeWidth: 6 })
  87.               .backgroundColor('#F1F1F1')
  88.             Text('添加设备').fontColor(Color.Black).fontSize(14).margin({ top: 105 })
  89.           }.margin({ left: 16 })
  90.         }.width('100%')
  91.       }.width('100%').height(200).backgroundColor(Color.White)
  92.       .borderRadius(10)
  93.       Row() {
  94.         Text().backgroundColor('#4EBB53').width(40).height(40).borderRadius(20)
  95.         Text('显示电量百分比').fontColor(Color.Black).fontSize(22).margin({ left: 5 }).layoutWeight(1)
  96.         Toggle({ type: ToggleType.Switch, isOn: true })
  97.       }
  98.       .width('100%')
  99.       .height(80)
  100.       .margin({ top: 20 })
  101.       .backgroundColor(Color.White)
  102.       .borderRadius(10)
  103.       .padding(16)
  104.       Row() {
  105.         Text().backgroundColor(Color.Blue).width(40).height(40).borderRadius(20)
  106.         Text('更多电池设置').fontColor(Color.Black).fontSize(22).margin({ left: 5 }).layoutWeight(1)
  107.         Text('>').fontColor(Color.Black).fontSize(22)
  108.       }
  109.       .width('100%')
  110.       .height(80)
  111.       .margin({ top: 20 })
  112.       .backgroundColor(Color.White)
  113.       .borderRadius(10)
  114.       .padding(16)
  115.     }
  116.     .height('100%')
  117.     .width('100%')
  118.     .padding(16)
  119.     .backgroundColor('#F2F3F5')
  120.     .margin({ top: 60 })
  121.     .alignItems(HorizontalAlign.Start)
  122.   }
  123.   aboutToAppear() {
  124.     hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  125.     this.loginWithHuaweiID();
  126.   }
  127.   /**
  128.    * Sample code for using HUAWEI ID to log in to atomic service.
  129.    * According to the Atomic Service Review Guide, when a atomic service has an account system,
  130.    * the option to log in with a HUAWEI ID must be provided.
  131.    * The following presets the atomic service to use the HUAWEI ID silent login function.
  132.    * To enable the atomic service to log in successfully using the HUAWEI ID, please refer
  133.    * to the HarmonyOS HUAWEI ID Access Guide to configure the client ID and fingerprint certificate.
  134.    */
  135.   private loginWithHuaweiID() {
  136.     // Create a login request and set parameters
  137.     let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
  138.     // Whether to forcibly launch the HUAWEI ID login page when the user is not logged in with the HUAWEI ID
  139.     loginRequest.forceLogin = false;
  140.     // Execute login request
  141.     let controller = new authentication.AuthenticationController();
  142.     controller.executeRequest(loginRequest).then((data) => {
  143.       let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
  144.       let authCode = loginWithHuaweiIDResponse.data?.authorizationCode;
  145.       // Send authCode to the backend in exchange for unionID, session
  146.     }).catch((error: BusinessError) => {
  147.       hilog.error(0x0000, 'testTag', 'error: %{public}s', JSON.stringify(error));
  148.       if (error.code == authentication.AuthenticationErrorCode.ACCOUNT_NOT_LOGGED_IN) {
  149.         // HUAWEI ID is not logged in, it is recommended to jump to the login guide page
  150.       }
  151.     });
  152.   }
  153. }
复制代码
迩来文章>>>>>>>>>>>

HarmonyOS NEXT实战:元服务与应用 APP 发布应用市场的详细步调与流程
若本文对您稍有帮助,诚望您不吝点赞,多谢。
有兴趣的同砚可以点击查看源码



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




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4