开发者

How to use Native Activity? Can it be combined with traditional activity?

I have two libraries (.so), which I load in java code.

However there are a few specific operations which require Java(Activity)<->C++(.so files) calls.

Can I use Native Activity to implement part of those functionalities? Is native activity something which is additional to traditional activity or do I have to choose, which type of activity I will use?

[EDIT]

there is a set of events which can be handled in native code by native activity

android-ndk/sources/android/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 h开发者_运维问答as 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,
};

since I know that part of my code (which should be called after specific event) is written in C++, I think it will be better to handle this in C++ via native Activity. However I have also code which have to be called after handle events in Java.

question is... can I have native version (native interface) of my activity, which will help me with some events, and traditional java interface to this same activity in this same time?


I would answer you can't have two versions of code of one activity.

  • How would you specify that in your manifest ?

  • In the sample provided by Google, the comment of the main is quite explicit :

It runs in its own thread, with its own event loop for receiving input events and doing other things

The native activity would handle all events in the loop while(1) {...}. Mixing Java and native event won't be possible.

IMHO, the main reason to use a native activity is the UI. If you already have a fully functional UI in C++, then it is easier for you and more portable to use a native activity. You can still customize you app for android add other java-activity (don't forget to put android:hasCode="TRUE" in your manifest!). In the other case, using a java activity allows you to fully use the google UI, and call your native library when needed.

About you performance question, when you say :

I think it will be better to handle this in C++ via native Activity

take a look at this : http://developer.android.com/guide/practices/design/performance.html#native_methods

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜