#鸿蒙通关秘籍#如何在鸿蒙应用中获取应用自身信息?

HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
数据小英雄

在鸿蒙应用开发中,通过使用Native Bundle接口可以获取应用自身的相关信息。

  1. 打开src/main/cpp/CMakeLists.txt文件,在target_link_libraries依赖中添加libbundle_ndk.z.so: bash target_link_libraries(entry PUBLIC libace_napi.z.so libbundle_ndk.z.so)

  2. src/main/cpp/hello.cpp中添加头文件: cpp #include "bundle/native_interface_bundle.h"

  3. 初始化映射: 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

  4. 添加获取应用信息的方法: 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天前
相关问题
如何获取应用自身的bundleName
2507浏览 • 1回复 待解决
应用如何获取应用的metadata信息
1035浏览 • 1回复 待解决