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

我有两个库(.so),我在Java代码中加载。

但是有一些特定的操作需要Java(Activity)< - > C ++(.so文件)调用。

我可以使用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界面在同一时间同一活动?


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

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

  • 在Google提供的样本中,主体的评论非常明确:

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

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

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

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

    我认为通过本地Activity在C ++中处理会更好

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

    希望这可以帮助!

    链接地址: http://www.djcxy.com/p/5761.html

    上一篇: How to use Native Activity? Can it be combined with traditional activity?

    下一篇: Automated PayPal payments