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:

方式一:


方式二:





更多相关文章

  1. android中在子线程中更新UI的几种方法
  2. android利用handler线程间的通信
  3. 关于Android在非UI线程更新UI的问题。
  4. Android中帧动画在Activity启动时自动运行的几种方式
  5. Android中日志信息的打印方式
  6. android 线程通信Handler Bundle

随机推荐

  1. Android开发工具总结
  2. Android(安卓)Toast 用法总结
  3. Android底部导航栏之BottomNavigationBar
  4. Android(安卓)OpenGL ES(十):绘制三角形Tri
  5. android 之 SurfaceView使用(桌面弹球)
  6. Android(安卓)CountDownTimer倒计时器的
  7. 第六章 Android常见的UI基础控件(二)
  8. Android(安卓)与J2ME 虚拟机相关资料
  9. 【Android】广播大全 Intent Action 事件
  10. jdk环境变量的配置和android环境变量及ad