【每日学点鸿蒙知识】跳转三方地图、getStringSync性能、键盘避让模式等

1、跳转三方地图导航页

类似于Android 跳转到地图APP 导航页面:

// 目标地点的经纬度和名称 
double destinationLat = 36.547901; 
double destinationLon = 104.258354; 
String destinationName = "目的地名称"; 
// 构建URI Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + destinationLat + "&dlon=" + destinationLon + "&dname=" + destinationName + "&dev=0&t=0"); 
// 创建Intent并启动活动 
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addCategory(Intent.CATEGORY_DEFAULT); 
intent.setPackage("com.autonavi.minimap"); 
if (intent.resolveActivity(getPackageManager()) != null) 
{ 
	startActivity(intent); 
} else 
{ 
	// 提示用户未安装高德地图 
	Toast.makeText(this, "请先安装高德地图", Toast.LENGTH_LONG).show(); 
}

跳转花瓣地图参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/map-petalmaps-V5

import { common, Want } from '@kit.AbilityKit';
let petalMapWant: Want = {
  bundleName: 'com.huawei.hmos.maps.app',
  uri: 'maps://routes',
  parameters: {
    linkSource: 'com.other.app',
    destinationLatitude: 31.983015468224288,
    destinationLongitude: 118.78058590757131,
    destinationPoiId: '2031694785326435456',
    destinationName: '南京(雨花)国际软件外包产业园'
  }
}

let context = getContext(this) as common.UIAbilityContext;
context.startAbility(petalMapWant);

跳转高德地图参考:https://lbs.amap.com/api/amap-mobile/gettingstarted

let want: Want = {
  uri: 'amapuri://route/plan?sid=BGVIS1&dlat=39.98848272&dname=B&slat=39.92848272&dlon=116.47560823&did=BGVIS2&slon=116.39560823&sname=A&t=0'
}
// this.context:一般是在 Component 组件里调用 getContext(this) as common.UIAbilityContext 获取到的 UIAbilityContext
this.context.startAbility(want, (err: BusinessError) => {
  if (err.code) {
    // 处理业务逻辑错误
    console.error(`startAbility failed,code is ${err.code},message is ${err.message}`);
    return
  }
  // 执行正常业务
  console.info('startAbility succeed')
})

2、限制输入框最大字节数

TextArea 输入框可以通过maxLength设置最大输入字符数,但是如何设置最大输入的字节数?utf-8每个字符占用字节数量不一致,无法直接转换,Android EditText

提供了setFilters方式:

InputFilter[] inputFilters = new InputFilter[1];  
inputFilters[0] = new ByteLengthInputFilter(MSG_LENGTH_LIMIT_BYTES);  
mEditText.setFilters(inputFilters);

public class ByteLengthInputFilter implements InputFilter {  
  private final int mMax;  
  private final Charset mCharset;  
  private final CharsetDecoder mCharsetDecoder;

...
}

鸿蒙目前没有提供这种机制,期待后面版本迭代更新。

3、getStringSync性能问题

使用系统提供的getStringSync耗时有点高,要几毫秒,必须使用异步的回调的方式吗:

static getStringSync(context: common.Context | undefined, resource: Resource): string {  
  let rscManager = (context ?? getContext() as common.UIAbilityContext).resourceManager;  
    return rscManager.getStringSync(resource)  
}

可以改成rscManager.getStringSync(resource.id),使用id方式可以减少到40us。

4、如何设置键盘的避让模式

可以设置键盘避让模式,默认是KeyboardAvoidMode.OFFSET,:

this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE)

5、调用NAPI 中的C 方法报错:Cannot read property encodeLame of undefined

创建Native C++模块,直接运行Add Demo没有问题,但是使用预编译的MP3编码器的SO 后,调用所有的NAPI 方法都报错:Cannot read property encodeLame of undefined,不依赖MP3 SO库Add 方法可以调用成功。MP3 SO库没有问题:

$ ls -la
total 1648
drwxr-xr-x@ 7 shen  staff     224  8 12 23:29 .
drwxr-xr-x@ 6 shen  staff     192  8 12 23:29 ..
-rw-r--r--@ 1 shen  staff  484466  8 12 23:29 libmp3lame.a
-rwxr-xr-x@ 1 shen  staff    1007  8 12 23:29 libmp3lame.la
lrwxr-xr-x@ 1 shen  staff      19  8 12 23:29 libmp3lame.so -> libmp3lame.so.0.0.0
lrwxr-xr-x@ 1 shen  staff      19  8 12 23:29 libmp3lame.so.0 -> libmp3lame.so.0.0.0
-rwxr-xr-x@ 1 shen  staff  348448  8 12 23:29 libmp3lame.so.0.0.0

依赖方式:

add_library(lame SHARED IMPORTED)  
set_target_properties(lame  
    PROPERTIES  
    IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lame/libs/${OHOS_ARCH}/libmp3lame.so)  
  
add_library(audio_engine SHARED napi_init.cpp)  
target_link_libraries(audio_engine PUBLIC libace_napi.z.so lame)

问题原因:不能直接将libmp3lame.so.0.0.0 重命名为libmp3lame.so后使用,要使用libmp3lame.so.0。

当前应用的so是IDE侧打包带入的,允许应用通过 libxxx.so.x的方式提供so,如果需要同时带入两个版本的 so,real name 与 so name 名字要相同,明确到主版本号libxxx.x,不需要带上 .y.z;所以目前libxxx.so是能够使用的,libxxx.3.1以及libxxx.so.3.1.0需要改成libxxx.x的形式使用。然后需要在CMakeLists.txt文件中重新配置并编译。

参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-ndk-36-V5

相关推荐
knowledgebao7 小时前
OHOS(HarmonyOS) 编译 C/C++ 库
c语言·c++·harmonyos
塞尔维亚大汉8 小时前
OpenHarmony(鸿蒙南向开发)——小型系统内核(LiteOS-A)【中断及异常处理】
操作系统·harmonyos
Data-Miner11 小时前
81页精品PPT | 华为流程与信息化实践与架构规划分享
华为·架构
小哆啦小可爱13 小时前
uniapp 编译生成鸿蒙正式app步骤
华为·uni-app·harmonyos
hunteritself14 小时前
DeepSeek全球第二,R1生态扩展,华为&荣耀接入,OpenAI推出深度研究,谷歌Gemini 2.0发布!AI Weekly 2.3-2.9
人工智能·科技·华为·ai·chatgpt·deepseek
塞尔维亚大汉15 小时前
OpenHarmony(鸿蒙南向开发)——小型系统内核(LiteOS-A)
操作系统·harmonyos
时光凉忆19 小时前
鸿蒙开发 - 支持导出,跨文件使用的自定义样式 AttributeModifier
harmonyos·鸿蒙开发
freflying11191 天前
使用Jenkins实现鸿蒙HAR应用的自动化构建打包
自动化·jenkins·harmonyos
塞尔维亚大汉1 天前
OpenHarmony(鸿蒙南向开发)——轻量系统内核(LiteOS-M)【Shell】
操作系统·harmonyos
xianKOG2 天前
鸿蒙UI(ArkUI-方舟UI框架)- 使用文本
ui·华为·harmonyos·鸿蒙