如何使用Native Activity?它可以与传统活动相结合吗?

时间:2011-03-18 13:49:15

标签: android android-activity native

我有两个库(.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 ++编写的,我认为通过本机Activity在C ++中处理它会更好。但是,我还有一些代码,必须在Java中处理事件后调用。

问题是......我可以拥有我的活动的本机版本(本机界面),这将帮助我解决一些事件,同时还有传统的java接口来处理同一个活动吗?

1 个答案:

答案 0 :(得分:3)

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

  • 您如何在清单中指明?

  • 在Google提供的示例中,主要评论非常明确:

  

它在自己的线程中运行,具有自己的事件循环,用于接收输入事件和执行其他操作

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

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

关于你的表现问题,当你说:

  

我认为最好通过原生Activity

在C ++中处理这个问题

看看这个:http://developer.android.com/guide/practices/design/performance.html#native_methods

希望这有帮助!