【每日学点HarmonyOS Next知识】导入cardEmulation、自定义装饰器、CallState状态码顺序、kv配置、签名文件配置

145 阅读3分钟

1、HarmonyOS 无法导入cardEmulation?

在工程entry mudule里的index.ets文件里导入cardEmulation失败 可以按照下面方式添加SystemCapability;在src/main/syscap.json(此文件需要手动创建)中添加如下内容

{
  "devices": {
  "general": [
  //这里要和module.json5中设备类型保持一致
  "phone"
  ]
},
  "development": {
  "addedSysCaps": [
  "SystemCapability.Communication.NFC.CardEmulation"
  ]
}
}

2、自定义装饰器ClassDecorator如何使用?

ArkTS支持TS5.0之前的TS装饰器语法。关于装饰器的定义和运行时行为,可以参考TS官方文档:www.typescriptlang.org/docs/handbo…。注意,如果在ets文件中定义装饰器,则需要同时满足ArkTS的语法规则,比如不能使用any等。

以下是类装饰器、属性装饰器、方法装饰器、参数装饰器的简单示例:

function TestClassDecorator (target: Function) {}
function TestMemberDecorator (target: testClass, memberName: String) {}
function TestFunDecorator (target: testClass, propertyName: String, descriptor: PropertyDescriptor) {}
function TestArgDecorator (target: Function, methodName: String, paramIndex: Number) {}

@TestClassDecorator
class testClass {
  @TestMemberDecorator
  count: number = 123;

  @TestFunDecorator
  TestFun(@TestArgDecorator param: string) {}
}

3、HarmonyOS 用户拨打电话时的CallState状态码变化顺序?

用户拨打电话时(电话拨出),对方拒接、接听、未接听三种情况CallState状态码变化顺序: 主动呼叫对方拒接/呼叫方响铃过程中提前挂断:2 -> 0 对方接听:2 -> 2(响铃过程中状态码是2)

4、HarmonyOS 文件key-value形式的自定义配置?

需求为在app目录下存放各个模块都能读取的,key-value形式的自定义配置,并且value的类型不固定,string、number、bool都有,相关的json文件应该怎么创建和读取,最好能直接用key读取出value,而不需要先读取整个文件

  1. 在resources/base/element/下新建json文件 boolean.json 示例内容如下:
{
  "boolean": [
  {
    "name": "module_desc",
  "value": true
  },
  {
    "name": "EntryAbility_desc",
  "value": false
  },
  {
    "name": "EntryAbility_label",
  "value": false
  }
  ]
}

2. 使用资源管理接口调用:getBoolean

import { common } from '@kit.AbilityKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private context = getContext(this) as common.UIAbilityContext;

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(()=>{
          let aa:boolean = this.context.resourceManager.getBoolean($r('app.boolean.EntryAbility_desc').id)
          console.log('lujun EntryAbility_desc value is '+aa)
        })
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

4、HarmonyOS 签名时报文件不支持的问题?

在进行HarmonyOS app打包时,发现报签名文件不支持的问题;但签名文件我是通过命令直接生成的;这个问题怎么解决? 问题场景描述: 使用签名命令生成签名文件:

keytool -genkeypair -alias "zofund" -keyalg EC -sigalg SHA256withECDSA -dname "C=CN,O=zofund,OU=zofund,CN=zofund"  -keystore /Users/lidaofu/DevEcoStudioProjects\debug.p12 -storetype pkcs12 -validity 9125 -storepass *** -keypass **** 

签名中profile应该时p7b文件。 具体的签名流程如下(手动):

  1. 生成CSR文件:编辑器:----->Build---->Generate Key and CSR;
  2. 已有p12,选择已有,否则选择新建,
  3. 填好Alias,Password等信息,并牢记
  4. 点击Finish完成---->csr文件创建完成

申请调试签名证书

  1. 登录AppGallery Connect,选择用户与访问
  2. 左侧证书管理------>新增证书,选择上面生成的csr,可以下载保存,供后续使用,
  3. 左侧设备管理------>添加相应的设备

申请调试profile

  1. 登录AppGallery Connect,选择我的项目
  2. 找到项目,在项目中找到管理HAP Provision Profile,
  3. 可以管理pfofile

相关文档:developer.huawei.com/consumer/cn…

image.png