轉:http://blog.csdn.net/xiruanliuwei/article/details/7560914android
Android NDK爲咱們提供了兩種方式來實現咱們的native activity:app
一、The native_activity.h header defines the native version of the NativeActivity class. It函數
contains the callback interface and data structures that you need to create your nativeflex
activity. Because the main thread of your application handles the callbacks, your callbackui
implementations must not be blocking. If they block, you might receive ANR (Application Notthis
Responding) errors because your main thread will be unresponsive until the callback returns.spa
Read the comments in the .net
<ndk_root>/platforms/android-9/arch-arm/usr/include/android/native_activity.h file for線程
more information.orm
二、The android_native_app_glue.h file defines a static helper library built on top of the
native_activity.h interface. It spawns another thread to handle things such as callbacks or
input events. This prevents any callbacks from blocking your main thread and adds some
flexibility in how you implement the callbacks, so you might find this programming model a
bit easier to implement.
The <ndk_root>/sources/android/native_app_glue/android_native_app_glue.c
source is also available to you, so you can modify the implementation if you need. Read the
comments in the <ndk_root>/sources/android/native_app_glue/android_native_app_glue.h
file for more information.
經過上面的描述,咱們能夠發現方式二會簡單一些。在使用方式一實現native activity時,
須要注意在實現回調函數時,不要阻塞了main UI thread,不然會出現ANR。而方式二中,
則在一個新線程中建立一個事件循環執行回調函數,所以不會形成main UI thread阻塞。