2011-03-18 54 views
7

我有两个库(.so),我在java代码中加载。如何使用本机活动?它可以与传统活动结合吗?

但是有几个具体的操作需要Java(Activity)< - > C++(.so files)调用。

我可以使用Native Activity来实现这些功能的一部分吗?本土活动是传统活动的补充或我必须选择哪种类型的活动?

[编辑]

有一组事件可以在本机代码由native activity

机器人-NDK /源/机器人/ native_app_glue/android_native_app_glue.h

处理
enum { 
    /** 
    * Command from main thread: the AInputQueue has changed. Upon processing 
    * this command, android_app->inputQueue will be updated to the new queue 
    * (or NULL). 
    */ 
    APP_CMD_INPUT_CHANGED, 

    /** 
    * Command from main thread: a new ANativeWindow is ready for use. Upon 
    * receiving this command, android_app->window will contain the new window 
    * surface. 
    */ 
    APP_CMD_INIT_WINDOW, 

    /** 
    * Command from main thread: the existing ANativeWindow needs to be 
    * terminated. Upon receiving this command, android_app->window still 
    * contains the existing window; after calling android_app_exec_cmd 
    * it will be set to NULL. 
    */ 
    APP_CMD_TERM_WINDOW, 

    /** 
    * Command from main thread: the current ANativeWindow has been resized. 
    * Please redraw with its new size. 
    */ 
    APP_CMD_WINDOW_RESIZED, 

    /** 
    * Command from main thread: the system needs that the current ANativeWindow 
    * be redrawn. You should redraw the window before handing this to 
    * android_app_exec_cmd() in order to avoid transient drawing glitches. 
    */ 
    APP_CMD_WINDOW_REDRAW_NEEDED, 

    /** 
    * Command from main thread: the content area of the window has changed, 
    * such as from the soft input window being shown or hidden. You can 
    * find the new content rect in android_app::contentRect. 
    */ 
    APP_CMD_CONTENT_RECT_CHANGED, 

    /** 
    * Command from main thread: the app's activity window has gained 
    * input focus. 
    */ 
    APP_CMD_GAINED_FOCUS, 

    /** 
    * Command from main thread: the app's activity window has lost 
    * input focus. 
    */ 
    APP_CMD_LOST_FOCUS, 

    /** 
    * Command from main thread: the current device configuration has changed. 
    */ 
    APP_CMD_CONFIG_CHANGED, 

    /** 
    * Command from main thread: the system is running low on memory. 
    * Try to reduce your memory use. 
    */ 
    APP_CMD_LOW_MEMORY, 

    /** 
    * Command from main thread: the app's activity has been started. 
    */ 
    APP_CMD_START, 

    /** 
    * Command from main thread: the app's activity has been resumed. 
    */ 
    APP_CMD_RESUME, 

    /** 
    * Command from main thread: the app should generate a new saved state 
    * for itself, to restore from later if needed. If you have saved state, 
    * allocate it with malloc and place it in android_app.savedState with 
    * the size in android_app.savedStateSize. The will be freed for you 
    * later. 
    */ 
    APP_CMD_SAVE_STATE, 

    /** 
    * Command from main thread: the app's activity has been paused. 
    */ 
    APP_CMD_PAUSE, 

    /** 
    * Command from main thread: the app's activity has been stopped. 
    */ 
    APP_CMD_STOP, 

    /** 
    * Command from main thread: the app's activity is being destroyed, 
    * and waiting for the app thread to clean up and exit before proceeding. 
    */ 
    APP_CMD_DESTROY, 
}; 

因为我知道我的部分代码(应该在特定事件后调用)是用C++编写的,我认为它会更好通过本地活动在C++中处理。不过,我也有代码必须在Java中的句柄事件后调用。

问题是...我可以有我的活动的本地版本(本机界面),这将有助于我的一些事件,并在这同一时间的同一活动的传统java界面?

+0

据我了解@noisy你的问题是,“你无法加载两个本地库”我是否正确? – 100rabh 2011-03-21 09:59:09

回答

3

我会回答你不能有一个活动的代码的两个版本。

  • 您如何在您的清单中指定?

  • 在由谷歌提供的样品,主要的意见是非常明确的:

它运行在自己的线程,有自己的事件循环用于接收输入事件和做其他事情

本地活动将处理循环中的所有事件while(1) {...}。混合Java和本地事件是不可能的。

恕我直言,使用本地活动的主要原因是用户界面。如果您已经在C++中拥有全功能的用户界面,那么您可以更轻松地使用本地活动,并且更便于使用。您仍然可以自定义您的Android应用程序添加其他Java活动(不要忘记在您的清单中放入android:hasCode="TRUE"!)。在另一种情况下,使用java活动可以让您完全使用google用户界面,并在需要时调用您的本地库。

关于你的性能问题,当你说:

我认为这将是更好的通过本地活动

来处理这在C++来看看这个:http://developer.android.com/guide/practices/design/performance.html#native_methods

希望这可以帮助!

相关问题