#鸿蒙通关秘籍#鸿蒙系统中如何转换Native应用信息为JavaScript对象?

HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
ORM晨光破晓

在鸿蒙系统中,要将应用信息从Native格式转换为JavaScript对象格式,需要以下步骤:

  1. 创建工程并初始化:

    • 在工程目录下生成cpp目录,包含libentry/index.d.ts, hello.cpp, CMakeLists.txt等文件
  2. 编辑构建文件:

    • src/main/cpp/CMakeLists.txt中,添加libbundle_ndk.z.sotarget_link_libraries依赖: bash target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so)
  3. 在Native侧,增加头文件: cpp #include "bundle/native_interface_bundle.h"

  4. 定义映射与转换方法:

    • 映射初始化: cpp EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { { "getCurrentApplicationInfo", nullptr, GetCurrentApplicationInfo, nullptr, nullptr, nullptr, napi_default, nullptr } };

      napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; } EXTERN_C_END

    • 定义数据转换方法: cpp static napi_value GetCurrentApplicationInfo(napi_env env, napi_callback_info info) { OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); napi_value result = nullptr; napi_create_object(env, &result);

      napi_value bundleName; napi_create_string_utf8(env, nativeApplicationInfo.bundleName, NAPI_AUTO_LENGTH, &bundleName); napi_set_named_property(env, result, "bundleName", bundleName);

      napi_value fingerprint; napi_create_string_utf8(env, nativeApplicationInfo.fingerprint, NAPI_AUTO_LENGTH, &fingerprint); napi_set_named_property(env, result, "fingerprint", fingerprint);

      char* appId = OH_NativeBundle_GetAppId(); napi_value napi_appId; napi_create_string_utf8(env, appId, NAPI_AUTO_LENGTH, &napi_appId); napi_set_named_property(env, result, "appId", napi_appId);

      char* appIdentifier = OH_NativeBundle_GetAppIdentifier(); napi_value napi_appIdentifier; napi_create_string_utf8(env, appIdentifier, NAPI_AUTO_LENGTH, &napi_appIdentifier); napi_set_named_property(env, result, "appIdentifier", napi_appIdentifier);

      free(nativeApplicationInfo.bundleName); free(nativeApplicationInfo.fingerprint); free(appId); free(appIdentifier);

      return result; }

  5. 在JavaScript中使用转换后的对象: javascript import testNapi from 'libentry.so';

    @Entry @Component struct Index { @State message: string = 'Hello World';

     build() {
         Row() {
             Column() {
                 Text(this.message)
                     .fontSize(50)
                     .fontWeight(FontWeight.Bold)
    
                 Button() {
                     Text("GetCurrentApplicationInfo").fontSize(30)
                 }
                 .type(ButtonType.Capsule)
                 .margin({ top: 20 })
                 .backgroundColor('#0D9FFB')
                 .width('70%')
                 .height('5%')
                 .onClick(() => {
                     try {
                         let data = testNapi.getCurrentApplicationInfo();
                         console.info("getCurrentApplicationInfo success, data is " + JSON.stringify(data));
                     } catch (error) {
                         console.error("getCurrentApplicationInfo failed");
                         this.message = "getCurrentApplicationInfo failed";
                     }
                 })
             }
             .width('100%')
         }
         .height('100%')
     }
    

    }

分享
微博
QQ
微信
回复
5天前
相关问题