Android NDK之二:创建NativeActivity
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阻塞。
下面分别以上面介绍的两种方式实现我们的native activity:
方式一:
方式二:
更多相关文章
- android中在子线程中更新UI的几种方法
- android利用handler线程间的通信
- 关于Android在非UI线程更新UI的问题。
- Android中帧动画在Activity启动时自动运行的几种方式
- Android中日志信息的打印方式
- android 线程通信Handler Bundle