DEV Community

Cover image for [Learn HarmonyOS Next Knowledge Daily] Jumping to Third-party Maps, getStringSync Performance, Keyboard Avoidance Mode, etc.
kouwei qing
kouwei qing

Posted on

[Learn HarmonyOS Next Knowledge Daily] Jumping to Third-party Maps, getStringSync Performance, Keyboard Avoidance Mode, etc.

[Learn HarmonyOS Next Knowledge Daily] Jumping to Third-party Maps, getStringSync Performance, Keyboard Avoidance Mode, etc.

1. Jumping to Third-party Map Navigation Pages

Similar to jumping to a map APP navigation page in Android:

// Latitude and longitude of the destination and its name
double destinationLat = 36.547901;
double destinationLon = 104.258354;
String destinationName = "Destination Name";
// Build the URI
Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + destinationLat + "&dlon=" + destinationLon + "&dname=" + destinationName + "&dev=0&t=0");
// Create and start the 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 {
    // Prompt the user that Amap is not installed
    Toast.makeText(this, "Please install Amap first", Toast.LENGTH_LONG).show();
}
Enter fullscreen mode Exit fullscreen mode

For jumping to Petal Maps, refer to: 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);
Enter fullscreen mode Exit fullscreen mode

For jumping to Amap, refer to: 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: Typically obtained by calling getContext(this) as common.UIAbilityContext in a Component
this.context.startAbility(want, (err: BusinessError) => {
  if (err.code) {
    // Handle business logic errors
    console.error(`startAbility failed,code is ${err.code},message is ${err.message}`);
    return
  }
  // Execute normal business logic
  console.info('startAbility succeed')
})
Enter fullscreen mode Exit fullscreen mode

2. Limiting the Maximum Byte Count of Input Fields

The TextArea input field can set the maximum number of input characters via maxLength, but how to set the maximum number of input bytes? Since each character in UTF-8 occupies a different number of bytes and cannot be directly converted, Android EditText provides the setFilters method:

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;

...
}
Enter fullscreen mode Exit fullscreen mode

HarmonyOS currently does not provide this mechanism, and we look forward to updates in future versions.

3. Performance Issues with getStringSync

Using the system-provided getStringSync is slightly time-consuming (taking several milliseconds). Must an asynchronous callback be used?

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

Change it to rscManager.getStringSync(resource.id) to reduce the time to 40 microseconds.

4. How to Set the Keyboard Avoidance Mode

The keyboard avoidance mode can be set (default is KeyboardAvoidMode.OFFSET):

this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.RESIZE)
Enter fullscreen mode Exit fullscreen mode

5. Error When Calling C Methods in NAPI: Cannot read property encodeLame of undefined

When creating a Native C++ module, running the Add Demo works fine, but after using a precompiled MP3 encoder SO, all NAPI method calls throw the error: Cannot read property encodeLame of undefined. The Add method works successfully without depending on the MP3 SO library. The MP3 SO library is intact:

$ 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
Enter fullscreen mode Exit fullscreen mode

Dependency configuration:

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)
Enter fullscreen mode Exit fullscreen mode

Cause of the issue: Do not directly rename libmp3lame.so.0.0.0 to libmp3lame.so; use libmp3lame.so.0 instead. The current app's SO is included in the IDE-side packaging, allowing the app to provide SO in the form of libxxx.so.x. If two SO versions need to be included, the real name and SO name must be the same, specified to the major version number libxxx.x (without .y.z). Therefore, libxxx.so is usable, and libxxx.3.1 or libxxx.so.3.1.0 should be renamed to libxxx.x. Then, reconfigure and compile in the CMakeLists.txt file.

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

Top comments (0)