【每日学点鸿蒙知识】跳转三方地图、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

相关推荐
别说我什么都不会32 分钟前
【仓颉三方库】 数据库驱动——redis-sdk
harmonyos
悬空八只脚36 分钟前
React-Native开发鸿蒙NEXT-环境配置问题(续)
harmonyos
寒雪谷1 小时前
用户登陆UI
开发语言·javascript·ui·harmonyos·鸿蒙
simple_lau1 小时前
鸿蒙项目如何调起微信功能
harmonyos
simple_lau1 小时前
如何发布HarmonyOS应用
harmonyos·arkts·arkui
谢道韫6663 小时前
探索鸿蒙沉浸式:打造无界交互体验
microsoft·交互·harmonyos
ChinaDragonDreamer7 小时前
HarmonyOS:页面滚动时标题悬浮、背景渐变
harmonyos·鸿蒙
__Benco13 小时前
OpenHarmony - 小型系统内核(LiteOS-A)(一)
人工智能·harmonyos
明月与玄武15 小时前
HarmonyOS 5 开发环境全解析:从搭建到实战
华为·harmonyos·harmonyos 5开发环境
飞露15 小时前
HarmonyOS应用开发的工程目录结构
华为·harmonyos