Android(安卓)NDK之二:创建NativeActivity
转:http://blog.csdn.net/xiruanliuwei/article/details/7560914
AndroidNDK为我们提供了两种方式来实现我们的nativeactivity:
1、Thenative_activity.hheaderdefinesthenativeversionoftheNativeActivityclass.It
containsthecallbackinterfaceanddatastructuresthatyouneedtocreateyournative
activity.Becausethemainthreadofyourapplicationhandlesthecallbacks,yourcallback
implementationsmustnotbeblocking.Iftheyblock,youmightreceiveANR(ApplicationNot
Responding)errorsbecauseyourmainthreadwillbeunresponsiveuntilthecallbackreturns.
Readthecommentsinthe
<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.hfilefor
moreinformation.
2、Theandroid_native_app_glue.hfiledefinesastatichelperlibrarybuiltontopofthe
native_activity.hinterface.Itspawnsanotherthreadtohandlethingssuchascallbacksor
inputevents.Thispreventsanycallbacksfromblockingyourmainthreadandaddssome
flexibilityinhowyouimplementthecallbacks,soyoumightfindthisprogrammingmodela
biteasiertoimplement.
The<ndk_root>/sources/android/native_app_glue/android_native_app_glue.c
sourceisalsoavailabletoyou,soyoucanmodifytheimplementationifyouneed.Readthe
commentsinthe<ndk_root>/sources/android/native_app_glue/android_native_app_glue.h
fileformoreinformation.
通过上面的描述,我们可以发现方式二会简单一些。在使用方式一实现native activity时,
需要注意在实现回调函数时,不要阻塞了main UI thread,否则会出现ANR。而方式二中,
则在一个新线程中创建一个事件循环执行回调函数,因此不会造成main UI thread阻塞。
更多相关文章
- C语言函数的递归(上)
- Android(安卓)获取验证码倒计时实现
- [置顶] Android(安卓)IPC 通讯机制源码分析【下】
- Android(安卓)Wifi --自动连接指定SSID(各种加密方式均可)
- android4.4.2 bluetooth解析(二)
- Android(安卓)NDK学习笔记11-JNI异常处理
- Android(安卓)Zygote进程源码分析
- android 底部弹出提示框的实现方式
- Android中实现短信发送的一种方式