Android NDK之纯 C 或 C++ 安卓工程NativeActivity

Android NDK支持用户使用“纯粹"的c或c++代码,这是对C或C++开发人员提供的便利,也是兼容性的体现,使得一些优秀的C或C++代码能完美的运行在安卓上,比如现在流行使用的视频处理库函数库FFMpeg库函数,经过移植可以完美运行在安卓平台,成为流媒体处理的基石,缩短了再次在安卓平台开发的周期。说远了,这篇文章主要介绍是将纯正的C或C++工程移植到安卓平台,开发人员没必要对Java语言进行理解也可以开发安卓应用,这在游戏开发领域有很好的应用,开发人员可以直接将一些游戏库,图形库移植到安卓平台。那么要怎么操作呢,看接下来的介绍。

首先最重要的就是 NativeActivity


The Android SDK provides a helper class, NativeActivity, that allows you to write a completely native activity. NativeActivity handles the communicationbetween the Android framework and your native code, so you do not have to subclass it or call itsmethods. All you need to do is declare your application to be native in yourAndroidManifest.xml file, and begin creating your native application.

An Android application using NativeActivity still runs in its own virtualmachine, sandboxed from other applications. You can therefore still access Android framework APIsthrough the JNI. In certain cases, however–such as for sensors, input events, andassets–the NDK provides native interfaces that you can use instead of having to callacross the JNI. For more information about such support, seeAndroid NDK Native APIs.

Regardless of whether or not you are developing a native activity, we recommend that you createyour projects with the traditional Android build tools. Doing so helps ensure building and packagingof Android applications with the correct structure.

The Android NDK provides you with two choices to implement your native activity:

  • The native_activity.hheader defines the native version of theNativeActivity class. It contains the callback interface and data structuresthat you need to create your native activity. Because the main thread of your application handlesthe callbacks, your callback implementations must not be blocking. If they block, you might receiveANR (Application Not Responding) errors because your main thread is unresponsive until the callbackreturns.
  • The android_native_app_glue.h file defines a static helper library built on top of thenative_activity.h interface.It spawns another thread, which handles things such ascallbacks or input events in an event loop. Moving these events to a separate thread prevents anycallbacks from blocking your main thread.

The <ndk_root>/sources/android/native_app_glue/android_native_app_glue.c source isalso available, allowing you to modify the implementation.

For more information on how to use this static library, examine the native-activity sampleapplication and its documentation. Further reading is also available in the comments in the <ndk_root>/sources/android/native_app_glue/android_native_app_glue.h file.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值