转: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阻塞。

更多相关文章

  1. C语言函数的递归(上)
  2. Android(安卓)获取验证码倒计时实现
  3. [置顶] Android(安卓)IPC 通讯机制源码分析【下】
  4. Android(安卓)Wifi --自动连接指定SSID(各种加密方式均可)
  5. android4.4.2 bluetooth解析(二)
  6. Android(安卓)NDK学习笔记11-JNI异常处理
  7. Android(安卓)Zygote进程源码分析
  8. android 底部弹出提示框的实现方式
  9. Android中实现短信发送的一种方式

随机推荐

  1. Android监听网络变化 二
  2. 安卓学习资料推荐
  3. Android(安卓)SharedPreferences 存储复
  4. 利用Android(安卓)Studio、MAT对Android
  5. 关于Android的nodpi,xhdpi,hdpi,mdpi,ldp
  6. Android简明开发教程九:创建应用程序框架
  7. 面试题
  8. android studio for android learning (九
  9. Android之SlidingDrawer抽屉效果
  10. Android(安卓)TV Audio基本框架及启动流